1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Ruby シンボルでのuninitialized constant NET (NameError)

1
Last updated at Posted at 2026-06-01

書籍でRuby学習中にお会いしたuninitialized constant NET (NameError)ついて

参考にした書籍Rubyコードレシピ集

code1.rb
require 'net/http'
uri = URI.parse('https://runteq.jp/')
puts Net::HTTP.get(uri)

これをそのままターミナルに入力すると、HTML取得できます。が、

code1.rb
require 'net/http'
uri = URI.parse('https://runteq.jp/')
puts NET::HTTP.get(uri)

だと、uninitialized constant NET (NameError)が出る。

code1.rb
require 'net/http'
uri = URI.parse('https://runteq.jp/')
puts 'net/http'(uri)

だと、'net/http'が出力されるだけ。

なぜ書籍コードをいじったか

変えたらどうなるかなと興味本位です。

uninitialized constant NET (NameError)

このエラーはNETじゃなくて、Netだよということです。

Rubyでは、命名規則によりクラス名、モジュール名の最初の一文字は大文字であとは小文字と決まっています。
そのため、NETだとエラーになります。

じゃあ、HTTPは全部大文字じゃん。となりました。

HTTPのような略語は全部大文字でいいみたい。

code1.rb
require 'net/http'
uri = URI.parse('https://runteq.jp/')
puts Net::http.get(uri)

undefined method `http' for Net:Module (NoMethodError)が出ました。

略語は大文字でということですね。

1
0
8

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?