Class: MyValue

Inherits:
Object
  • Object
show all
Defined in:
lib/test_coerce.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#valueObject (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_sObject



43
44
45
# File 'lib/test_coerce.rb', line 43

def to_s
    value.to_s
end