Class: Rational

Inherits:
Object
  • Object
show all
Defined in:
lib/json/add/rational.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.json_create(object) ⇒ Object

Deserializes JSON string by converting numerator value n, denominator value d, to a Rational object.



9
10
11
# File 'lib/json/add/rational.rb', line 9

def self.json_create(object)
  Rational(object['n'], object['d'])
end

Instance Method Details

#as_jsonObject

Returns a hash, that will be turned into a JSON object and represent this object.



15
16
17
18
19
20
21
# File 'lib/json/add/rational.rb', line 15

def as_json(*)
  {
    JSON.create_id => self.class.name,
    'n'            => numerator,
    'd'            => denominator,
  }
end

#to_jsonObject

Stores class name (Rational) along with numerator value n and denominator value d as JSON string



24
25
26
# File 'lib/json/add/rational.rb', line 24

def to_json(*)
  as_json.to_json
end