Need to help to get data from bellow syntax


I need a help to read data from bellow syntax

[{:name=>“value1”}, {:name=>“Value2”}]

so its wiill be


but dont understand how to get that.
any help and explanation will be really helpful

Something like this, maybe:

bellow_syntax = [{:name=>"value1"}, {:name=>"Value2"}]
puts{ |hash|
  name = hash[:name]
  name =~ /^V(alu)e(2)$/ ? "v#$1#$2" : name

… but that’s mean.

[…] means an array, so you can use .each to iterate over its members.
{…} means a hash, so you can use foo[bar] to access its members
:foo means a symbol, which is just a ruby datatype

ary = [{:name=>"value1"}, {:name=>"value2"}]
ary.each do |hsh|
  # hsh == {:name=>"valueX"}
  # hsh[:name] == "valueX"
  puts hsh[:name]

The [ and ] indicate a list. So we need to loop over the list

bellow_syntax = [{:name=>“value1”}, {:name=>“Value2”}]

bellow_syntax.each do |item|
p item

This prints out the items in the list as shown bellow


The { and } indicate a hash. We want the value of the :name key so we do

bellow_syntax.each do |item|
p item[:name]

and this prints out


In a more Rubyish way we can do this

p{|x| x[:name]}

which returns a list of the values

[“value1”, “Value2”]

If you don’t know what list and hashes are, and the fact that you asked
this question means that you don’t, please follow the various tutorials
that get mentioned in this list on an almost daily basis. Once you have
done that the final answer I gave you will make sense.