Solved

Attaching array to existing Ruby method

Posted on 2011-03-21
4
529 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now