Ruby Access Hash Element

Access Ruby hash variables

Try request.params["model"]["password"]

A Hash's keys can consist of both symbols and strings. However, a string key is different than a symbol key.

Note the following:

h = {:name => 'Charles', "name" => 'Something else'}
h[:name] #=> 'Charles'
h["name"] #=> 'Something else'

EDIT:

In your particular situation, it appears request.params["model"] returns a string instead of a hash. There is a method String#[] which is a means of getting a substring.

s = "Winter is coming"
s["Winter"] #=> "Winter"
s["Summer"] #=> nil

This would explain your comments.

There are a couple things you can do to remedy your specific situation. I have found the most simplest way to be using JSON. (I'm sure there are others and maybe those will surface through other answers or through comments.)

require 'json'
hash_of_params = JSON.load(request.params["model"]).to_hash
hash_of_params["password"] #=> "36494092d7d5682666ac04f62d624141"

Accessing elements of a ruby hash

With a Hash, iteration is made through key first, then value. So have your block use what you need.

test.each do |key|
puts key
end

test.each do |key, value|
puts key
puts value
end

There are also

test.each_key do |key|
puts key
end

test.each_value do |value|
puts value
end

Sidenote: id is inside test["foo"], so maybe you'd need 2 loops


To get id from your hash directly:

test["foo"]["id"]

test["foo"].each {|k, v| puts "#{k}: #{v}" }

Ruby access hash value by a variable

Old good recursive Ruby 1.9+ solution:

hash = {:test => {:foo => true}}
path = [[:test],[:foo]]

path.flatten.reduce(hash) { |h, p| h[p] }
#⇒ true

Or, as @Stefan suggested in comments:

path.reduce(hash) { |h, (p)| h[p] }
# or even
path.reduce(hash) { |h, p| h[p.first] }

More defensive:

path.flatten.reduce(hash) { |h, p| h.nil? ? nil : h[p] }

How to access a symbol hash key using a variable in Ruby

You want to convert your string to a symbol first:

another_family[somevar.to_sym]

If you want to not have to worry about if your hash is symbol or string, simply convert it to symbolized keys

see: How do I convert a Ruby hash so that all of its keys are symbols?

Rails access hash value

@options[:priority]["value"] looks to be a strong containing json, not a hash. This is why you get an error when using [:id] (this method doesn't accept symbols) and why ["id"] returns the string "id".

You'll need to parse it first, for example with JSON.parse, at which point you'll have a hash which you should be able to access as normal. By default the keys will be strings so you'll need

JSON.parse(value)["id"]

Accessing Data inside hash / array

You should be able to do note_attributes[0].name to access it

Accessing hash value with variable

Try this:

print "Enter number "
num = gets.chomp().to_i
puts "Value: #{DHASH[num]}"

Accessing hash values with a variable in Ruby is wonderfully easy! You just make sure the variable has the proper key, and then use the variable instead of the key. In your case, the number that you are getting will be a string, and you need it to be an integer, so you need to turn it into an integer. And you need to correct the string interpolation.

Ruby access hash element

Well, you have a hashmap (not an array) which maps the key "metrics" to an array. That array contains a hash as its only element. And that hash maps the key :pageviews to the value 25474. So to get that value you can do:

the_hash["metrics"][0][:pageviews]

This assumes that the hash with the :pageviews key will always be at position 0 in the array, which the key "metrics" is mapped to.

How to access an array element inside a hash in Ruby

Here's one way to access the second value of the first key of your hash:

cities.values.first[1]
# => -87.61184692382812

This fetches the value of your first key (in this case it's that first array in the hash), and then retrieves by index the second element of that array.

Ruby hash access value from string array

Assuming that the user inputs numbers for array indices and words for hash keys:

keys = string.scan(/(\d+)|(\w+)/).map do |number, string|
number&.to_i || string.to_sym
end

hash.dig(*keys) # => "d"


Related Topics



Leave a reply



Submit