📄 12 - making a hash more like a struct.rb
字号:
require 'rubygems'require 'facet/hash/to_ostruct'cliches = { 'hammer' => 'tongs', :lock => ['stock', 'barrel'] }cliches_struct = cliches.to_ostructcliches_struct.hammer # => "tongs"cliches_struct.lock # => ["stock", "barrel"]cliches_struct.rolling_stone # => nil#---class StructlikeHash < Hash def method_missing(name, *args) if include?(name) self[name] elsif include?(name.to_s) self[name.to_s] else super end endend#---cliches = StructlikeHash['cat' => 'mouse', 'grin' => { 'bear' => 'it' } ]cliches.cat # => "mouse"cliches.creek # NoMethodError: undefined method `creek' for {"cat"=>"mouse", "grin"=>{"bear"=>"it"}}:StructlikeHash#---class StructlikeHash2 < Hash def method_missing(name, *args) if include?(name) self[name] elsif include?(name.to_s) self[name.to_s] else begin super rescue NoMethodError nil end end endendcliches = StructlikeHash2['cat' => 'mouse', 'grin' => { 'bear' => 'it' } ]cliches.creek # => nil#---cliches[:keys] = 'to the kingdom'cliches[1] = 'and only'cliches[:top] = 'of the world'cliches["top"] = 'to bottom'cliches.keys # => ["cat", :top, 1, "grin", "top", :keys]cliches.top # => "of the world"cliches.1 # SyntaxError: compile error#---
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -