Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 1. Reimplement Symbol#to_proc. Now that you’ve seen how Symbol#to_proc is implemented, you should have a go at it yourself.
- class Symbol
- def to_proc
- proc { |x, args=nil| x.send(self, *args) }
- end
- end
- # 2. You can use #to_proc instantiate classes. . Consider this behavior:
- class SpiceGirl
- def initialize(name, nick)
- @name = name
- @nick = nick
- end
- def inspect
- "#{@name} (#{@nick} Spice)"
- end
- end
- class Class
- def to_proc
- proc {|x| new(x[0], x[1])}
- end
- end
- spice_girls = [["Mel B", "Scary"], ["Mel C", "Sporty"], ["Emma B", "Baby"], ["Geri H", "Ginger",], ["Vic B", "Posh"]]
- p spice_girls.map(&SpiceGirl)
- # This returns:
- # [Mel B (Scary Spice), Mel C (Sporty Spice),
- # Emma B (Baby Spice), Geri H (Ginger Spice), Vic B (Posh Spice)]
- # This example demonstrates how to_proc can be used to initialize a class. Implement this.
Add Comment
Please, Sign In to add comment