Skip to content

Instantly share code, notes, and snippets.

@Igosuki
Last active August 29, 2015 14:06
Show Gist options
  • Save Igosuki/152e2aec3d37c73156bc to your computer and use it in GitHub Desktop.
Save Igosuki/152e2aec3d37c73156bc to your computer and use it in GitHub Desktop.
Pass-through struct to deserialize an API into YAML
class Duration < Struct.new(:value, :quantity)
def method_missing id, *args
raise "Illegal Duration" if !value.is_a?(Numeric) || ![:years, :days, :weeks, :months, :hours, :minutes, :seconds].include?(quantity.to_sym)
value.send(quantity.to_sym).send(id.id2name.to_sym)
end
end
@simcap
Copy link

simcap commented Sep 26, 2014

I do not get the use for it

Duration.new(4, :days).any_method(:my_id)  # => 4.days.my_id

... so what.

(Also args are ignored ? And I do not get id.id2name.to_sym since if we call id2name on id it means it is already a symbol so we do not call to_sym on it)

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