Solved

Attaching array to existing Ruby method

Posted on 2011-03-21
4
531 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:neophyteScripter
  • 2
  • 2
4 Comments
 

Author Comment

by:neophyteScripter
ID: 35183250
basically, I need to create a for loop and push newdevices into the main method statement
0
 
LVL 4

Expert Comment

by:kristinalim
ID: 35183463
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

0
 

Author Comment

by:neophyteScripter
ID: 35183761
can you go into more detail, pretend i have no clue what you are talking about. Thanks
0
 
LVL 4

Accepted Solution

by:
kristinalim earned 500 total points
ID: 35184064
# You implied you already have a way of coming up with the objects you
# need to process. Assign this collection of objects to a local variable, say
# "new_devices". So "new_devices" at this point will be the array of objects.
new_devices = NewDevice.find(:all)

# The Ruby Array has a method Array#each that allows us to perform a
# block on each member of the array. It basically handles iteration for us so
# we no longer have to do the usual
#    for (int i = 0; i < length ; i ++)
# logic.

# Option 1 (IMHO preferred) among many more
# So we call the each method on the array and pass a block with one argument.
# What this will do is iterate through the array, and for each member call the block
# passing the member to the block. Ie, the current member is accessed using the
# variable "new_device" while inside the block.
new_devices.each do |new_device|
  # "new_device" at this point has been set to be the current member of the array
  new_device.output
  # ... maybe other code you want to execute
end

# Option 2 among many more
# Maybe a clearer way of illustrating its effect:
def something_to_perform_on_new_device(new_device)
  new_device.output
end
i = 0
begin
  something_to_perform_on_new_device(new_devices[i])
  i += 1
end while i < new_devices.length

# Option 3 among many more
# Good if you need to call only one method on the objects.
# This alternative form of calling the Array#each method calls the
# NewDevice#output method for each member of the array.
new_devices.each(&:output)

# Extra:
# If you don't want to have to store the array of NewDevice objects, you can
# just join the return values together:
NewDevice.find(:all).each(&:output)
# instead of:
new_devices = NewDevice.find(:all)
new_devices.each(&:output)

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question