Skip to content

Instantly share code, notes, and snippets.

@satou-haruka-37
Created January 29, 2024 01:34
Show Gist options
  • Save satou-haruka-37/ed0d5fe5af5d1d5288a53088b104dfe2 to your computer and use it in GitHub Desktop.
Save satou-haruka-37/ed0d5fe5af5d1d5288a53088b104dfe2 to your computer and use it in GitHub Desktop.
Ruby Silver default_procによるデフォルト値の書き換え
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