kenty_s
@kenty_s

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

Sinatraの環境構築

解決したいこと

Sinatraの環境構築に1週間ほど苦戦、、、らちが明かず何とかお助けいただきたく投稿しますTT
※勉強を始めて1か月ほどです。

例)
スクールの課題でSinatraの環境を構築しています。
過去2回できたのですが、立ち上げるたびに、ほかのコードを打つたびに環境が壊れ、ついに打つ手なしの状況です。(スクールでも質問を数日繰り返していますが、今回は解決に至らず)
どうか玄人の皆様解決方法を教えて下さい。

発生している問題・エラー

web-1  | /app/app.rb:1:in `<top (required)>': undefined method `get' for main:Object 

ターミナル全文

Welcome to Ubuntu 24.04.1 LTS (GNU/Linux 5.15.167.4-microsoft-standard-WSL2 x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/pro

 System information as of Wed Feb 19 06:09:52 JST 2025

  System load:  3.15                Processes:             74
  Usage of /:   0.4% of 1006.85GB   Users logged in:       0
  Memory usage: 16%                 IPv4 address for eth0: 172.29.143.103
  Swap usage:   0%

 * Strictly confined Kubernetes makes edge and IoT secure. Learn how MicroK8s
   just raised the bar for easy, resilient and secure K8s cluster deployment.

   https://ubuntu.com/engage/secure-kubernetes-at-the-edge

This message is shown once a day. To disable it please create the
/home/kenta/.hushlogin file.
kenta@sakamoto:~$ cd 75456_kenty-s_introduction_sinatra
kenta@sakamoto:~/75456_kenty-s_introduction_sinatra$ docker compose build
[+] Building 0/0
[+] Building 0/1Building                                                                                           0.1s
[+] Building 6.1s (16/16) FINISHED                                                                       docker:default
 => [web internal] load build definition from Dockerfile                                                           0.1s
 => => transferring dockerfile: 374B                                                                               0.0s
 => [web] resolve image config for docker-image://docker.io/docker/dockerfile:1                                    3.3s
 => [web auth] docker/dockerfile:pull token for registry-1.docker.io                                               0.0s
 => CACHED [web] docker-image://docker.io/docker/dockerfile:1@sha256:93bfd3b68c109427185cd78b4779fc82b484b0b7618e  0.0s
 => => resolve docker.io/docker/dockerfile:1@sha256:93bfd3b68c109427185cd78b4779fc82b484b0b7618e36d0f104d4d801e66  0.0s
 => [web internal] load metadata for docker.io/library/ruby:3.1.4                                                  1.9s
 => [web auth] library/ruby:pull token for registry-1.docker.io                                                    0.0s
 => [web internal] load .dockerignore                                                                              0.0s
 => => transferring context: 2B                                                                                    0.0s
 => [web internal] load build context                                                                              0.1s
 => => transferring context: 14.63kB                                                                               0.1s
 => [web 1/6] FROM docker.io/library/ruby:3.1.4@sha256:8ebc619f459ec62a7d0763f910e50787cc7999de3c09632536b22aaf87  0.0s
 => => resolve docker.io/library/ruby:3.1.4@sha256:8ebc619f459ec62a7d0763f910e50787cc7999de3c09632536b22aaf8725d3  0.0s
 => CACHED [web 2/6] RUN apt-get update -qq &&     apt-get install -y build-essential sqlite3 &&     apt-get clea  0.0s
 => CACHED [web 3/6] WORKDIR /app                                                                                  0.0s
 => CACHED [web 4/6] COPY Gemfile* ./                                                                              0.0s
 => CACHED [web 5/6] RUN gem install bundler && bundle install                                                     0.0s
 => CACHED [web 6/6] COPY / .                                                                                      0.0s
 => [web] exporting to image                                                                                       0.1s
 => => exporting layers                                                                                            0.0s
 => => exporting manifest sha256:4d20e2cdb57183c82de439ae7c0dc2306fbfebe7ec6ca3cd19436b94de79320e                  0.0s
 => => exporting config sha256:d9fec578a94df367d04a7402c6ddbbba675e997b5e634c68b64599bea7ccfd89                    0.0s
 => => exporting attestation manifest sha256:8fbe270dce5ce3aa9d65b2652927935a5d02b9587ff9780e3d0f532df6af2395      0.0s
 => => exporting manifest list sha256:4af1491708be9df7fc66ca20fa7d8587700db2f63f84e1f8fba4cc94e5eaa9f3             0.0s
 => => naming to docker.io/library/75456_kenty-s_introduction_sinatra-web:latest                                   0.0s
[+] Building 1/1 to docker.io/library/75456_kenty-s_introduction_sinatra-web:latest                                0.0s
 ✔ Service web  Built                                                                                              6.8s
kenta@sakamoto:~/75456_kenty-s_introduction_sinatra$ docker compose up
[+] Running 1/1
 ✔ Container 75456_kenty-s_introduction_sinatra-web-1  Rec...                                                      0.4s
Attaching to web-1
web-1  | [1] Puma starting in cluster mode...
web-1  | [1] * Puma version: 6.4.2 (ruby 3.1.4-p223) ("The Eagle of Durango")
web-1  | [1] *  Min threads: 0
web-1  | [1] *  Max threads: 5
web-1  | [1] *  Environment: development
web-1  | [1] *   Master PID: 1
web-1  | [1] *      Workers: 3
web-1  | [1] *     Restarts: (✔) hot (✖) phased
web-1  | [1] * Preloading application
web-1  | [1] ! Unable to load application: NoMethodError: undefined method `get' for main:Object
web-1  |
web-1  | get '/' do
web-1  | ^^^
web-1  | Did you mean?  gets
web-1  |                gem
web-1  | bundler: failed to load command: puma (/usr/local/bundle/bin/puma)
web-1  | /app/app.rb:1:in `<top (required)>': undefined method `get' for main:Object (NoMethodError)
web-1  |
web-1  | get '/' do
web-1  | ^^^
web-1  | Did you mean?  gets
web-1  |                gem
web-1  |        from config.ru:1:in `require'
web-1  |        from config.ru:1:in `block (2 levels) in <top (required)>'
web-1  |        from /usr/local/bundle/gems/rack-3.1.3/lib/rack/builder.rb:108:in `eval'
web-1  |        from /usr/local/bundle/gems/rack-3.1.3/lib/rack/builder.rb:108:in `new_from_string'
web-1  |        from /usr/local/bundle/gems/rack-3.1.3/lib/rack/builder.rb:97:in `load_file'
web-1  |        from /usr/local/bundle/gems/rack-3.1.3/lib/rack/builder.rb:67:in `parse_file'
web-1  |        from /usr/local/bundle/gems/puma-6.4.2/lib/puma/configuration.rb:368:in `load_rackup'
web-1  |        from /usr/local/bundle/gems/puma-6.4.2/lib/puma/configuration.rb:290:in `app'
web-1  |        from /usr/local/bundle/gems/puma-6.4.2/lib/puma/runner.rb:162:in `load_and_bind'
web-1  |        from /usr/local/bundle/gems/puma-6.4.2/lib/puma/cluster.rb:371:in `run'
web-1  |        from /usr/local/bundle/gems/puma-6.4.2/lib/puma/launcher.rb:194:in `run'
web-1  |        from /usr/local/bundle/gems/puma-6.4.2/lib/puma/cli.rb:75:in `run'
web-1  |        from /usr/local/bundle/gems/puma-6.4.2/bin/puma:10:in `<top (required)>'
web-1  |        from /usr/local/bundle/bin/puma:25:in `load'
web-1  |        from /usr/local/bundle/bin/puma:25:in `<top (required)>'
web-1  |        from /usr/local/lib/ruby/3.1.0/bundler/cli/exec.rb:58:in `load'
web-1  |        from /usr/local/lib/ruby/3.1.0/bundler/cli/exec.rb:58:in `kernel_load'
web-1  |        from /usr/local/lib/ruby/3.1.0/bundler/cli/exec.rb:23:in `run'
web-1  |        from /usr/local/lib/ruby/3.1.0/bundler/cli.rb:486:in `exec'
web-1  |        from /usr/local/lib/ruby/3.1.0/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
web-1  |        from /usr/local/lib/ruby/3.1.0/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
web-1  |        from /usr/local/lib/ruby/3.1.0/bundler/vendor/thor/lib/thor.rb:392:in `dispatch'
web-1  |        from /usr/local/lib/ruby/3.1.0/bundler/cli.rb:31:in `dispatch'
web-1  |        from /usr/local/lib/ruby/3.1.0/bundler/vendor/thor/lib/thor/base.rb:485:in `start'
web-1  |        from /usr/local/lib/ruby/3.1.0/bundler/cli.rb:25:in `start'
web-1  |        from /usr/local/lib/ruby/gems/3.1.0/gems/bundler-2.3.26/libexec/bundle:48:in `block in <top (required)>'
web-1  |        from /usr/local/lib/ruby/3.1.0/bundler/friendly_errors.rb:120:in `with_friendly_errors'
web-1  |        from /usr/local/lib/ruby/gems/3.1.0/gems/bundler-2.3.26/libexec/bundle:36:in `<top (required)>'
web-1  |        from /usr/local/bundle/bin/bundle:25:in `load'
web-1  |        from /usr/local/bundle/bin/bundle:25:in `<main>'
web-1 exited with code 1
kenta@sakamoto:~/75456_kenty-s_introduction_sinatra$ docker compose down
[+] Running 2/2r Desktop   o View Config   w Enable Watch
 ✔ Container 75456_kenty-s_introduction_sinatra-web-1  Rem...                                                      0.0s
 ✔ Network 75456_kenty-s_introduction_sinatra_default  Rem...                                                      0.3s
kenta@sakamoto:~/75456_kenty-s_introduction_sinatra$ docker compose build --no-cache
[+] Building 0/0
[+] Building 0/1Building                                                                                           0.1s
[+] Building 26.3s (14/14) FINISHED                                                                      docker:default
 => [web internal] load build definition from Dockerfile                                                           0.0s
 => => transferring dockerfile: 374B                                                                               0.0s
 => [web] resolve image config for docker-image://docker.io/docker/dockerfile:1                                    1.0s
 => CACHED [web] docker-image://docker.io/docker/dockerfile:1@sha256:93bfd3b68c109427185cd78b4779fc82b484b0b7618e  0.0s
 => => resolve docker.io/docker/dockerfile:1@sha256:93bfd3b68c109427185cd78b4779fc82b484b0b7618e36d0f104d4d801e66  0.0s
 => [web internal] load metadata for docker.io/library/ruby:3.1.4                                                  0.9s
 => [web internal] load .dockerignore                                                                              0.0s
 => => transferring context: 2B                                                                                    0.0s
 => CACHED [web 1/6] FROM docker.io/library/ruby:3.1.4@sha256:8ebc619f459ec62a7d0763f910e50787cc7999de3c09632536b  0.0s
 => => resolve docker.io/library/ruby:3.1.4@sha256:8ebc619f459ec62a7d0763f910e50787cc7999de3c09632536b22aaf8725d3  0.0s
 => [web internal] load build context                                                                              0.0s
 => => transferring context: 14.63kB                                                                               0.0s
 => [web 2/6] RUN apt-get update -qq &&     apt-get install -y build-essential sqlite3 &&     apt-get clean &&     5.8s
 => [web 3/6] WORKDIR /app                                                                                         0.0s
 => [web 4/6] COPY Gemfile* ./                                                                                     0.0s
 => [web 5/6] RUN gem install bundler && bundle install                                                           14.2s
 => [web 6/6] COPY / .                                                                                             0.2s
 => [web] exporting to image                                                                                       3.7s
 => => exporting layers                                                                                            3.0s
 => => exporting manifest sha256:f5250301104a790583b3df39008bfdfd93b804948cb6b0dbb746831bad294008                  0.0s
 => => exporting config sha256:4705d0dd89fef67de6be3ee4aae42847ab41cb654e16efe283528c7c75af0420                    0.0s
 => => exporting attestation manifest sha256:1c2856665edf0472273c196e7572bd4f08ef5d54ccb3ead006a604c7ebee7d29      0.0s
 => => exporting manifest list sha256:897b508594fbb9edce69cb5833582ed5f99900a6a4d4d567b83b997e644d54f2             0.0s
 => => naming to docker.io/library/75456_kenty-s_introduction_sinatra-web:latest                                   0.0s
[+] Building 1/1 to docker.io/library/75456_kenty-s_introduction_sinatra-web:latest                                0.6s
 ✔ Service web  Built                                                                                             26.5s
kenta@sakamoto:~/75456_kenty-s_introduction_sinatra$ docker compose up
[+] Running 2/2
 ✔ Network 75456_kenty-s_introduction_sinatra_default  Cre...                                                      0.0s
 ✔ Container 75456_kenty-s_introduction_sinatra-web-1  Cre...                                                      0.5s
Attaching to web-1
web-1  | [1] Puma starting in cluster mode...
web-1  | [1] * Puma version: 6.4.2 (ruby 3.1.4-p223) ("The Eagle of Durango")
web-1  | [1] *  Min threads: 0
web-1  | [1] *  Max threads: 5
web-1  | [1] *  Environment: development
web-1  | [1] *   Master PID: 1
web-1  | [1] *      Workers: 3
web-1  | [1] *     Restarts: (✔) hot (✖) phased
web-1  | [1] * Preloading application
web-1  | bundler: failed to load command: puma (/usr/local/bundle/bin/puma)
web-1  | [1] ! Unable to load application: NoMethodError: undefined method `get' for main:Object
web-1  |
web-1  | get '/' do
web-1  | ^^^
web-1  | Did you mean?  gets
web-1  |                gem
web-1  | /app/app.rb:1:in `<top (required)>': undefined method `get' for main:Object (NoMethodError)
web-1  |
web-1  | get '/' do
web-1  | ^^^
web-1  | Did you mean?  gets
web-1  |                gem
web-1  |        from config.ru:1:in `require'
web-1  |        from config.ru:1:in `block (2 levels) in <top (required)>'
web-1  |        from /usr/local/bundle/gems/rack-3.1.3/lib/rack/builder.rb:108:in `eval'
web-1  |        from /usr/local/bundle/gems/rack-3.1.3/lib/rack/builder.rb:108:in `new_from_string'
web-1  |        from /usr/local/bundle/gems/rack-3.1.3/lib/rack/builder.rb:97:in `load_file'
web-1  |        from /usr/local/bundle/gems/rack-3.1.3/lib/rack/builder.rb:67:in `parse_file'
web-1  |        from /usr/local/bundle/gems/puma-6.4.2/lib/puma/configuration.rb:368:in `load_rackup'
web-1  |        from /usr/local/bundle/gems/puma-6.4.2/lib/puma/configuration.rb:290:in `app'
web-1  |        from /usr/local/bundle/gems/puma-6.4.2/lib/puma/runner.rb:162:in `load_and_bind'
web-1  |        from /usr/local/bundle/gems/puma-6.4.2/lib/puma/cluster.rb:371:in `run'
web-1  |        from /usr/local/bundle/gems/puma-6.4.2/lib/puma/launcher.rb:194:in `run'
web-1  |        from /usr/local/bundle/gems/puma-6.4.2/lib/puma/cli.rb:75:in `run'
web-1  |        from /usr/local/bundle/gems/puma-6.4.2/bin/puma:10:in `<top (required)>'
web-1  |        from /usr/local/bundle/bin/puma:25:in `load'
web-1  |        from /usr/local/bundle/bin/puma:25:in `<top (required)>'
web-1  |        from /usr/local/lib/ruby/3.1.0/bundler/cli/exec.rb:58:in `load'
web-1  |        from /usr/local/lib/ruby/3.1.0/bundler/cli/exec.rb:58:in `kernel_load'
web-1  |        from /usr/local/lib/ruby/3.1.0/bundler/cli/exec.rb:23:in `run'
web-1  |        from /usr/local/lib/ruby/3.1.0/bundler/cli.rb:486:in `exec'
web-1  |        from /usr/local/lib/ruby/3.1.0/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
web-1  |        from /usr/local/lib/ruby/3.1.0/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
web-1  |        from /usr/local/lib/ruby/3.1.0/bundler/vendor/thor/lib/thor.rb:392:in `dispatch'
web-1  |        from /usr/local/lib/ruby/3.1.0/bundler/cli.rb:31:in `dispatch'
web-1  |        from /usr/local/lib/ruby/3.1.0/bundler/vendor/thor/lib/thor/base.rb:485:in `start'
web-1  |        from /usr/local/lib/ruby/3.1.0/bundler/cli.rb:25:in `start'
web-1  |        from /usr/local/lib/ruby/gems/3.1.0/gems/bundler-2.3.26/libexec/bundle:48:in `block in <top (required)>'
web-1  |        from /usr/local/lib/ruby/3.1.0/bundler/friendly_errors.rb:120:in `with_friendly_errors'
web-1  |        from /usr/local/lib/ruby/gems/3.1.0/gems/bundler-2.3.26/libexec/bundle:36:in `<top (required)>'
web-1  |        from /usr/local/bundle/bin/bundle:25:in `load'
web-1  |        from /usr/local/bundle/bin/bundle:25:in `<main>'
web-1 exited with code 1
kenta@sakamoto:~/75456_kenty-s_introduction_sinatra$
v View in Docker Desktop   o View Config   w Enable Watch

VSCode の app.rb

# frozen_string_literal: true

require 'sinatra'
require 'sinatra/reloader'
require 'sqlite3'

db = SQLite3::Database.new 'db/kakeibo.db'

enable :method_override

get '/' do
  erb :index
end

自分で試したこと

● ターミナルでリセット

docker compose down

● app.rb.saveで誤りであろうコードを削除

【削除前】

require 'sinatra'
require 'sinatra/reloader'
require 'sqlite3'

db = SQLite3::Database.new 'db/kakeibo.db'

enable :method_override
set :root,  File.dirname(__FILE__)
set :views, Proc.new { File.join(root, 'views') }

get '/' do
  erb :index
end
git checkout master
 git branch   
 git pull origin master  
 git checkout -b 02_hello_world 
 git branch

 require 'sinatra'

get '/hello' do
  "Hello World"
end

【削除後】

require 'sinatra'
require 'sinatra/reloader'
require 'sqlite3'

db = SQLite3::Database.new 'db/kakeibo.db'

enable :method_override

get '/' do
  erb :index
end

● VScodeの打ち直し

0

2Answer

エラーメッセージの /app/app.rb:1:in は app.rb の1行目にエラーがあることを表しています。1行目で呼んでいる get メソッドが未定義だといっていますが、質問に貼ってあるコードとは食い違っていますね。貼るコードを間違えたか、 app.rb が2箇所にあって編集しているのと実行したのが別のファイルだったりしませんか?

getrequire 'sinatra' の後でしか使えません。実行したコードは本当に get が1行目にあり、そのせいでエラーになっていると思われます。

0Like

Comments

  1. @kenty_s

    Questioner

    ご回答ありがとうございます。
    git clone
    を無尽蔵に行っていたがゆえに無数に不要なファイルができているようです、、、
    別のファイルがどれなのかを追求しつつ、ファイルを絞っていきます。

  2. git clone を乱発したとして不要なファイルが影響してくる状況がよく分からないですね。

    今回 Docker で起動しているのは ~/75456_kenty-s_introduction_sinatra ディレクトリの中にある app.rb のように見えます。どのディレクトリで他の何を clone したとしてもこの app.rb に直接影響する配置にはならないはずです。

自分の環境だと、次の内容で問題なく起動できました。

環境
Windows 11 Pro バージョン 24H2 (OSビルド 26100.3037)
WSL バージョン: 2.4.11.0
Ubuntu 22.04.5 LTS  カーネル 5.15.167.4-microsoft-standard-WSL2
Docker version 27.5.1, build 9f9e405
Dockerファイル
FROM ruby:3.1.4

RUN gem install sinatra rackup puma

COPY app.rb .

EXPOSE 4567

CMD ["ruby", "app.rb"]
app.rb
require 'sinatra'

set :bind, '0.0.0.0'

get '/' do
  'Hello docker!!'
end
me@Ryzen:~/sinatra_test$ ls
Dockerfile  app.rb

me@Ryzen:~/sinatra_test$ docker build -t sinatra_image .
[+] Building 6.1s (8/8) FINISHED                                                                         docker:default
 => [internal] load build definition from Dockerfile                                                               0.0s
 => => transferring dockerfile: 141B                                                                               0.0s
 => [internal] load metadata for docker.io/library/ruby:3.1.4                                                      1.3s
 => [internal] load .dockerignore                                                                                  0.0s
 => => transferring context: 2B                                                                                    0.0s
 => CACHED [1/3] FROM docker.io/library/ruby:3.1.4@sha256:8ebc619f459ec62a7d0763f910e50787cc7999de3c09632536b22aa  0.0s
 => [internal] load build context                                                                                  0.0s
 => => transferring context: 27B                                                                                   0.0s
 => [2/3] RUN gem install sinatra rackup puma                                                                      4.8s
 => [3/3] COPY app.rb .                                                                                            0.0s
 => exporting to image                                                                                             0.1s
 => => exporting layers                                                                                            0.0s
 => => writing image sha256:c16f3f8571a6ca7609b51f7c07e46c4b58a544e4946bd3fd11fb81bf3ef85ce5                       0.0s
 => => naming to docker.io/library/sinatra_image                                                                   0.0s

me@Ryzen:~/sinatra_test$ docker run -d -p 4567:4567 sinatra_image
c54780612a1b65a0470a5c9ae1148fe20bd650821ffbe6007607d842e412bf4d

me@Ryzen:~/sinatra_test$ docker ps -a
CONTAINER ID   IMAGE           COMMAND         CREATED          STATUS          PORTS                                       NAMES
c54780612a1b   sinatra_image   "ruby app.rb"   16 minutes ago   Up 16 minutes   0.0.0.0:4567->4567/tcp, :::4567->4567/tcp   stupefied_merkle

me@Ryzen:~$ docker logs c54780612a1b
Puma starting in single mode...
* Puma version: 6.6.0 ("Return to Forever")
* Ruby version: ruby 3.1.4p223 (2023-03-30 revision 957bb7cb81) [x86_64-linux]
*  Min threads: 0
*  Max threads: 5
*  Environment: development
*          PID: 1
== Sinatra (v4.1.1) has taken the stage on 4567 for development with backup from Puma
* Listening on http://0.0.0.0:4567
Use Ctrl-C to stop

docker.png

0Like

Comments

  1. @kenty_s

    Questioner

    ご返信ありがとうございます。
    余計なコードを打ちすぎてわかりにくくなっており申し訳ございませんが、こちらの課題では家計簿アプリを作成することになっております。
    混乱した原因がgit cloneの乱発にあるのではないかと指摘を受けたので精査してみます。

  2. Dockercompose.yml ファイルの内容を 見せてもらうことはできますか?

Your answer might help someone💌