Guest User

Untitled

a guest
Jun 20th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.39 KB | None | 0 0
  1. class Foo
  2. class << self
  3. def make_this_method name, &block
  4. define_method(name, &block)
  5. end
  6. end
  7. end
  8.  
  9. Foo.make_this_method(:bar) do |*args|
  10. case args.size
  11. when 0 ; args = ['fizz']
  12. when 1 ; # nothing
  13. else ; raise ArgumentError.new("no") if args.size > 1
  14. end
  15. arg1, = *args
  16. puts "bar! #{arg1}"
  17. end
  18.  
  19. Foo.new.bar('baz') # outputs "bar! baz"
  20. Foo.new.bar # outputs "bar! fizz"
Add Comment
Please, Sign In to add comment