Skip to content

Instantly share code, notes, and snippets.

@remigijusj
Created October 11, 2013 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save remigijusj/6937669 to your computer and use it in GitHub Desktop.
Save remigijusj/6937669 to your computer and use it in GitHub Desktop.
method_missing and splat arguments
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