ruby notes(5)

  • When you invoke super with arguments, Ruby sends a message to the parent of the current object, asking it to invoke a method of the same name as the method invoking super.The way super handles arguments is as follows:
    1. When you invoke super with no arguments Ruby sends a message to the parent of the current object, asking it to invoke a method of the same name as the method invoking super. It automatically forwards the arguments that were passed to the method from which it’s called.
    2. Called with an empty argument list – super()-it sends no arguments to the higher-up method, even if arguments were passed to the current method.
    3. Called with specific arguments – super(a, b, c) – it sends exactly those arguments.
  • Unlike C++, instance variables cannot be inherited; The reason that they sometimes appear to be inherited is that instance variables are created by the methods that first assign values to them, and those methods are often inherited or chained.
  • The initialize method is always private.
  •  overload method by telling how many args passed in
    class Car
    def initialize(*args)
    if args.size == 2
    puts ‘2 arguments’
    else
    if args.size == 3
    puts ‘3 arguments’
    end
    end
    end; end
    Car.new([10, 23], 4, 10)  #=> 3 arguments
    Car.new([10, 23], [14, 13])  #=> 2 arguments
  • protected methods are called when the receiver is of the same class as ‘self’

class Person
def initialize(age)
@age = age
end
def age
@age
end
def compare_age(c)
if c.age > age  #works because self=Person and c is also an instance of Person
“The other object’s age is bigger.”
else
“The other object’s age is the same or smaller.”
end
end
protected :age
end

chris = Person.new(25)
marcos = Person.new(34)
puts chris.compare_age(marcos)
#puts chris.age => not work because self=main, but chris, the receiver, is of class Person.

With age protected rather than private, chris can ask marcos to execute age, because chris and marcos are both instances of the same class. But if you try to call the age method of a Person object when self is anything other than a Person object, the method will fail.

  • private/public/protected are only for methods; all instance and class variables are private, all constants are effectively public
  • Private methods cannot normally be invoked from outside the class that defines them. But they are inherited by subclasses. This means that subclasses can invoke them and can override them. initialize is a good example of such method.
  • In Ruby, we can define an abstractclass that invokes certain undefined “abstract” methods, which are left for subclasses to define. For example:
    # This class is abstract; it doesn’t define name
    any class that invokes methods that are intended for a subclass to implement is abstract
    class AbstractKlass
    def welcome; puts “hello #{name}”; end
    end
    # A concrete class
    class ConcreteKlass < AbstractKlass
    def name; “Ruby students”; end
    endConcreteKlass.new.welcome # Displays “hello Ruby students”
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s