Solved

Create a Class based on an Object

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

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…
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

617 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