-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Karthikeyan A K
committed
Oct 29, 2013
1 parent
67c612a
commit d35cf8e
Showing
8 changed files
with
58 additions
and
2 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# define_method.rb | ||
|
||
class Square | ||
define_method :area do |side| | ||
side * side | ||
end | ||
end | ||
|
||
s = Square.new | ||
puts s.area 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ def print_line char = '_' length = 20 | |
end | ||
|
||
print_line | ||
print_line 40 | ||
print_line '*' | ||
print_line '&', 40 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# method_missing.rb | ||
|
||
class Something | ||
def method_missing method, *args, &block | ||
puts "You called: #{method}" | ||
p args | ||
block.call 7 | ||
end | ||
end | ||
|
||
s = Something.new | ||
s.call_method "boo", 5 do |x| | ||
x.times{ puts "in block" } | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# method_missing_in_action.rb | ||
|
||
class Person | ||
attr_accessor :name, :age | ||
|
||
def initialize name, age | ||
@name, @age = name, age | ||
end | ||
|
||
def method_missing method_name | ||
method_name.to_s.match(/get_(\w+)/) | ||
eval("self.#{$1}") | ||
end | ||
end | ||
|
||
person = Person.new "Zigor", "67893" | ||
puts "#{person.get_name} is #{person.get_age} years old" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Person | ||
attr_accessor :name | ||
|
||
def speak | ||
"Hello I am #{@name}" | ||
end | ||
end | ||
|
||
|
||
p = Person.new | ||
p.name = "Karthik" | ||
puts p.send(:speak) |