Skip to content

Commit

Permalink
RDoc for Range addition
Browse files Browse the repository at this point in the history
  • Loading branch information
BurdetteLamar committed Nov 21, 2023
2 parents 50ab4a0 + 46b33c9 commit 3313554
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 26 deletions.
38 changes: 32 additions & 6 deletions lib/json/add/bigdecimal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,50 @@
end

class BigDecimal
# Import a JSON Marshalled object.
#
# method used for JSON marshalling support.

# See #as_json.
def self.json_create(object)
BigDecimal._load object['b']
end

# Marshal the object to JSON.
# Methods <tt>BigDecimal#as_json</tt> and +BigDecimal.json_create+ may be used
# to serialize and deserialize a \BigDecimal object;
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
#
# \Method <tt>BigDecimal#as_json</tt> serializes +self+,
# returning a 2-element hash representing +self+:
#
# require 'json/add/bigdecimal'
# x = BigDecimal(2).as_json # => {"json_class"=>"BigDecimal", "b"=>"27:0.2e1"}
# y = BigDecimal(2.0, 4).as_json # => {"json_class"=>"BigDecimal", "b"=>"36:0.2e1"}
# z = BigDecimal(Complex(2, 0)).as_json # => {"json_class"=>"BigDecimal", "b"=>"27:0.2e1"}
#
# \Method +JSON.create+ deserializes such a hash, returning a \BigDecimal object:
#
# BigDecimal.json_create(x) # => 0.2e1
# BigDecimal.json_create(y) # => 0.2e1
# BigDecimal.json_create(z) # => 0.2e1
#
# method used for JSON marshalling support.
def as_json(*)
{
JSON.create_id => self.class.name,
'b' => _dump,
}
end

# return the JSON value
# Returns a JSON string representing +self+:
#
# require 'json/add/bigdecimal'
# puts BigDecimal(2).to_json
# puts BigDecimal(2.0, 4).to_json
# puts BigDecimal(Complex(2, 0)).to_json
#
# Output:
#
# {"json_class":"BigDecimal","b":"27:0.2e1"}
# {"json_class":"BigDecimal","b":"36:0.2e1"}
# {"json_class":"BigDecimal","b":"27:0.2e1"}
#
def to_json(*args)
as_json.to_json(*args)
end
Expand Down
45 changes: 25 additions & 20 deletions lib/json/add/range.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,28 @@

class Range

# Returns a new \Range object constructed from the given argument,
# which must be a hash containing the entry <tt>{'a' => _array_}</tt>,
# where +array+ contains values suitable as arguments for a call
# to Range.new[https://docs.ruby-lang.org/en/master/Range.html#method-c-new]:
#
# require 'json/add/range'
# Range.json_create({'a' => [1, 4]}) # => 1..4
# Range.json_create({'a' => [1, 4, true]}) # => 1...4
# Range.json_create({'a' => ['a', 'd']}) # => "a".."d"
#
# Note that other entries in the hash are ignored,
# so the hash can be one returned by #as_json.
#
# See #as_json.
def self.json_create(object)
new(*object['a'])
end

# Returns a 2-element hash representing +self+:
# Methods <tt>Range#as_json</tt> and +Range.json_create+ may be used
# to serialize and deserialize a \Range object;
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
#
# \Method <tt>Range#as_json</tt> serializes +self+,
# returning a 2-element hash representing +self+:
#
# require 'json/add/range'
# (1..4).as_json # => {"json_class"=>"Range", "a"=>[1, 4, false]}
# (1...4).as_json # => {"json_class"=>"Range", "a"=>[1, 4, true]}
# ('a'..'d').as_json # => {"json_class"=>"Range", "a"=>["a", "d", false]}
# x = (1..4).as_json # => {"json_class"=>"Range", "a"=>[1, 4, false]}
# y = (1...4).as_json # => {"json_class"=>"Range", "a"=>[1, 4, true]}
# z = ('a'..'d').as_json # => {"json_class"=>"Range", "a"=>["a", "d", false]}
#
# \Method +JSON.create+ deserializes such a hash, returning a \Range object:
#
# Range.json_create(x) # => 1..4
# Range.json_create(y) # => 1...4
# Range.json_create(z) # => "a".."d"
#
def as_json(*)
{
Expand All @@ -39,9 +38,15 @@ def as_json(*)
# Returns a JSON string representing +self+:
#
# require 'json/add/range'
# (1..4).to_json # => "{\"json_class\":\"Range\",\"a\":[1,4,false]}"
# (1...4).to_json # => "{\"json_class\":\"Range\",\"a\":[1,4,true]}"
# ('a'..'d').to_json # => "{\"json_class\":\"Range\",\"a\":[\"a\",\"d\",false]}"
# puts (1..4).to_json
# puts (1...4).to_json
# puts ('a'..'d').to_json
#
# Output:
#
# {"json_class":"Range","a":[1,4,false]}
# {"json_class":"Range","a":[1,4,true]}
# {"json_class":"Range","a":["a","d",false]}
#
def to_json(*args)
as_json.to_json(*args)
Expand Down

0 comments on commit 3313554

Please sign in to comment.