how can fetch data from hash nested array in ruby?

ruby nested hash
nested arrays ruby
ruby add to multidimensional array
ruby array of hashes get value
ruby iterate nested hash
ruby iterate over array of hashes
ruby hash of arrays
ruby create nested hash

I have an array like:

array = [:a, b: [:c, :d]]
 => [:a, {:b=>[:c, :d]}] 

when i was tried array[:b] i got this error:

TypeError (no implicit conversion of Symbol into Integer)

how can I get :b element from this array?

Note: I don't want to use index to do that (array[1]).

Because it is an array (and not a hash) you can get an element by its index. In this example:

#=> { :b => [:c, :d] }

4.4.2 Nested hashes, using each , it passes two values to the block: the key and the value of each element. The main difference between an array and a hash is the manner in which data is stored. Arrays, represented by square brackets, contain elements which are indexed beginning at 0. So if an array contained three elements, the indexes for those elements would be array [0], array [1] and array [2].

Note: I don't want to use index to do that (array[1]).

> array.find{|e| e.is_a?(Hash) && e.has_key?(:b)}
#=> {:b=>[:c, :d]}


> array.find{|e| e.has_key?(:b) rescue false}
#=> {:b=>[:c, :d]}

for your specific example:

> Hash[*array][:a][:b]
#=> [:c, :d] 

Ruby Primer - Hashes, in and out., Accessing Values in a Nested Array. If we want to access the first value in nested_array, we would do so in the same manner as a regular  Each named key is a symbol you can access in hash: options [:font_size] # => 10. A Hash can also be created through its ::new method: grades = Hash. new grades ["Dorothy Doe"] = 9. Hashes have a default value that is returned when accessing keys that do not exist in the hash. If no default is set nil is used.

i think this is the solution


in this type of array all (key: val) values will store on last element of array as a hash

for example in:

array = [:a, :b, c: [:d], e: [:f], g: [:h]]
 => [:a, :b, {:c=>[:d], :e=>[:f], :g=>[:h]}] 

I can access the :c element with:

 => [:d] 

or{|x| x.instance_of?(Hash)}.last[:c]
 => [:d]

Nested Arrays, Hashes & Loops in Ruby, To add data to a nested array, we can use the same << , or shovel, method we use to add data to a one-dimensional array. To add an element to an array that is nested inside of another array, we first use the same bracket notation as above to dig down to the nested array, and then we can use the << on it. The solution recursively check what type of key you are trying to fetch and depending on the type recursively call dig_and_collect either on a Hash directly, or on all the elements in the Array that you found along your path.

Nested Arrays Ruby, Easily Extract Deeply Nested Hash Data in Ruby. hash = { first: { second: { third: 'value' } } } hash[:first][:second][:third] #=> 'value' hash = {} hash[:first][:second][:third] #=> NoMethodError: undefined method `[]' for nil:NilClass. hash. try(:[], :first). try(:[], :second). try(:[], :third) #=> 'value' array = The inner arrays work exactly the same way than any other arrays, and can contain any type of data. multi_arr = [ [ 1 , 4 , "Hello" ], [ - 54 , false , "Ruby" ] ] Accessing values inside this data structure happens the same way than if you had one array, but you chain the bracket notations.

Easily Extract Deeply Nested Hash Data in Ruby, Are you hoping to return an array of values for each element or just a pared down hash? If you want an array of values you can do something lie: { |a| [ a['id​  Now that we are getting comfortable retrieving data from a nested array, let's work on adding data to such an array. Adding Data to a Nested Array To add data to a nested array, we can use the same << , or shovel, method we use to add data to a one-dimensional array.

Get values from nested Array : rails, Arrays and hashes are data structures that allow you to store multiple In this article, we will explore their syntaxes, how to populate them, retrieve values and called multi-dimensional arrays, arrays nested inside an array. Up to a certain version of Ruby the syntax that uses hash rockets was the only syntax to create Hashes, and it works the same for all Hashes no matter what kinds of objects you use as keys. Then, a few years back, a new syntax was introduced.