Skip to content

Commit ec47749

Browse files
BurdetteLamarhsbt
authored andcommitted
Enhanced RDoc for Range extensions
1 parent aa2c032 commit ec47749

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

lib/json/add/range.rb

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,39 @@
55

66
class Range
77

8-
# Deserializes JSON string by constructing new Range object with arguments
9-
# <tt>a</tt> serialized by <tt>to_json</tt>.
8+
# Returns a new \Range object constructed from <tt>object['a']</tt>,
9+
# which must be an array of values suitable for a call to Range.new:
10+
#
11+
# require 'json/add/range'
12+
# Range.json_create({"a"=>[1, 4]}) # => 1..4
13+
# Range.json_create({"a"=>[1, 4, true]}) # => 1...4
14+
# Range.json_create({"a" => ['a', 'd']}) # => "a".."d"
15+
#
1016
def self.json_create(object)
1117
new(*object['a'])
1218
end
1319

14-
# Returns a hash, that will be turned into a JSON object and represent this
15-
# object.
20+
# Returns a 2-element hash representing +self+:
21+
#
22+
# require 'json/add/range'
23+
# (1..4).as_json # => {"json_class"=>"Range", "a"=>[1, 4, false]}
24+
# (1...4).as_json # => {"json_class"=>"Range", "a"=>[1, 4, true]}
25+
# ('a'..'d').as_json # => {"json_class"=>"Range", "a"=>["a", "d", false]}
26+
#
1627
def as_json(*)
1728
{
1829
JSON.create_id => self.class.name,
1930
'a' => [ first, last, exclude_end? ]
2031
}
2132
end
2233

23-
# Stores class name (Range) with JSON array of arguments <tt>a</tt> which
24-
# include <tt>first</tt> (integer), <tt>last</tt> (integer), and
25-
# <tt>exclude_end?</tt> (boolean) as JSON string.
34+
# Returns a JSON string representing +self+:
35+
#
36+
# require 'json/add/range'
37+
# (1..4).to_json # => "{\"json_class\":\"Range\",\"a\":[1,4,false]}"
38+
# (1...4).to_json # => "{\"json_class\":\"Range\",\"a\":[1,4,true]}"
39+
# ('a'..'d').to_json # => "{\"json_class\":\"Range\",\"a\":[\"a\",\"d\",false]}"
40+
#
2641
def to_json(*args)
2742
as_json.to_json(*args)
2843
end

0 commit comments

Comments
 (0)