Returns the value as a string.
Rational(2).to_s #=> "2/1" Rational(-8, 6).to_s #=> "-4/3" Rational('1/2').to_s #=> "1/2"
Returns a string showing the options and string of self
:
r0 = /ab+c/ix s0 = r0.to_s # => "(?ix-m:ab+c)"
The returned string may be used as an argument to Regexp.new
, or as interpolated text for a Regexp interpolation:
r1 = Regexp.new(s0) # => /(?ix-m:ab+c)/ r2 = /#{s0}/ # => /(?ix-m:ab+c)/
Note that r1
and r2
are not equal to r0
because their original strings are different:
r0 == r1 # => false r0.source # => "ab+c" r1.source # => "(?ix-m:ab+c)"
Related: Regexp#inspect
.
Returns an array containing all elements in the set.
Set[1, 2].to_a #=> [1, 2] Set[1, 'c', :s].to_a #=> [1, "c", :s]
Returns a new string containing the set entries:
s = Set.new s.inspect # => "#<Set: {}>" s.add(1) s.inspect # => "#<Set: {1}>" s.add(2) s.inspect # => "#<Set: {1, 2}>"
Related: see Methods for Converting.
Returns the values in self
as an array:
Customer = Struct.new(:name, :address, :zip) joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) joe.to_a # => ["Joe Smith", "123 Maple, Anytown NC", 12345]
Related: members
.
Returns a hash containing the name and value for each member:
Customer = Struct.new(:name, :address, :zip) joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) h = joe.to_h h # => {:name=>"Joe Smith", :address=>"123 Maple, Anytown NC", :zip=>12345}
If a block is given, it is called with each name/value pair; the block should return a 2-element array whose elements will become a key/value pair in the returned hash:
h = joe.to_h{|name, value| [name.upcase, value.to_s.upcase]} h # => {:NAME=>"JOE SMITH", :ADDRESS=>"123 MAPLE, ANYTOWN NC", :ZIP=>"12345"}
Raises ArgumentError
if the block returns an inappropriate value.
Returns a string representation of self
:
Customer = Struct.new(:name, :address, :zip) # => Customer joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) joe.inspect # => "#<struct Customer name=\"Joe Smith\", address=\"123 Maple, Anytown NC\", zip=12345>"
Returns a string representation of self
(not including the leading colon):
:foo.to_s # => "foo"
Related: Symbol#inspect
, Symbol#name
.
Return the path as a String
.
to_path
is implemented so Pathname
objects are usable with File.open
, etc.
returns the socket address as packed struct sockaddr string.
Addrinfo.tcp("localhost", 80).to_sockaddr #=> "\x02\x00\x00P\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00"
With a block given, returns a new hash whose content is based on the block; the block is called with each entry’s key and value; the block should return a 2-element array containing the key and value to be included in the returned array:
h = {foo: 0, bar: 1, baz: 2} h.to_h {|key, value| [value, key] } # => {0 => :foo, 1 => :bar, 2 => :baz}
With no block given, returns self
if self
is an instance of Hash
; if self
is a subclass of Hash
, returns a new hash containing the content of self
.
Related: see Methods for Converting.
Returns all elements of self
as an array of 2-element arrays; each nested array contains a key-value pair from self
:
h = {foo: 0, bar: 1, baz: 2} h.to_a # => [[:foo, 0], [:bar, 1], [:baz, 2]]
Related: see Methods for Converting.
Returns a new string containing the hash entries:
h = {foo: 0, bar: 1, baz: 2} h.inspect # => "{foo: 0, bar: 1, baz: 2}"
Related: see Methods for Converting.
Returns the contents of ENV
as an Array
of 2-element Arrays, each of which is a name/value pair:
ENV.replace('foo' => '0', 'bar' => '1') ENV.to_a # => [["bar", "1"], ["foo", "0"]]
With no block, returns a Hash
containing all name/value pairs from ENV:
ENV.replace('foo' => '0', 'bar' => '1') ENV.to_h # => {"bar"=>"1", "foo"=>"0"}
With a block, returns a Hash
whose items are determined by the block. Each name/value pair in ENV
is yielded to the block. The block must return a 2-element Array
(name/value pair) that is added to the return Hash
as a key and value:
ENV.to_h { |name, value| [name.to_sym, value.to_i] } # => {bar: 1, foo: 0}
Raises an exception if the block does not return an Array:
ENV.to_h { |name, value| name } # Raises TypeError (wrong element type String (expected array))
Raises an exception if the block returns an Array
of the wrong size:
ENV.to_h { |name, value| [name] } # Raises ArgumentError (element has wrong array length (expected 2, was 1))
Returns “ARGF”.
Returns an integer representing the numeric file descriptor for the current file. Raises an ArgumentError
if there isn’t a current file.
ARGF.fileno #=> 3
Reads each file in ARGF
in its entirety, returning an Array
containing lines from the files. Lines are assumed to be separated by sep.
lines = ARGF.readlines lines[0] #=> "This is line one\n"
See IO.readlines
for a full description of all options.
Returns the integer representation of the ipaddr.
Returns a string containing the IP address representation.
Returns option summary list.
Returns Hash
representation of the data object.
Measure = Data.define(:amount, :unit) distance = Measure[10, 'km'] distance.to_h #=> {:amount=>10, :unit=>"km"}
Like Enumerable#to_h
, if the block is provided, it is expected to produce key-value pairs to construct a hash:
distance.to_h { |name, val| [name.to_s, val.to_s] } #=> {"amount"=>"10", "unit"=>"km"}
Note that there is a useful symmetry between to_h
and initialize:
distance2 = Measure.new(**distance.to_h) #=> #<data Measure amount=10, unit="km"> distance2 == distance #=> true