Undefined method when accessing hash element
Try something like this :
p = {:headline=>"Managing Director at Test company name", :pid=>"0tSsRvCR7r", :first_name=>"John", :last_name=>"Doe", :industry=>"Financial Services", :summary=>nil, :public_profile_url=>"http://www.linkedin.com/pub/john-doe/7a/78/606", :distance=>0}
puts p
puts p[:pid]
hash docs
more on hashes
Rails undefined method when accessing hash in view
I edited the form to use a select_tag
instead of a c.select
element, as follows:
= fields_for :custom_params do |c|
= label_tag('custom_params[:custom_plot_type]', "Plot Type")
= select_tag('custom_params[:custom_plot_type]', options_for_select(['Line', 'Scatter', 'Heat Map', 'Column'], @custom_params[:custom_plot_type]))
I still use the value of c for other form elements later on, but using _tag elements instead of elements related to the form builder allow me to use the arbitrary hash within the select tag - thanks @tgmerritt for the suggestion
undefined method `' for {}:Hash
It's because you are trying to assign values for a Hash using <<
(shovel operator).
Instead, try assigning them like this: (There should be key/value pairs in a Hash
)
a = Hash.new
a[:foo] = "Bom dia!"
or,
a = {foo: "Bom dia!"}
Ruby hash error: undefined method [] when attempting to set deeply nested keys
Hashes aren't nested by default. As my_hash[first_key]
is not set to anything, it is nil
. And nil
is not a hash, so trying to access one of its elements fails.
So:
my_hash = {}
first_key = 1
second_key = 2
third_key = 3
my_hash[first_key] # nil
my_hash[first_key][second_key]
# undefined method `[]' for nil:NilClass (NoMethodError)
my_hash[first_key] = {}
my_hash[first_key][second_key] # nil
my_hash[first_key][second_key] = {}
my_hash[first_key][second_key][third_key] = 100
my_hash[first_key][second_key][third_key] # 100
Ruby - NoMethodError: undefined method for hash
you can access the value like this
availability_zone_id = $evm.root.attributes["dialog_param_placement_availability_zone"]
Here are some great answers explaining how you can access hash values.
Getting undefined method `[]=' for nil:NilClass error when injecting array with hash
Since puts
is the last statement in the block and it returns nil
, nil
is returned causing the accumulator hash
to be nil
in the second iteration. You should return the hash. And you should pass the array as an argument to the inj_hash
method to make it reusable.
def inj_hash(arr)
arr.inject({}) do |hash, element|
hash[element.first] = element.last
hash
end
end
inj_hash([[:key1, "value1"], [:key2, "value2"]])
#=> {:key1=>"value1", :key2=>"value2"}
The simplest solution to create a hash from an array of key-value pairs is to use Hash::[]
.
arr = [[:key1, "value1"], [:key2, "value2"]]
Hash[arr]
#=> {:key1=>"value1", :key2=>"value2"}
Ruby undefined method for hash
Hash does not have an add
method (see here). It does have store
which requires a key & value pair so your code might read:
if schemes.has_key?(schemeName)
puts "This scheme has already been added "
else
schemes.store(schemeName, scheme)
end
Related Topics
How Can a Nested Class Access a Method in The Outer Class in Ruby
Rails App Moved to Production Server Gives "Dump Format Error for Symbol" Error
List of Ruby Metaprogramming Methods
Ruby: Multiply All Elements of an Array
Ruby JSON.Pretty_Generate ... Is Pretty Unpretty
How to Set Ruby Environment in Linux
Capistrano - Can't Deploy My Database.Yml
Using Each_With_Index with Map
Rbenv and Bundler: "Bad Interpreter: No Such File or Directory"
Alternative for Accepts_Nested_Attributes_For - Maybe Virtus
What's The Impact of Eager_Load=True
Ruby Group Hashes by Value of Key
How to Make Single Digit Number a Two Digit Number in Ruby
Compute Geo Distance in Elasticsearch
Set Compression Level When Generating a Zip File Using Rubyzip
Best Way to Use HTML5 Data Attributes with Rails Content_Tag Helper