Sha256: f7ee9bc7c877bca1cde600117baf446c164a3d1472f57231e4158ab1a8812db6

Contents?: true

Size: 1.33 KB

Versions: 75

Compression:

Stored size: 1.33 KB

Contents

#frozen_string_literal: false
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
  require 'json'
end

class Range

  # Returns a new \Range object constructed from <tt>object['a']</tt>,
  # which must be an array of values suitable for a call to Range.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"
  #
  def self.json_create(object)
    new(*object['a'])
  end

  # Returns 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]}
  #
  def as_json(*)
    {
      JSON.create_id  => self.class.name,
      'a'             => [ first, last, exclude_end? ]
    }
  end

  # 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]}"
  #
  def to_json(*args)
    as_json.to_json(*args)
  end
end

Version data entries

75 entries across 75 versions & 3 rubygems

Version Path
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/json-2.7.0/lib/json/add/range.rb
harbr-0.2.10 vendor/bundle/ruby/3.2.0/gems/json-2.7.0/lib/json/add/range.rb
harbr-0.2.9 vendor/bundle/ruby/3.2.0/gems/json-2.7.0/lib/json/add/range.rb
harbr-0.2.8 vendor/bundle/ruby/3.2.0/gems/json-2.7.0/lib/json/add/range.rb
harbr-0.2.7 vendor/bundle/ruby/3.2.0/gems/json-2.7.0/lib/json/add/range.rb
harbr-0.2.6 vendor/bundle/ruby/3.2.0/gems/json-2.7.0/lib/json/add/range.rb
harbr-0.2.5 vendor/bundle/ruby/3.2.0/gems/json-2.7.0/lib/json/add/range.rb
harbr-0.2.4 vendor/bundle/ruby/3.2.0/gems/json-2.7.0/lib/json/add/range.rb
harbr-0.2.3 vendor/bundle/ruby/3.2.0/gems/json-2.7.0/lib/json/add/range.rb
harbr-0.2.2 vendor/bundle/ruby/3.2.0/gems/json-2.7.0/lib/json/add/range.rb
harbr-0.2.1 vendor/bundle/ruby/3.2.0/gems/json-2.7.0/lib/json/add/range.rb
harbr-0.2.0 vendor/bundle/ruby/3.2.0/gems/json-2.7.0/lib/json/add/range.rb
harbr-0.1.99 vendor/bundle/ruby/3.2.0/gems/json-2.7.0/lib/json/add/range.rb
harbr-0.1.98 vendor/bundle/ruby/3.2.0/gems/json-2.7.0/lib/json/add/range.rb
harbr-0.1.97 vendor/bundle/ruby/3.2.0/gems/json-2.7.0/lib/json/add/range.rb
harbr-0.1.96 vendor/bundle/ruby/3.2.0/gems/json-2.7.0/lib/json/add/range.rb
harbr-0.1.95 vendor/bundle/ruby/3.2.0/gems/json-2.7.0/lib/json/add/range.rb
harbr-0.1.94 vendor/bundle/ruby/3.2.0/gems/json-2.7.0/lib/json/add/range.rb
harbr-0.1.93 vendor/bundle/ruby/3.2.0/gems/json-2.7.0/lib/json/add/range.rb
harbr-0.1.91 vendor/bundle/ruby/3.2.0/gems/json-2.7.0/lib/json/add/range.rb