Guest User

Untitled

a guest
Apr 21st, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. # 1. Reimplement Symbol#to_proc. Now that you’ve seen how Symbol#to_proc is implemented, you should have a go at it yourself.
  2.  
  3. class Symbol
  4. def to_proc
  5. proc { |x, args=nil| x.send(self, *args) }
  6. end
  7. end
  8.  
  9. # 2. You can use #to_proc instantiate classes. . Consider this behavior:
  10.  
  11. class SpiceGirl
  12. def initialize(name, nick)
  13. @name = name
  14. @nick = nick
  15. end
  16.  
  17. def inspect
  18. "#{@name} (#{@nick} Spice)"
  19. end
  20.  
  21. end
  22.  
  23. class Class
  24. def to_proc
  25. proc {|x| new(x[0], x[1])}
  26. end
  27. end
  28.  
  29. spice_girls = [["Mel B", "Scary"], ["Mel C", "Sporty"], ["Emma B", "Baby"], ["Geri H", "Ginger",], ["Vic B", "Posh"]]
  30.  
  31. p spice_girls.map(&SpiceGirl)
  32.  
  33. # This returns:
  34.  
  35. # [Mel B (Scary Spice), Mel C (Sporty Spice),
  36. # Emma B (Baby Spice), Geri H (Ginger Spice), Vic B (Posh Spice)]
  37.  
  38. # This example demonstrates how to_proc can be used to initialize a class. Implement this.
Add Comment
Please, Sign In to add comment