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.
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_json ⇒ Object
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_json(*args) ⇒ Object
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(*args) as_json.to_json(*args) end |