Ruby Language Object-Oriented Programming Concept

Sang Shin, sang.shin@sun.com, www.javapassion.com/rubyonrails



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.


OS platforms you can use


Change Log


Things to be done (By Sang Shin)


Lab Exercises


Exercise 1: The concept of "self"


Learning points

Tasks to be performed
  1. Open, build, and run "RubyObject_self_in_TopLevelContext" sample application
  2. Open, build, and run "RubyObject_self_in_instanceMethod" sample application
  3. Open, build, and run "RubyObject_self_in_ClassMethod" sample application
  4. Open, build, and run "RubyObject_self_in_SingletonMethod" sample application
  5. 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.
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.

4. Spend some time understanding the result.

main
Object
main

                                                                                                                                                 return to top of exercise

(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.

#<MyClass:0xae3364>

                                                                                                                                                 return to top of exercise


(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.

MyClass

                                                                                                                                                 return to top of exercise



(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>


                    
                                                                                                                            return to top of exercise


(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>


                    
                                                                                                                            return to top of exercise

Exercise 2: Singleton Method


  1. Open, build, and run "RubyObject_SingletonMethod" sample application
  2. Open, build, and run "RubyObject_SingletonMethod2" sample application

(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.>


<>