Ruby sample scripts - Object Orientation

Submitted by Chandrashekar Babu on August 15, 2010 - 4:01pm.
::

CloneTest.rb

# A Bad example for implementing accessors.
# Very much Java-like. See CloneTest2.rb
# for better implementation

class Address
def setCity(city)
@city = city
end

def getCity
@city
end
end

class Person
def getName
@name
end

def setName(name)
@name = name
end

def getAge
@age
end

def setAge(age)
@age = age
end

def getLocation
@location
end

def setLocation(loc)
@location = loc
end
end

p1 = Person.new
p1.setLocation Address.new

p1.setName "John"
p1.setAge 12
p1.getLocation.setCity "Chennai"

p2 = p1.clone # Shallow copy!

p1.setName "Smith"
p1.setAge 16
p1.getLocation.setCity "Delhi"

puts "Person p1 -> name: #{p1.getName}, age: #{p1.getAge}, city: #{p1.getLocation.getCity}"
puts "Person p2 -> name: #{p2.getName}, age: #{p2.getAge}, city: #{p2.getLocation.getCity}"



CloneTest2.rb

class Address
attr_accessor :city
end

class Person
attr_accessor :name, :age, :location
end

p1 = Person.new
p1.location = Address.new

p1.name = "John"
p1.age = 12
p1.location.city = "Chennai"

p2 = p1.clone # Shallow copy!

p2.name = "Bourne"
p2.age = 16
p2.location.city = "Delhi"

puts "Person p1 -> name: #{p1.name}, age: #{p1.age}, city: #{p1.location.city}"
puts "Person p2 -> name: #{p2.name}, age: #{p2.age}, city: #{p2.location.city}"




exception-test.rb

def printCallTrace
calltrace = caller
calltrace.each {|line| puts line }
end

class MyObject
def greet
puts "Hello..."
printCallTrace
end
end

begin
obj = MyObject.new
obj.greet
f = File.open("test.txt")

f.each_line do |line|
puts line
end
f.close

obj.sayHello

rescue SystemCallError
puts "Error in System call:" + $!
rescue NoMethodError
puts "Method not found: " + $!
return false
ensure
f.close unless f.nil?
end

puts "End of program..."




laptop.rb

require "openclose"

# Demonstration of module (cross-cutting functionality)

class Laptop
include OpenClose
def initialize
puts "Laptop initialized..."
end
end

class Cupboard
include OpenClose
def initialize
puts "Cupboard instantiated..."
end
end

laptop = Laptop.new
laptop.open
laptop.close

cupboard = Cupboard.new
cupboard.open
cupboard.close




method_missing_example.rb

class Foo
def method_missing(method_name, *args)
print "Method: #{method_name} called\n"
end

def sayHello(args)
puts "Hello #{args}"
end

end

foo = Foo.new
foo.sayHello("John")
foo.greet
foo.sdfsddsf



MyString.rb

class String
def speak
puts "My own custom method..."
end
end

str = "Hello world..."
str.speak




openclose.rb

module OpenClose
def open
puts "opened..."
end

def close
puts "closed..."
end
end




Person.rb

class Person

attr_reader :name, :age, :city

attr_writer :name, :age, :city

def greet
@name = "John"
puts "Hello..."
end

def sayGoodBye
puts @name
puts "Good bye..."
end

# private :sayGoodBye

end

#p = Person.new

#p.greet if p.respond_to?(:sdfsdfsd)

#p.sayGoodBye

#p.name = "Smith"

#puts p.name

#print "Enter method: "

#method_name = gets.chomp

#p = Person.new

#p.send(method_name);




SingletonTest.rb

require 'singleton'

class World
include Singleton
def show_id
self.object_id
end
end

a = World.instance
b = World.instance
puts a.show_id
puts b.show_id