Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Attaching array to existing Ruby method

Posted on 2011-03-21
4
Medium Priority
?
536 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 2000 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

Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

Question has a verified solution.

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

Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

722 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