Solved

Attaching array to existing Ruby method

Posted on 2011-03-21
4
533 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I recently rediscovered rails when I needed a holiday project and decided to build a management dashboard for the company where I work.  With it being a project done in my free time, I could focus my time on learning the basics rather than trying to…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

751 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