Returns a JSON
string representing self
:
require 'json/add/ostruct' puts OpenStruct.new('name' => 'Rowdy', :age => nil).to_json
Output:
{"json_class":"OpenStruct","t":{'name':'Rowdy',"age":null}}
Returns a JSON
string representing self
:
require 'json/add/range' 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]}
With a block given, passes each element of self
to the block in reverse order:
a = [] (1..4).reverse_each {|element| a.push(element) } # => 1..4 a # => [4, 3, 2, 1] a = [] (1...4).reverse_each {|element| a.push(element) } # => 1...4 a # => [3, 2, 1]
With no block given, returns an enumerator.
Returns a JSON
string representing self
:
require 'json/add/rational' puts Rational(2, 3).to_json
Output:
{"json_class":"Rational","n":2,"d":3}
Returns a JSON
string representing self
:
require 'json/add/regexp' puts /foo/.to_json
Output:
{"json_class":"Regexp","o":0,"s":"foo"}
With no argument, returns the value of $~
, which is the result of the most recent pattern match (see Regexp global variables):
/c(.)t/ =~ 'cat' # => 0 Regexp.last_match # => #<MatchData "cat" 1:"a"> /a/ =~ 'foo' # => nil Regexp.last_match # => nil
With non-negative integer argument n
, returns the _n_th field in the matchdata, if any, or nil if none:
/c(.)t/ =~ 'cat' # => 0 Regexp.last_match(0) # => "cat" Regexp.last_match(1) # => "a" Regexp.last_match(2) # => nil
With negative integer argument n
, counts backwards from the last field:
Regexp.last_match(-1) # => "a"
With string or symbol argument name
, returns the string value for the named capture, if any:
/(?<lhs>\w+)\s*=\s*(?<rhs>\w+)/ =~ 'var = val' Regexp.last_match # => #<MatchData "var = val" lhs:"var"rhs:"val"> Regexp.last_match(:lhs) # => "var" Regexp.last_match('rhs') # => "val" Regexp.last_match('foo') # Raises IndexError.
Returns a hash representing named captures of self
(see Named Captures):
Each key is the name of a named capture.
Each value is an array of integer indexes for that named capture.
Examples:
/(?<foo>.)(?<bar>.)/.named_captures # => {"foo"=>[1], "bar"=>[2]} /(?<foo>.)(?<foo>.)/.named_captures # => {"foo"=>[1, 2]} /(.)(.)/.named_captures # => {}
See as_json
.
Returns a JSON
string representing self
:
require 'json/add/set' puts Set.new(%w/foo bar baz/).to_json
Output:
{"json_class":"Set","a":["foo","bar","baz"]}
Returns self if no arguments are given. Otherwise, converts the set to another with klass.new(self, *args, &block)
.
In subclasses, returns klass.new(self, *args, &block)
unless overridden.
Returns a JSON
string representing self
:
require 'json/add/struct' Customer = Struct.new('Customer', :name, :address, :zip) puts Struct::Customer.new.to_json
Output:
{"json_class":"Struct","t":{'name':'Rowdy',"age":null}}
Returns true
if the class was initialized with keyword_init: true
. Otherwise returns nil
or false
.
Examples:
Foo = Struct.new(:a) Foo.keyword_init? # => nil Bar = Struct.new(:a, keyword_init: true) Bar.keyword_init? # => true Baz = Struct.new(:a, keyword_init: false) Baz.keyword_init? # => false
Returns a hash of the name/value pairs for the given member names.
Customer = Struct.new(:name, :address, :zip) joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) h = joe.deconstruct_keys([:zip, :address]) h # => {:zip=>12345, :address=>"123 Maple, Anytown NC"}
Returns all names and values if array_of_names
is nil
:
h = joe.deconstruct_keys(nil) h # => {:name=>"Joseph Smith, Jr.", :address=>"123 Maple, Anytown NC", :zip=>12345}
Returns a JSON
string representing self
:
require 'json/add/symbol' puts :foo.to_json
Output:
# {"json_class":"Symbol","s":"foo"}
Returns a Proc
object which calls the method with name of self
on the first parameter and passes the remaining parameters to the method.
proc = :to_s.to_proc # => #<Proc:0x000001afe0e48680(&:to_s) (lambda)> proc.call(1000) # => "1000" proc.call(1000, 16) # => "3e8" (1..3).collect(&:to_s) # => ["1", "2", "3"]
Equivalent to self.to_s.start_with?
; see String#start_with?
.
Returns true if this class can be used to create an instance from a serialised JSON
string. The class has to implement a class method json_create that expects a hash as first parameter. The hash should include the required data.
Return the path as a String
.
to_path
is implemented so Pathname
objects are usable with File.open
, etc.