How define a method which takes an optional argument w/ a default value, but you have to distinguish whether the default arg or no arg was passed in?
class Foo
def bar(a = "whatevs")
# Was "whatevs" passed in, or no arg?
end
end
Two very different examples that distinguish between nil
and no arg:
class Foo
def bar(a = (a_not_specified = true; nil))
# [a_not_specified, a].detect &:present?
puts "a is #{a}."
puts "default value applied" if a_not_specified
end
end
vs.
class Foo
NOT_PROVIDED = Object.new
def bar(a = NOT_PROVIDED)
if a == NOT_PROVIDED
puts "not provided"
a = nil
end
end
private_constant :NOT_PROVIDED
end