Created
October 11, 2013 16:18
-
-
Save remigijusj/6937669 to your computer and use it in GitHub Desktop.
method_missing and splat arguments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A | |
def method_missing(_, value, *_) | |
puts "A: #{value.inspect} #{_.inspect}" | |
end | |
def existing1(_, value, *_) | |
puts "Ae1: #{value.inspect} #{_.inspect}" | |
end | |
def existing2(_, value, *) | |
puts "Ae2: #{value.inspect} #{_.inspect}" | |
end | |
end | |
class B | |
def method_missing(m, value, *_) | |
puts "B: #{value.inspect} (#{m})" | |
end | |
end | |
class C | |
def method_missing(_, value, *args) | |
puts "C: #{value.inspect} #{args.inspect}" | |
end | |
end | |
class D | |
def method_missing(_, value, *) | |
puts "D: #{value.inspect} #{_.inspect}" | |
end | |
end | |
a = A.new | |
b = B.new | |
c = C.new | |
d = D.new | |
a.one(2) # A: nil [] | |
a.two(2, :s) # A: nil [:s] | |
a.existing1(:x, 2, 42) # Ae1: nil [42] | |
a.existing2(:x, 2, 42) # Ae2: 2 :x | |
b.one(2) # B: 2 (one) | |
b.two(2, :s) # B: 2 (two) | |
c.one(2) # C: 2 [] | |
c.two(2, :s) # C: 2 [:s] | |
d.one(2) # D: 2 :one | |
d.two(2, :s) # D: 2 :two | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment