vendredi 6 janvier 2017

Update hash value in class

Here I have a little demo-code:

class UpdateHash
  def initialize(value)
    @value = value
    save
  end

  def save
    @value = 10
  end
end


hash = {uno: "50"}

UpdateHash.new(hash[:uno])

puts hash[:uno]
 => "50"

What would I have to change so that the result of puts hash[:uno] is not "50" but 10. Is there the possibility to update the value of the hash in the class, without having to pass the whole Hash?

This would work: UpdateHash.new(hash). But am I able to only pass the value and update the value in the class? Thanks!

Aucun commentaire:

Enregistrer un commentaire