Solved

Create a Class based on an Object

Posted on 2011-03-17
2
346 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

Title # Comments Views Activity
What is Ruby programming? 7 431
Upgrade Ruby from 1.8.7 to 1.9.2-p320 7 327
Github help 2 464
How to extract values from string in Ruby on Rails 3 403
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…
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…

839 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