Created
September 18, 2018 19:04
-
-
Save jc00ke/968f32d021fe4da07401acec36b93585 to your computer and use it in GitHub Desktop.
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
def bar | |
:bar | |
end | |
def foo(a, b=bar) | |
puts [a, b].inspect | |
end | |
foo("hi") | |
# => ["hi", :bar] | |
foo("hi", "this is my bar") | |
# => ["hi", "this is my bar"] | |
def foo(a, b=bar()) # do the parens help? | |
puts [a, b].inspect | |
end | |
foo("hi") | |
# => ["hi", :bar] |
Author
jc00ke
commented
Sep 18, 2018
What I'm really asking is:
Which is "better" (not necessarily more idiomatic)
def bar
:bar
end
def foo(a, b=nil)
b ||= bar
# ...
end
def foo(a, b=nil)
if b.nil?
b = bar
end
# ...
end
def foo(a, b=bar) # or kwargs, doesn't really matter
# no checking necessary
# ...
end
def foo(a, b=bar()) # or kwargs, doesn't really matter
# no checking necessary
# ...
end
Turns out, the 2nd arg is never passed in code or tests anyway, so I'm going with
def foo(a)
b = bar
# ...
end
bar
is actually a ActiveRecord::FinderMethods.find_by!
and we can't continue of the model doesn't exist anyway.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment