Search
 
SCRIPT & CODE EXAMPLE
 

RUBY

find a key in nested hash ruby

def nested_hash_value(obj,key)
  if obj.respond_to?(:key?) && obj.key?(key)
    obj[key]
  elsif obj.respond_to?(:each)
    r = nil
    obj.find{ |*a| r=nested_hash_value(a.last,key) }
    r
  end
end

h = { foo:[1,2,[3,4],{a:{bar:42}}] }
p nested_hash_value(h,:bar)
#=> 42
Comment

ruby create nested hash from array

h = array.reverse.inject(value) { |a, n| { n => a } }
Comment

PREVIOUS NEXT
Code Example
Ruby :: ruby get decimal 
Ruby :: rails migration add default value 
Ruby :: ruby square root 
Ruby :: smallest base64 image string 
Ruby :: ruby create csv 
Ruby :: ruby pop array 
Ruby :: ruby not include 
Ruby :: ruby prepend array 
Ruby :: create a new hash from existing hash ruby 
Ruby :: Convert Date and time to utc in rails 
Ruby :: ruby latest version 
Ruby :: rails generate controller no respec 
Ruby :: %w meaning in ruby 
Ruby :: rails server stop pid 
Ruby :: rails always 2 decimal 
Ruby :: true sting to true in rails 
Ruby :: ruby on rails scaffold generator example with belongs to relationship 
Ruby :: ruby frozen_string_literal 
Ruby :: stripe test keys 
Ruby :: ruby generate array of numbers 
Ruby :: next rails gem 
Ruby :: run a specific delayed job from console 
Ruby :: rails check test database 
Ruby :: rails date field default today 
Ruby :: ruby global property from object 
Ruby :: rails rails admin secure page 
Ruby :: write heroku logs 
R :: select columns without na in r 
R :: read csv in r 
R :: create dataframe or table in r 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =