Hi Readers,
Today we will know about the Ruby methods and parenthesis:
- When invoking a method argument parenthesis are optional
- Methods always have a receiver. The implicit receiver is self.
- Methods are identified by their name only. No overloading on argument signatures.
- There are class methods and instance methods
- Methods can be public, protected, or private
- The last evaluated expression in a method is the return value
- Arguments can have default values: def my_method(a, b = {})
Methods and Parenthesis:
def square(number)
number * number
end
square(2+2)*2 # => square(4)*2 = 32
square (2+2)*2 # => square(4*2) = 64
Method Visibility:
class MyClass
def method1 # Methods are public by default
end
# Protected methods can be invoked by any instance of the same class or a
# subclass of MyClass
protected
def method2
end
# Private methods can only be invoked within an instance of MyClass or a subclass
# of MyClass. The receiver of a private method is always self.
private
def method3
end
end
def method1 # Methods are public by default
end
# Protected methods can be invoked by any instance of the same class or a
# subclass of MyClass
protected
def method2
end
# Private methods can only be invoked within an instance of MyClass or a subclass
# of MyClass. The receiver of a private method is always self.
private
def method3
end
end
Hope you will understood, in next session i will come up with new concept.
Do you like my posts follow this blog by clicking the Follow button About me in right side menu.
Thank you.
No comments:
Post a Comment