Associative array/Creation: Difference between revisions

m
→‎[[Ruby]]: Updated formatting. Added link to nil.
(Languages sorted)
m (→‎[[Ruby]]: Updated formatting. Added link to nil.)
Line 121:
 
==[[Ruby]]==
#aA hash object that returns [[nil]] for unknown keys
hash={}
hash[666]='devil'
Line 127:
hash[666] # => 'devil'
 
#aA hash object that returns 'unknown key' for unknown keys
hash=Hash.new('unknown key')
hash[666]='devil'
Line 133:
hash[666] # => 'devil'
 
#aA hash object that returns "unknown key #{key}" for unknown keys
hash=Hash.new{|h,k|h[k]="unknown key #{k}"}
hash[666]='devil'