Solved

Create a Class based on an Object

Posted on 2011-03-17
2
345 Views
Last Modified: 2012-05-11
As the name suggests, I am new to scripting in general and have my primary education in HTML and CSS. I'm just looking for a simple example of a Class that pulls in data from an Object.

Here is the base of what I am working on. Very simplistic

# NewDeviceClass

class NewDevice
    attr_accessor :device_names
    #Create the object
  def initialize(device_names = "Mobile Device Alpha")
    @device_names = device_names

  end

  #display nickname of device
def nickname
  if @device_names.nil?
    puts "..."
  elsif @device_names.respond_to?("each")

    @device_names.each do |device_names|
      puts "Device Nickname #{device_names}"
    end
  else
    puts "Hello #{@device_names}!"
  end
end

#End Tag
end


if __FILE__ == $0
dn = NewDevice.new
dn.nickname


#List of Device Names
dn.device_names = ["windows_pc", "apple_iphone", "android", "ubuntu", "symbian"]
dn.nickname

end


I know that I created the object here, but what if I wanted to be able to pull information from an existing object?

Any help is appreciated... FYI I am using NetBeans and running the files via Terminal on Mac OSX
0
Comment
Question by:neophyteScripter
2 Comments
 
LVL 4

Accepted Solution

by:
kristinalim earned 500 total points
ID: 35162120
Are you asking about how to declare class methods? Please clarify if you weren't.

class ClassWithClassMethod
  # Called on the class:
  #   ClassWithClassMethod.print_summary(device_object)
  def self.print_summary(device)
    # Calls object method on passed object
    device.nickname
  end
end

Open in new window


This ClassWithClassMethod does not have to be a separate class:

class NewDevice
  # ... the rest of your class definition

  # Called on the class:
  #   NewDevice.print_summary(device_object)
  def self.print_summary(device)
    # Calls object method on passed object
    device.nickname
  end
end

Open in new window


But remember that the example below works okay too. So always still consider who needs to be the caller.

class NewDevice
  # ... the rest of your class definition

  # Called on the object:
  #   device_object.print_summary
  def print_summary
    # Calls object method on current object (self)
    self.nickname
  end
end

Open in new window

0
 

Author Comment

by:neophyteScripter
ID: 35162456
Close enough. I think I have it now, thanks
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

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…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
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…

770 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