Skip to content

Commit

Permalink
metaprogramming started
Browse files Browse the repository at this point in the history
  • Loading branch information
Karthikeyan A K committed Oct 29, 2013
1 parent 67c612a commit d35cf8e
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 2 deletions.
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions define_method.rb
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
3 changes: 2 additions & 1 deletion function_many_arguments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ def print_line char = '_' length = 20
end

print_line
print_line 40
print_line '*'
print_line '&', 40
2 changes: 1 addition & 1 deletion inheritance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def side_length
end

def side_length=(length)
@width = @height = length
@width = @length = length
end
end

Expand Down
15 changes: 15 additions & 0 deletions method_missing.rb
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

18 changes: 18 additions & 0 deletions method_missing_in_action.rb
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"

12 changes: 12 additions & 0 deletions send.rb
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)

0 comments on commit d35cf8e

Please sign in to comment.