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}
Returns a new Time
object with the same value as self
; if self
is a Julian date, derives its Gregorian date for conversion to the Time object:
Date.new(2001, 2, 3).to_time # => 2001-02-03 00:00:00 -0600 Date.new(2001, 2, 3, Date::JULIAN).to_time # => 2001-02-16 00:00:00 -0600
Returns a DateTime
whose value is the same as self
:
Date.new(2001, 2, 3).to_datetime # => #<DateTime: 2001-02-03T00:00:00+00:00>
See as_json
.
Methods Date#as_json
and Date.json_create
may be used to serialize and deserialize a Date object; see Marshal
.
Method Date#as_json
serializes self
, returning a 2-element hash representing self
:
require 'json/add/date' x = Date.today.as_json # => {"json_class"=>"Date", "y"=>2023, "m"=>11, "d"=>21, "sg"=>2299161.0}
Method JSON.create
deserializes such a hash, returning a Date object:
Date.json_create(x) # => #<Date: 2023-11-21 ((2460270j,0s,0n),+0s,2299161j)>
Returns a JSON
string representing self
:
require 'json/add/date' puts Date.today.to_json
Output:
{"json_class":"Date","y":2023,"m":11,"d":21,"sg":2299161.0}
Returns a Time
object which denotes self.
Returns self.
Methods DateTime#as_json
and DateTime.json_create
may be used to serialize and deserialize a DateTime object; see Marshal
.
Method DateTime#as_json
serializes self
, returning a 2-element hash representing self
:
require 'json/add/datetime' x = DateTime.now.as_json # => {"json_class"=>"DateTime", "y"=>2023, "m"=>11, "d"=>21, "sg"=>2299161.0}
Method JSON.create
deserializes such a hash, returning a DateTime object:
DateTime.json_create(x) # BUG? Raises Date::Error "invalid date"
Returns a JSON
string representing self
:
require 'json/add/datetime' puts DateTime.now.to_json
Output:
{"json_class":"DateTime","y":2023,"m":11,"d":21,"sg":2299161.0}
Returns self.
Returns a DateTime
object which denotes self.
See as_json
.
Methods Time#as_json
and Time.json_create
may be used to serialize and deserialize a Time object; see Marshal
.
Method Time#as_json
serializes self
, returning a 2-element hash representing self
:
require 'json/add/time' x = Time.now.as_json # => {"json_class"=>"Time", "s"=>1700931656, "n"=>472846644}
Method JSON.create
deserializes such a hash, returning a Time object:
Time.json_create(x) # => 2023-11-25 11:00:56.472846644 -0600
Returns a JSON
string representing self
:
require 'json/add/time' puts Time.now.to_json
Output:
{"json_class":"Time","s":1700931678,"n":980650786}