Test Your Understanding!
-
Reimplement Symbol#to_proc. Now that you’ve seen how Symbol#to_proc is implemented, you should have a go at it yourself.
-
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 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. ...
Get Mastering Ruby Closures now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.