Wednesday, July 21, 2010

Relatively unknown Object#instance_exec

Do u happen to know Object#instance_eval's brother, Object#instance_exec ?? Here's a uninteresting way of coding wo Object#instance_exec:
1
2
3
4
5
6
7
8
9
10
11
12
Someone = Struct.new(:name, :password)

greet = lambda do |someone|
puts "Hey %s, no worries, i won't tell anyone ur password is '%s' !!" % [
someone.name, someone.password
]
end

greet[Someone.new('Peter', 'aabbcc')]
# >> Hey Peter, no worries, i won't tell anyone ur password is 'aabbcc' !!
greet[Someone.new('Jane', 'bbccdd')]
# >> Hey Jane, no worries, i won't tell anyone ur password is 'bbccdd' !!

And here's a more interesting way to do it, w Object#instance_exec:
1
2
3
4
5
6
7
8
9
10
11
12
Someone = Struct.new(:name, :password)

greet = lambda do
puts "Hey %s, no worries, i won't tell anyone ur password is '%s' !!" % [
name, password
]
end

Someone.new('Peter', 'aabbcc').instance_exec(&greet)
# >> Hey Peter, no worries, i won't tell anyone ur password is 'aabbcc' !!
Someone.new('Jane', 'bbccdd').instance_exec(&greet)
# >> Hey Jane, no worries, i won't tell anyone ur password is 'bbccdd' !!

Yet another way to do it can be:
1
2
3
4
5
6
7
8
9
10
Someone = Struct.new(:name, :password)
greet = lambda{|msg| puts msg % [name, password] }

Someone.new('Peter', 'aabbcc').
instance_exec("Hey %s, no worries, i won't tell anyone ur password is '%s' !!", &greet)
# >> Hey Peter, no worries, i won't tell anyone ur password is 'aabbcc' !!

Someone.new('Jane', 'bbccdd').
instance_exec("Hey %s, i'm glad that u entrust me with ur password '%s' !!", &greet)
# >> Hey Jane, i'm glad that u entrust me with ur password 'bbccdd' !!

Enjoy !!

No comments:

Post a Comment

Labels