> When I have this pattern
>
> sign_me(@user, :event => :authentication, :subdomain => subdomain)
>
> how can I write it to avoid sending the :subdomain option if
subdomain.nil?
> ( if possible ..)
>
> sign_me(@user, :event => :authentication #?, :subdomain =>
subdomain
> unless subdomain.nil? )
You could try
sign_me(@user, :event => :authentication, (:subdomain =>
subdomain unless subdomain.nil?) )
I was surprised that you answered this Colin so I tried it out but I got a
syntax error
app.root_path(foo: 'me', (bar: 'blah' if false))
SyntaxError: (irb):17: syntax error, unexpected tLABEL
app.root_path(foo: 'me', (bar: 'blah' if false))
^
(irb):17: syntax error, unexpected ')', expecting tASSOC
from
/home/jim/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.2/lib/rails/commands/console.rb:47:in
`start'
from
/home/jim/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.2/lib/rails/commands/console.rb:8:in
`start'
from
/home/jim/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.2/lib/rails/commands.rb:41:in
`<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
> When I have this pattern
>
> sign_me(@user, :event => :authentication, :subdomain =>
> subdomain)
>
> how can I write it to avoid sending the :subdomain option if
> subdomain.nil?
> ( if possible ..)
>
> sign_me(@user, :event => :authentication #?, :subdomain =>
> subdomain
> unless subdomain.nil? )
You could try
sign_me(@user, :event => :authentication, (:subdomain =>
subdomain unless subdomain.nil?) )
I was surprised that you answered this Colin so I tried it out but I got a
syntax error
app.root_path(foo: 'me', (bar: 'blah' if false))
SyntaxError: (irb):17: syntax error, unexpected tLABEL
app.root_path(foo: 'me', (bar: 'blah' if false))
^
(irb):17: syntax error, unexpected ')', expecting tASSOC
from
/home/jim/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.2/lib/rails/commands/console.rb:47:in
`start'
from
/home/jim/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.2/lib/rails/commands/console.rb:8:in
`start'
from
/home/jim/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.2/lib/rails/commands.rb:41:in
`<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
I am not surprised that did not work, my answer was not intended to
convey any suggestion that it would work, just something that would be
interesting to try.
because if subdomain is nil, sign_me should correctly handle it.
I mean, in an hypothetical sign_in method I'd write:
def sign_me(user, options)
if options[:subdomain]
# do_something
end
end
if subdomain is nil the method handles it correctly.
but with this code:
def sign_me(user, options)
if options.has_key? :subdomain
# do_something
end
end
this goes wrong ("options.has_key? :subdomain" is true)! So I'd say the first implementation seems more correct, do you agree with me or I' missing something important about checking hash parameters?