Skip to content

Instantly share code, notes, and snippets.

@max-power
Last active November 11, 2017 04:36
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 max-power/3b4b4bb345ee7157d696a5f3ab1aba5c to your computer and use it in GitHub Desktop.
Save max-power/3b4b4bb345ee7157d696a5f3ab1aba5c to your computer and use it in GitHub Desktop.
Number with Unit
require 'delegate'
class NumberWithUnit < SimpleDelegator
def initialize(number, unit)
super number
@unit = unit
end
def to_s
"#{super}#{@unit}"
end
alias_method :inspect, :to_s
end
# NumberWithUnit.new(100, :km).to_s # "100km"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment