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

x
?
Solved

Create a Class based on an Object

Posted on 2011-03-17
2
Medium Priority
?
351 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
[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 Comments
 
LVL 4

Accepted Solution

by:
kristinalim earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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 tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

730 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