Class: Rational
- Inherits:
-
Object
- Object
- Rational
- Defined in:
- lib/json/add/rational.rb
Class Method Summary collapse
-
.json_create(object) ⇒ Object
Deserializes JSON string by converting numerator value
n
, denominator valued
, to a Rational object.
Instance Method Summary collapse
-
#as_json ⇒ Object
Returns a hash, that will be turned into a JSON object and represent this object.
-
#to_json(*args) ⇒ Object
Stores class name (Rational) along with numerator value
n
and denominator valued
as JSON string.
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_json ⇒ Object
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_json(*args) ⇒ Object
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(*args) as_json.to_json(*args) end |