1

I have an array of objects with one instance variable:

oranges_array = 
[#<Orange:0x007faade859ea0 @diameter=2.7>,
 #<Orange:0x007faade859ef0 @diameter=2.8>,
 #<Orange:0x007faade859f40 @diameter=2.6>]

For example, how would I access the diameter instance variables? I want to eventually get their average. Do I need to or am I on the right track in thinking I maybe use an each loop to shovel each diameter into its own array then use inject? Or is there a simpler way to access the diameter variables? Thanks!

2
  • 3
    I noticed you never accept the answers given to your questions. Make sure to go over your questions some time and accept the best answers given they solve your question. This way you let the community know, that the problem is solved and at the same time you reward the effort of the one, who has provided the solution Commented Jan 10, 2017 at 5:16
  • right. Just going through my questions. Sorry about that. My love for this site has grown and I've learned better etiquette. Now I'm just trying to go back and give credit where it is due! Commented Mar 21, 2017 at 16:49

1 Answer 1

4

One option is to add attr_reader to Orange class:

class Orange
  attr_reader :diameter
end

Now you can get the avarage:

avg = oranges_array.map(&:diameter).inject(:+) / oranges_array.size.to_f
# with Ruby 2.4+ you can do the following:
# avg = oranges_array.sum(&:diameter) / oranges_array.size.to_f

Another option is to use Object#instance_variable_get (without having to add the attr_reader to the class:

avg = oranges_array.map { |orange| orange.instance_variable_get(:@diameter) }.inject(:+) / oranges_array.size.to_f

NOTE:

Using instance_variable_get for this task is the last resort option and I added it almost exclusively to show there's a way to get instance variables of objects in case there is, for example, no way to define attribute readers in the class.

Sign up to request clarification or add additional context in comments.

7 Comments

Using instance_variable_get is a very rude thing to do from an object-oriented design perspective, you're messing around with another object's private data. The attr_reader approach here is the best, and really only way to solve this properly. The latter's a last-ditch emergency option.
@tadman posted it almost exclusively for showing alternative option (for example, if user does not have a chance to add attr_reader to Orange), but I agree that instance_variable_get is definitely not a way to go here..
Yeah, I agree, just flagging that in case people get ideas.
How can we check "oranges_array" carries array of objects? Like oranges_array.is_a? like anything is there
@Ronats I don't understand what exactly you are trying to ask/do. Could you precise so I can try helping you out?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.