Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Hey guys,
- I need to loop through the instance variables of an object, and set them all to a value. So far I have:
- a.instance_variables.each do |var|
- #the next line I can't figure out
- a.instance_variable_get(var.to_s) = 5 #I don't know the terms to google to figure this out. I want the variable var represents to be assigned a value, but I cannot figure out the syntax.
- end
- Background: I actually have two identical (but unknown) objects, and I randomly assign a new instance of the same object properties from either of the two parents. It's some genetics stuff I'm mocking up, but Ruby isn't my strong suit. Thanks in advance.
Advertisement
Add Comment
Please, Sign In to add comment