Associative array/Creation: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 48: | Line 48: | ||
@hash->{'key1', 'three'} = ('val1', -238.83); |
@hash->{'key1', 'three'} = ('val1', -238.83); |
||
==[[Ruby]]== |
|||
#a hash object that returns nil for unknown keys |
|||
hash={} |
|||
hash[666]='devil' |
|||
hash[777] # => nil |
|||
hash[666] # => 'devil' |
|||
#a hash object that returns 'unknown key' for unknown keys |
|||
hash=Hash.new('unknown key') |
|||
hash[666]='devil' |
|||
hash[777] # => 'unknown key' |
|||
hash[666] # => 'devil' |
|||
#a hash object that returns "unknown key #{key}" for unknown keys |
|||
hash=Hash.new{|h,k|h[k]="unknown key #{k}"} |
|||
hash[666]='devil' |
|||
hash[777] # => 'unknown key 777' |
|||
hash[666] # => 'devil' |