Module: ActiveTools::CoreExtension::Hashup::ArrayExtension
- Defined in:
- lib/active_tools/core_extension/hashup.rb
Instance Method Summary collapse
-
#hashup ⇒ Object
Return a nested Hash object from Array’s elements sequence, where elements used as names of
hash
keys.
Instance Method Details
#hashup ⇒ Object
Return a nested Hash object from Array’s elements sequence, where elements used as names of hash
keys. The last element of array would be the last nested value.
Example:
[:vehicle, :car, :ford, :mustang, "2 please"].hashup
#=> {:vehicle=>{:car=>{:ford=>{:mustang=>"2 please"}}}}
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/active_tools/core_extension/hashup.rb', line 14 def hashup raise(Exception, "At least 2 elements needed!") if size < 2 value = delete_at(-1) {}.tap do |hash| index = 0 while index < size hash = hash[at(index)] = (index + 1 == size) ? value : {} index += 1 end end end |