Link to home
Start Free TrialLog in
Avatar of neophyteScripter
neophyteScripter

asked on

Attaching array to existing Ruby method

Neophyte has another question. I understand arrays and there purpose and am able to work with arrays adding and deleting information using the irb and prompt through the Mac terminal. What I am trying to do is connect an array to an existing ruby file that already has the class established and is producing the results. Basically, I would like to be able to push new objects through the method using an array.
# NewDeviceClass
#device
#nickname : old home pc
# status  : active
# type    : windows_pc
class NewDevice
    attr_accessor :device_name
      attr_accessor :device_status
      attr_accessor :device_type

  def output
  puts "This device has a nickname of #{device_name}, type of #{device_type}, and is #{device_status}."

  end

end

if __FILE__ == $0
dn = NewDevice.new
dn.device_name = "new device"
dn.device_status = "active"
dn.device_type = "windows pos"
dn.output

a = ["device_type1", "device_type2"]
end

Open in new window

Avatar of neophyteScripter
neophyteScripter

ASKER

basically, I need to create a for loop and push newdevices into the main method statement
There are a lot of options if you want to loop. I use Array#each a lot -- it is able to handle iteration on the collection most simply.

# Call whatever you use to get the array of NewDevice objects.
new_devices = NewDevice.find(:all)

# Perform a block on each member of the collection.
# new_device becomes the current member of the collection.
new_devices.each do |new_device|
  new_device.output
  # ... maybe other code you want to execute
end

# Alternatively, you can pass a reference to the object method to be called.
new_devices.each(&:output)

# You can also do everything straight away.
NewDevice.find(:all).each(&:output)

Open in new window

can you go into more detail, pretend i have no clue what you are talking about. Thanks
ASKER CERTIFIED SOLUTION
Avatar of kristinalim
kristinalim
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial