Created
January 29, 2024 01:34
-
-
Save satou-haruka-37/ed0d5fe5af5d1d5288a53088b104dfe2 to your computer and use it in GitHub Desktop.
Ruby Silver default_procによるデフォルト値の書き換え
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
hash = Hash.new { |h, k| "#{k}は存在しないキーです" } | |
p hash.default #=>nil | |
p hash.default_proc #=>#<Proc:0x000014e9e367fe08> | |
p hash[:hoge] #=>"hogeは存在しないキーです" | |
puts "---" | |
hash.default_proc = proc { |h, k| "#{k}というキーは存在しません" } | |
p hash.default_proc #=>#<Proc:0x00001532a916ed60> | |
p hash[:hoge] #=>"hogeというキーは存在しません" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment