Skip to content

Instantly share code, notes, and snippets.

@saturnflyer
Created April 27, 2011 03:25
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 saturnflyer/943663 to your computer and use it in GitHub Desktop.
Save saturnflyer/943663 to your computer and use it in GitHub Desktop.
Method fallback
# Instead of writing ternary operators with respond_to?:
# obj.respond_to?(:method_one) ? obj.method_one : obj.method_two
# I want something simpler
class Object
def fallback(*args)
method_to_try = args.find{|meth| self.respond_to?(meth) && meth.to_s != __method__.to_s }
self.send(method_to_try)
end
alias trial fallback
alias trip fallback
end
obj = Object.new
# I'm not sure what method name I like here
puts obj.fallback(:hello, 'fallback', 'name', :to_s, 'inspect') # returns the output of :to_s
puts obj.trial(:hello, 'fallback', 'name', :to_s, 'inspect') # returns the output of :to_s
puts obj.trip(:hello, 'fallback', 'name', :to_s, 'inspect') # returns the output of :to_s
@rwilcox
Copy link

rwilcox commented Apr 27, 2011

Obj.this_or_that ?
Obj.do_this_but_at_the_end_of_the_day_do_that ?
Obj.send_with_default ?

IMHO you should also take args to pass in:

obj.send_default(:primary_method => [arg1, arg2], :fallback_method => [arg1, arg2])

@saturnflyer
Copy link
Author

this_or_that reads like it would only work with 2 options.
Passing in arguments sounds interesting, but are you likely to send different arguments per method?

Perhaps optional params could be passed in like this
obj.fallback(:method1, :method2, :method3, :arguments => [arg1,arg2])

I think it's far more likely that you'd do that than something like this, but this is still doable

obj.fallback(:method1, :method2, :method3, :arguments => {:method1 => [:arg1, :arg2], :method2 => [:arg3, arg4]})
# method3 would receive no arguments

@erithmetic
Copy link

We at Brighter Planet developed a gem, falls_back_on, that uses a somewhat different approach. Not sure if it's similar to what you're looking for, but thought I'd bring it up. Instead of saying "try this method, or use this one instead" we say, "use this object, or instead use a fallback object with some default values"

Example:

class Car
attr_accessor :mpg, :speed, :capacity

falls_back_on :mpg => 30,
:speed => lambda { self.current_highway.max_speed },
:capacity => 4
end

Then, we'd do something like:

if(car = Car.find(13))

use the found car

else
car = Car.fallback
end

Another example is here: https://github.com/dkastner/falls_back_on/blob/master/test/test_falls_back_on.rb

@erithmetic
Copy link

Sorry for the duplicates! I can't seem to edit or delete my comments.

@saturnflyer
Copy link
Author

Thanks, Derek.

My use case is in the scenario where you don't know the object that you'll have:

"This is the #{obj.fallback(:title, :name, :label, :to_s)}"

Where obj could be a User, Car, Document or Whatever. A standard method would, of course, be better, but in the case where that's not available a fallback can fit the bill.

@bokmann
Copy link

bokmann commented Apr 27, 2011

Of the three choices I like fallback more. I don't like the syntax of separating the arguments away from the method call though - I think a brave choice that would handle the 80% circumstance would be to assume these are getters that take no params, or that all methods take exactly the same params. If I found myself using this for ~3 methods that took different lists of params, I think the conventional code would win out for clarity.

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