Check if gem name
version version
is installed.
Displays an alert statement
. Asks a question
if given.
Check if YJIT is enabled.
Enable YJIT compilation.
If object
is an Array object, returns object
.
Otherwise if object
responds to :to_ary
, calls object.to_ary
and returns the result.
Returns nil
if object
does not respond to :to_ary
Raises an exception unless object.to_ary
returns an Array object.
Replaces the content of self
with the content of other_array
; returns self
:
a = [:foo, 'bar', 2] a.replace(['foo', :bar, 3]) # => ["foo", :bar, 3]
If object
is an Integer object, returns object
.
Integer.try_convert(1) # => 1
Otherwise if object
responds to :to_int
, calls object.to_int
and returns the result.
Integer.try_convert(1.25) # => 1
Returns nil
if object
does not respond to :to_int
Integer.try_convert([]) # => nil
Raises an exception unless object.to_int
returns an Integer object.
Methods Complex#as_json
and Complex.json_create
may be used to serialize and deserialize a Complex object; see Marshal
.
Method Complex#as_json
serializes self
, returning a 2-element hash representing self
:
require 'json/add/complex' x = Complex(2).as_json # => {"json_class"=>"Complex", "r"=>2, "i"=>0} y = Complex(2.0, 4).as_json # => {"json_class"=>"Complex", "r"=>2.0, "i"=>4}
Method JSON.create
deserializes such a hash, returning a Complex object:
Complex.json_create(x) # => (2+0i) Complex.json_create(y) # => (2.0+4i)
Returns a JSON
string representing self
:
require 'json/add/complex' puts Complex(2).to_json puts Complex(2.0, 4).to_json
Output:
{"json_class":"Complex","r":2,"i":0} {"json_class":"Complex","r":2.0,"i":4}
If object
is a String object, returns object
.
Otherwise if object
responds to :to_str
, calls object.to_str
and returns the result.
Returns nil
if object
does not respond to :to_str
.
Raises an exception unless object.to_str
returns a String object.
Replaces the contents of self
with the contents of other_string
:
s = 'foo' # => "foo" s.replace('bar') # => "bar"
Returns true
if self
contains only ASCII characters, false
otherwise:
'abc'.ascii_only? # => true "abc\u{6666}".ascii_only? # => false
Iterates the given block for each element with an arbitrary object, obj
, and returns obj
If no block is given, returns a new Enumerator
.
to_three = Enumerator.new do |y| 3.times do |x| y << x end end to_three_with_string = to_three.with_object("foo") to_three_with_string.each do |x,string| puts "#{string}: #{x}" end # => foo: 0 # => foo: 1 # => foo: 2
Returns an integer identifier for obj
.
The same number will be returned on all calls to object_id
for a given object, and no two active objects will share an id.
Note: that some objects of builtin classes are reused for optimization. This is the case for immediate values and frozen string literals.
BasicObject
implements __id__
, Kernel
implements object_id
.
Immediate values are not passed by reference but are passed by value: nil
, true
, false
, Fixnums, Symbols, and some Floats.
Object.new.object_id == Object.new.object_id # => false (21 * 2).object_id == (21 * 2).object_id # => true "hello".object_id == "hello".object_id # => false "hi".freeze.object_id == "hi".freeze.object_id # => true
Returns true
if obj is an instance of the given class. See also Object#kind_of?
.
class A; end class B < A; end class C < B; end b = B.new b.instance_of? A #=> false b.instance_of? B #=> true b.instance_of? C #=> false
Returns true
if obj responds to the given method. Private and protected methods are included in the search only if the optional second parameter evaluates to true
.
If the method is not implemented, as Process.fork
on Windows, File.lchmod
on GNU/Linux, etc., false is returned.
If the method is not defined, respond_to_missing?
method is called and the result is returned.
When the method name parameter is given as a string, the string is converted to a symbol.
Methods Exception#as_json
and Exception.json_create
may be used to serialize and deserialize a Exception object; see Marshal
.
Method Exception#as_json
serializes self
, returning a 2-element hash representing self
:
require 'json/add/exception' x = Exception.new('Foo').as_json # => {"json_class"=>"Exception", "m"=>"Foo", "b"=>nil}
Method JSON.create
deserializes such a hash, returning a Exception object:
Exception.json_create(x) # => #<Exception: Foo>
Returns a JSON
string representing self
:
require 'json/add/exception' puts Exception.new('Foo').to_json
Output:
{"json_class":"Exception","m":"Foo","b":null}
Methods BigDecimal#as_json
and BigDecimal.json_create
may be used to serialize and deserialize a BigDecimal object; see Marshal
.
Method BigDecimal#as_json
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
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"}