Access instance variable values in array of objects: Ruby

Rob Matthews

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!

Andrey Deineko

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Access Values in Array of Objects?

ruby access instance variable in instance_eval

Ruby: Access instance variable of parent object

access type variable values in instance declaration

Ruby: Nested Array behaving strangley as an Instance Variable

How to load some values to an instance of an array of objects?

Sorting an array of objects based on an instance variable

Using variable keys to access values in JavaScript objects

Ruby - How to change an instance variable in various objects using map method

manipulate nested array of objects to access children values

How do you access a Ruby instance variable/method dynamically?

How can I access an instance variable inside of a Ruby block?

Is there a way to store an array of Objects and the associated instance of another table in a single variable?

Ruby - instance variable as class variable

Access instance variable in python

How, with Ruby, can I access and compare these nested array values?

Access and extract values from doubly nested array of objects in JavaScript

React.js - Unable to access values inside array of objects

Access (and count) just object values from Postgres JSONB array of objects

Angular - Access values of object stored in array of objects inside template

How to access key/pair values within objects inside of an array in Ansible?

How do I access values on an array of objects in mustache template in moodle?

Javascript variable is an array of objects but can't access the elements

Mapping through array of objects and access their property dynamically (with a variable)

Array of objects values to array

Unique objects in the ruby ​array

Transform array of objects in Ruby

Sort an array of objects in ruby

Ruby array of objects