Results for: "to_proc"

Returns the Ruby source filename and line number containing this proc or nil if this proc was not defined in Ruby (i.e. native).

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"

Marks the proc as passing keywords through a normal argument splat. This should only be called on procs that accept an argument splat (*args) but not explicit keywords or a keyword splat. It marks the proc such that if the proc is called with keyword arguments, the final hash argument is marked with a special flag such that if it is the final element of a normal argument splat to another method call, and that method call does not include explicit keywords or a keyword splat, the final element is interpreted as keywords. In other words, keywords will be passed through the proc to other methods.

This should only be used for procs that delegate keywords to another method, and only for backwards compatibility with Ruby versions before 2.7.

This method will probably be removed at some point, as it exists only for backwards compatibility. As it does not exist in Ruby versions before 2.7, check that the proc responds to this method before calling it. Also, be aware that if this method is removed, the behavior of the proc will change so that it does not pass through keywords.

module Mod
  foo = ->(meth, *args, &block) do
    send(:"do_#{meth}", *args, &block)
  end
  foo.ruby2_keywords if foo.respond_to?(:ruby2_keywords)
end

Args

oth

URI or String

Description

Calculates relative path to oth from self.

Usage

require 'uri'

uri = URI.parse('http://my.example.com')
uri.route_to('http://my.example.com/main.rbx?page=1')
#=> #<URI::Generic /main.rbx?page=1>

When self is an instance of Array, returns self.

Otherwise, returns a new array containing the elements of self:

class MyArray < Array; end
my_a = MyArray.new(['foo', 'bar', 'two'])
a = my_a.to_a
a # => ["foo", "bar", "two"]
a.class # => Array # Not MyArray.

Related: see Methods for Converting.

Returns a new hash formed from self.

With no block given, each element of self must be a 2-element sub-array; forms each sub-array into a key-value pair in the new hash:

a = [['foo', 'zero'], ['bar', 'one'], ['baz', 'two']]
a.to_h # => {"foo"=>"zero", "bar"=>"one", "baz"=>"two"}
[].to_h # => {}

With a block given, the block must return a 2-element array; calls the block with each element of self; forms each returned array into a key-value pair in the returned hash:

a = ['foo', :bar, 1, [2, 3], {baz: 4}]
a.to_h {|element| [element, element.class] }
# => {"foo"=>String, :bar=>Symbol, 1=>Integer, [2, 3]=>Array, {:baz=>4}=>Hash}

Related: see Methods for Converting.

Returns the new string formed by calling method #inspect on each array element:

a = [:foo, 'bar', 2]
a.inspect # => "[:foo, \"bar\", 2]"

Related: see Methods for Converting.

Returns a string containing the place-value representation of self in radix base (in 2..36).

12345.to_s               # => "12345"
12345.to_s(2)            # => "11000000111001"
12345.to_s(8)            # => "30071"
12345.to_s(10)           # => "12345"
12345.to_s(16)           # => "3039"
12345.to_s(36)           # => "9ix"
78546939656932.to_s(36)  # => "rubyrules"

Raises an exception if base is out of range.

Converts self to a Float:

1.to_f  # => 1.0
-1.to_f # => -1.0

If the value of self does not fit in a Float, the result is infinity:

(10**400).to_f  # => Infinity
(-10**400).to_f # => -Infinity

Returns self (which is already an Integer).

Returns the value as a rational.

1.to_r        #=> (1/1)
(1<<64).to_r  #=> (18446744073709551616/1)

Returns a string representation of self:

Complex.rect(2).to_s                      # => "2+0i"
Complex.rect(-8, 6).to_s                  # => "-8+6i"
Complex.rect(0, Rational(1, 2)).to_s      # => "0+1/2i"
Complex.rect(0, Float::INFINITY).to_s     # => "0+Infinity*i"
Complex.rect(Float::NAN, Float::NAN).to_s # => "NaN+NaN*i"

Returns the value of self.real as an Integer, if possible:

Complex.rect(1, 0).to_i              # => 1
Complex.rect(1, Rational(0, 1)).to_i # => 1

Raises RangeError if self.imag is not exactly zero (either Integer(0) or Rational(0, n)).

Returns the value of self.real as a Float, if possible:

Complex.rect(1, 0).to_f              # => 1.0
Complex.rect(1, Rational(0, 1)).to_f # => 1.0

Raises RangeError if self.imag is not exactly zero (either Integer(0) or Rational(0, n)).

Returns the value of self.real as a Rational, if possible:

Complex.rect(1, 0).to_r              # => (1/1)
Complex.rect(1, Rational(0, 1)).to_r # => (1/1)
Complex.rect(1, 0.0).to_r            # => (1/1)

Raises RangeError if self.imag is not exactly zero (either Integer(0) or Rational(0, n)) and self.imag.to_r is not exactly zero.

Related: Complex#rationalize.

Returns self.

Returns zero as a Complex:

nil.to_c # => (0+0i)

Always returns zero.

nil.to_i   #=> 0

Always returns zero.

nil.to_f   #=> 0.0

Returns an empty String:

nil.to_s # => ""

Returns an empty Array.

nil.to_a # => []

Returns an empty Hash.

nil.to_h   #=> {}

Returns zero as a Rational:

nil.to_r # => (0/1)

Returns self as a Complex object.

Returns self interpreted as a Complex object; leading whitespace and trailing garbage are ignored:

'9'.to_c                 # => (9+0i)
'2.5'.to_c               # => (2.5+0i)
'2.5/1'.to_c             # => ((5/2)+0i)
'-3/2'.to_c              # => ((-3/2)+0i)
'-i'.to_c                # => (0-1i)
'45i'.to_c               # => (0+45i)
'3-4i'.to_c              # => (3-4i)
'-4e2-4e-2i'.to_c        # => (-400.0-0.04i)
'-0.0-0.0i'.to_c         # => (-0.0-0.0i)
'1/2+3/4i'.to_c          # => ((1/2)+(3/4)*i)
'1.0@0'.to_c             # => (1+0.0i)
"1.0@#{Math::PI/2}".to_c # => (0.0+1i)
"1.0@#{Math::PI}".to_c   # => (-1+0.0i)

Returns Complex zero if the string cannot be converted:

'ruby'.to_c        # => (0+0i)

See Kernel#Complex.

Search took: 3ms  ·  Total Results: 1563