Hi
I need a help to read data from bellow syntax
[{:name=>“value1”}, {:name=>“Value2”}]
so its wiill be
value1
valu2
but dont understand how to get that.
any help and explanation will be really helpful
Thanks
Hi
I need a help to read data from bellow syntax
[{:name=>“value1”}, {:name=>“Value2”}]
so its wiill be
value1
valu2
but dont understand how to get that.
any help and explanation will be really helpful
Thanks
Something like this, maybe:
bellow_syntax = [{:name=>"value1"}, {:name=>"Value2"}]
puts bellow_syntax.map{ |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]
end
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
end
This prints out the items in the list as shown bellow
{:name=>“value1”}
{:name=>“Value2”}
The { and } indicate a hash. We want the value of the :name key so we do
this:
bellow_syntax.each do |item|
p item[:name]
end
and this prints out
“value1”
“Value2”
In a more Rubyish way we can do this
p bellow_syntax.map{|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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs