Solved

Create a Class based on an Object

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now