Last active
August 29, 2015 14:17
-
-
Save thash/df8913fac159645a9b7e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Shell | |
end | |
Shell.autoload :Core, './core' # 同ディレクトリのcore.rbを指す | |
puts "Core is not loaded yet." | |
puts Shell::Core.new # Coreが必要になる | |
# => loading core.rb # ここで初めて読まれる | |
# => core -- initialized | |
#<Shell::Core:0x007f89998938c8> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
puts 'loading core.rb' | |
class Shell | |
class Core | |
def initialize | |
puts "core -- initialized" | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative 'def/bar' | |
p Bar.new | |
# 以下のように書くのと同じ効果 | |
require File.expand_path('def/bar', File.dirname(__FILE__)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
load './loaded.rb' | |
p XYZ # => 123 | |
load './loaded.rb' | |
#=> warning: already initialized constant XYZ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ls | |
load.rb | |
loaded.rb | |
$ cat loaded.rb | |
XYZ = 123 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# $LOAD_PATH, $:は同じものを指すグローバル変数 | |
p $LOAD_PATH == $: # => true | |
p $LOAD_PATH | |
# => ["/Users/hash/.rbenv/versions/2.1.2/lib/ruby/site_ruby/2.1.0", | |
# "/Users/hash/.rbenv/versions/2.1.2/lib/ruby/site_ruby/2.1.0/x86_64-darwin13.0", | |
# "/Users/hash/.rbenv/versions/2.1.2/lib/ruby/site_ruby", | |
# "/Users/hash/.rbenv/versions/2.1.2/lib/ruby/vendor_ruby/2.1.0", | |
# "/Users/hash/.rbenv/versions/2.1.2/lib/ruby/vendor_ruby/2.1.0/x86_64-darwin13.0", | |
# "/Users/hash/.rbenv/versions/2.1.2/lib/ruby/vendor_ruby", | |
# "/Users/hash/.rbenv/versions/2.1.2/lib/ruby/2.1.0", | |
# "/Users/hash/.rbenv/versions/2.1.2/lib/ruby/2.1.0/x86_64-darwin13.0"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# LOAD_PATH以下にprime.rbがある | |
p $LOAD_PATH.select {|path| | |
File.exist?(path + '/prime.rb') | |
} | |
#=> ["/Users/hash/.rbenv/versions/2.1.2/lib/ruby/2.1.0"] | |
# $LOAD_PATHにカレントディレクトリを追加すれば、ファイル名だけでrequireできる | |
$LOAD_PATH << '.' | |
require 'require_b' #=> true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
XYZ = 123 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ tree . | |
. | |
└── abc | |
├── def | |
│ └── bar.rb | |
└── foo.rb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ls | |
require_a.rb | |
require_b.rb | |
$ cat require_b.rb | |
class B | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ファイル名だけでは読み込めない | |
# require 'require_b' | |
# => in `require': cannot load such file -- require_b (LoadError) | |
# カレントディレクトリ(.)を書く必要がある | |
require './require_b' #=> true (読み込み成功) | |
require './require_b' #=> false (読み込み済なので何もしない) | |
# 別ファイルで定義したクラスが使える | |
p B.new #=> #<B:0x007ffc51888088> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class B | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require './loaded' #=> true | |
p XYZ # => 123 | |
require './loaded' #=> false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment