Solved

Attaching array to existing Ruby method

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

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
what does getent do 3 52
shell script for killing a linux process 19 171
improve Shell script 5 155
Linux Hanging with disable Error Output 4 89
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…
In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below: http://www.rubytips.org/2008/05/13/accessing-windows-api-fro…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

821 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