Ruby Language Object-Oriented
Programming Concept
In Ruby, everything is an object. Even a class is an object
instance of Class. This has a huge implication in how Ruby works
especially in the area of meta-programming.
Expected duration: 120 minutes
(excluding homework)
Software Needed
Before you begin, you need to install the JDK and NetBeans IDE
software on your
computer as
described in
here.
- 5513_ruby_object.zip (download)
- It contains this document and the lab contents
- Download it and unzip in a directory of your choice
OS platforms you can use
- Windows
- Solaris x86, Solaris Sparc
- Linux
- Mac OS X
Change Log
Things to be done (By Sang Shin)
- Add more detailed explanation to the exercises
Lab Exercises
Exercise 1: The concept of "self"

Learning points
- Self objects in different context.

Tasks to be performed
- Open, build, and run
"RubyObject_self_in_TopLevelContext"
sample
application
- Open, build, and run
"RubyObject_self_in_instanceMethod"
sample application
- Open, build, and run
"RubyObject_self_in_ClassMethod"
sample application
- Open, build, and run
"RubyObject_self_in_SingletonMethod"
sample application
- Open, build, and run
"RubyObject_self_in_SingletonMethod2"
sample application
(1.1)
Open, build, and run "RubyObject_self_in_TopLevelContext" sample
application
1. Open "
RubyObject_self_in_TopLevelContext"
sample
application.
- Select File from
top-level menu and select Open Project.
- Browse down to <LAB_UNZIPPED_DIRECTORY>/ruby_object/samples
directory.
- Windows: If you unzipped the 5513_ruby_object.zip
file under C:\handsonlabs
directory under
Windows, the directory to which you want
to browse down should be C:\handsonlabs\ruby_object\samples.
- Linux/Solaris/MacOS: If you unzipped the 5511_ruby_object.zip
file under $HOME/handsonlabs
directory
under Linux, the directory to which you want
to browse down should be $HOME/handsonlabs/ruby_object/samples.
- Select RubyObject_self_in_TopLevelContext
project and click Open Project
button.
- Observe that the Open Project dialog
box appears.
- Observe that the main.rb
of the RubyObject_self_in_TopLevelContext
project is displayed in the editor window.
2. Study the code in main.rb.
puts self
puts self.class
# Top level methods are 'private instance methods of the Object class.
# Because top-level methods are private, you cannot call the with an
# explicit receiver; you can only call them with the default receiver,
# self.
def my_method
puts self
end
my_method
|
3. Build and run the application.
- Right click the project node and select Run. (or press Shift+F6)
- Observe the result in the Output window.
4. Spend some time understanding the
result.
(1.2)
Open, build, and run "RubyObject_self_instanceMethod" sample
application
1. Open "
RubyObject_self_in_InstanceMethod"
sample
application.
2. Study the code in main.rb.
class MyClass
def my_method
puts self
end
end
my_class = MyClass.new
my_class.my_method #<MyClass:0xae3364>
|
3. Run the project.
4. Study the result.
(1.3)
Open, build, and run "RubyObject_self_in_ClassMethod" sample
application
1. Open "
RubyObject_self_in_ClassMethod"
sample
application.
2. Study the code in main.rb.
class MyClass
# Define a class method
def self.my_method
puts self
end
end
MyClass.my_method # MyClass
|
3. Run the project.
4. Study the result.
(1.4)
Open, build, and run "RubyObject_self_in_SingletonMethod" sample
application
1. Open "
RubyObject_self_in_SingletonMethod"
sample
application.
2. Study the code in main.rb.
foo = Object.new
# Singleton methods are defined for a particular object. They can be
called
# by only the object . When a singleton method is executed, self is the
# object that owns the method.
def foo.bar
puts self
end
# This is another way a singleton method can be defined
class << foo
def bar2
puts self
end
end
foo.bar #
<Object:0x10ffb38>
foo.bar2 # <Object:0x10ffb38>
|
3. Run the project.
4. Study the result.
#<Object:0x10ffb38>
#<Object:0x10ffb38>
|
(1.5)
Open, build, and run "RubyObject_self_in_SingletonMethod2" sample
application
1. Open "
RubyObject_self_in_SingletonMethod2"
sample
application.
2. Study the code in main.rb.
class C
end
foo = C.new
# Singleton methods are defined for a particular object. They can be
called
# by only the object . When a singleton method is executed, self is the
# object that owns the method.
def foo.bar
puts self
end
foo.bar
# This should display the same object displayed above
puts foo |
3. Run the project.
4. Study the result.
#<C:0xb02928>
#<C:0xb02928>
|
Exercise 2: Singleton Method
(2.1)
Open, build, and run "RubyObject_SingletonMethod" sample application
1. Open "RubyObject_SingletonMethod"
sample application.
2. Study the code in main.rb.
foo = Object.new
# Singleton methods are defined for a particular object. They can be
called
# by only the object . When a singleton method is executed, self is the
# object that owns the method.
def foo.bar
puts self
end
# This is another way a singleton method can be defined
class << foo
def bar2
puts self
end
end
foo.bar
#<Object:0x10ffb38>
foo.bar2 #<Object:0x10ffb38> |
3. Run the project.
4. Study the result.
#<Object:0x10ffb38>
#<Object:0x10ffb38>
|
return to top of exercise
(2.2)
Open, build, and run "RubyObject_SingletonMethod2" sample application
1. Open "
RubyObject_SingletonMethod"
sample application.
2. Study the code in main.rb.
class C
end
foo = C.new
# Singleton methods are defined for a particular object. They can be
called
# by only the object . When a singleton method is executed, self is the
# object that owns the method.
def foo.bar
puts self
end
foo.bar
# This should display the same object displayed above
puts foo
|
3. Run the project.
4. Study the result.
#<C:0xb02928>
#<C:0xb02928>
|
return to top of exercise
Homework
Exercise (for people
who
are taking Sang Shin's "Ruby/JRuby/Rails Development online course")
<There is no homework since we are not covering this topic in this
session. This will be covered in the future sessions of this
course.>
<>
>