Finding an object(s) within a hash with specific number using Ruby -


I need to create a method that takes a hash and number as arguments and new names according to age Changes / P>

  my_family_pets_ages = {"Evi" => 6, "hobbi" => 3, "George" => 12, "Bogart" = & gt; 4, "Pali" = & gt; 4, "Annabelle" => 0, "Move" = & gt; 3}   

I have two solutions for my problem:

  def my_hash_finding_method (source, thing_to_find) source.select {| Kashmir, V. V.include? Thing_to_find} end my_hash_finding_method (my_family_pets_ages, 3)   

or

  def my_hash_finding_method (source, thing_to_find) source.select {| Kashmir, V. V.has_value? Thing_to_find} end my_hash_finding_method (my_family_pets_ages, 3)   

When I run my program with IRB or command line, I get:

  my_solution Rb: 15: my_hash_finding_method contains `block ':' undefined method 'for 6?': Fixnum (NoMethodError)   

Why is included an undefined method ? When I use has_value , does that tell the same thing? Can I use the old version of Ruby? I am at 2.0.0

update :. With this new solution, I have come to work.

  def my_hash_finding_method (source, thing_to_find) num_array = [] Source.each do | K, v | If v == thing_to_find num_array & lt; & Lt; K end end print num_array.sort end    

I'm going to be a bit confused because it Sounds like homework, but do you know which Kashmir and V stands in that section?

The error message is a big sign that it is telling you that you tried to send a message which is not a hash, and naturally there are no Hashes defined methods for it. include on the FixName.

(If you want to know more then check to see what actually is being kept.)

In short: block inside, you work with variables Whose value is determined by the hash, which is not operating on the hash, you need check based on them

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -