Last active
November 11, 2017 04:36
-
-
Save max-power/3b4b4bb345ee7157d696a5f3ab1aba5c to your computer and use it in GitHub Desktop.
Number with Unit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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