Skip to content

Instantly share code, notes, and snippets.

@denysonique
Created June 27, 2011 07:51
Show Gist options
  • Save denysonique/1048460 to your computer and use it in GitHub Desktop.
Save denysonique/1048460 to your computer and use it in GitHub Desktop.
begin
variable = x.some_method.hax
rescue
variable = x.some_method.foo
rescue
variable = x.some_method.bar
end
#I would like to acheive something similar to:
variable = x.some_method.hax
variable ||= x.some_method.foo
variable ||= x.some_method.bar
@ghazel
Copy link

ghazel commented Jun 27, 2011

variable = x.try(:some_method).try(:hax)
variable ||= x.try(:some_method).try(:foo)
variable ||= x.try(:some_method).try(:bar)

or:

variable = x.some_method.hax rescue nil
variable ||= x.some_method.foo rescue nil
variable ||= x.some_method.bar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment