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.


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

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.


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

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


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

def to_json(*)
  as_json.to_json
end