Class: MyValue
- Inherits:
-
Object
- Object
- MyValue
- Defined in:
- lib/test_coerce.rb
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #*(other) ⇒ Object
- #**(other) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #-@ ⇒ Object
- #/(other) ⇒ Object
- #coerce(other) ⇒ Object
-
#initialize(value) ⇒ MyValue
constructor
A new instance of MyValue.
- #to_s ⇒ Object
Constructor Details
#initialize(value) ⇒ MyValue
Returns a new instance of MyValue.
4 5 6 |
# File 'lib/test_coerce.rb', line 4 def initialize(value) @value = value end |
Instance Attribute Details
#value ⇒ Object (readonly)
Returns the value of attribute value.
8 9 10 |
# File 'lib/test_coerce.rb', line 8 def value @value end |
Instance Method Details
#*(other) ⇒ Object
17 18 19 20 |
# File 'lib/test_coerce.rb', line 17 def *(other) other = to_v(other) out = MyValue.new(self.value * other.value) end |
#**(other) ⇒ Object
22 23 24 |
# File 'lib/test_coerce.rb', line 22 def **(other) out = MyValue.new(self.value ** other) end |
#+(other) ⇒ Object
12 13 14 15 |
# File 'lib/test_coerce.rb', line 12 def +(other) other = to_v(other) out = MyValue.new(self.value + other.value) end |
#-(other) ⇒ Object
30 31 32 |
# File 'lib/test_coerce.rb', line 30 def -(other) self + (-other) end |
#-@ ⇒ Object
26 27 28 |
# File 'lib/test_coerce.rb', line 26 def -@ self * -1 end |
#/(other) ⇒ Object
34 35 36 |
# File 'lib/test_coerce.rb', line 34 def /(other) self * (other ** -1) end |
#coerce(other) ⇒ Object
38 39 40 41 |
# File 'lib/test_coerce.rb', line 38 def coerce(other) other = to_v(other) [other, self.value] end |
#to_s ⇒ Object
43 44 45 |
# File 'lib/test_coerce.rb', line 43 def to_s value.to_s end |