When there is an invalid block with a keyword missing an end right before another end, it is unclear where which keyword is missing the end
Take this example:
class Dog # 1 def bark # 2 puts "woof" # 3 end # 4
However due to github.com/ruby/syntax_suggest/issues/32 the problem line will be identified as:
> class Dog # 1
Because lines 2, 3, and 4 are technically valid code and are expanded first, deemed valid, and hidden. We need to un-hide the matching end line 4. Also work backwards and if there’s a mis-matched keyword, show it too
Foo::Bar = 1 ^^^^^^^^^^^^
Foo::Foo, Bar::Bar = 1 ^^^^^^^^ ^^^^^^^^
Foo::Bar, = baz ^^^^^^^^
def foo(**bar); end
^^^^^
def foo(**); end
^^
Foo::Bar = 1 ^^^^^^^^^^^^
Foo::Foo, Bar::Bar = 1 ^^^^^^^^ ^^^^^^^^
Foo::Bar, = baz ^^^^^^^^
def foo(**bar); end
^^^^^
def foo(**); end
^^
def foo(bar:); end
^^^^
Returns a new Array
whose elements are the elements of self
at the given Integer
or Range
indexes
.
For each positive index
, returns the element at offset index
:
a = [:foo, 'bar', 2] a.values_at(0, 2) # => [:foo, 2] a.values_at(0..1) # => [:foo, "bar"]
The given indexes
may be in any order, and may repeat:
a = [:foo, 'bar', 2] a.values_at(2, 0, 1, 0, 2) # => [2, :foo, "bar", :foo, 2] a.values_at(1, 0..2) # => ["bar", :foo, "bar", 2]
Assigns nil
for an index
that is too large:
a = [:foo, 'bar', 2] a.values_at(0, 3, 1, 3) # => [:foo, nil, "bar", nil]
Returns a new empty Array
if no arguments given.
For each negative index
, counts backward from the end of the array:
a = [:foo, 'bar', 2] a.values_at(-1, -3) # => [2, :foo]
Assigns nil
for an index
that is too small:
a = [:foo, 'bar', 2] a.values_at(0, -5, 1, -6, 2) # => [:foo, nil, "bar", nil, 2]
The given indexes
may have a mixture of signs:
a = [:foo, 'bar', 2] a.values_at(0, -2, 1, -1) # => [:foo, "bar", "bar", 2]
Deletes an element from self
, per the given Integer
index
.
When index
is non-negative, deletes the element at offset index
:
a = [:foo, 'bar', 2] a.delete_at(1) # => "bar" a # => [:foo, 2]
If index is too large, returns nil
.
When index
is negative, counts backward from the end of the array:
a = [:foo, 'bar', 2] a.delete_at(-2) # => "bar" a # => [:foo, 2]
If index
is too small (far from zero), returns nil.
Returns the number of bits of the value of self
, which is the bit position of the highest-order bit that is different from the sign bit (where the least significant bit has bit position 1). If there is no such bit (zero or minus one), returns zero.
This method returns ceil(log2(self < 0 ? -self : self + 1))
>.
(-2**1000-1).bit_length # => 1001 (-2**1000).bit_length # => 1000 (-2**1000+1).bit_length # => 1000 (-2**12-1).bit_length # => 13 (-2**12).bit_length # => 12 (-2**12+1).bit_length # => 12 -0x101.bit_length # => 9 -0x100.bit_length # => 8 -0xff.bit_length # => 8 -2.bit_length # => 1 -1.bit_length # => 0 0.bit_length # => 0 1.bit_length # => 1 0xff.bit_length # => 8 0x100.bit_length # => 9 (2**12-1).bit_length # => 12 (2**12).bit_length # => 13 (2**12+1).bit_length # => 13 (2**1000-1).bit_length # => 1000 (2**1000).bit_length # => 1001 (2**1000+1).bit_length # => 1001
For Integer n, this method can be used to detect overflow in Array#pack
:
if n.bit_length < 32 [n].pack('l') # No overflow. else raise 'Overflow' end
Returns an array of the grapheme clusters in self
(see Unicode Grapheme Cluster Boundaries):
s = "\u0061\u0308-pqr-\u0062\u0308-xyz-\u0063\u0308" # => "ä-pqr-b̈-xyz-c̈" s.grapheme_clusters # => ["ä", "-", "p", "q", "r", "-", "b̈", "-", "x", "y", "z", "-", "c̈"]
Returns whether self
starts with any of the given string_or_regexp
.
Matches patterns against the beginning of self
. For each given string_or_regexp
, the pattern is:
string_or_regexp
itself, if it is a Regexp
.
Regexp.quote(string_or_regexp)
, if string_or_regexp
is a string.
Returns true
if any pattern matches the beginning, false
otherwise:
'hello'.start_with?('hell') # => true 'hello'.start_with?(/H/i) # => true 'hello'.start_with?('heaven', 'hell') # => true 'hello'.start_with?('heaven', 'paradise') # => false 'тест'.start_with?('т') # => true 'こんにちは'.start_with?('こ') # => true
Related: String#end_with?
.
Returns whether self
ends with any of the given strings
.
Returns true
if any given string matches the end, false
otherwise:
'hello'.end_with?('ello') #=> true 'hello'.end_with?('heaven', 'ello') #=> true 'hello'.end_with?('heaven', 'paradise') #=> false 'тест'.end_with?('т') # => true 'こんにちは'.end_with?('は') # => true
Related: String#start_with?
.
Returns the next-larger representable Float.
These examples show the internally stored values (64-bit hexadecimal) for each Float f
and for the corresponding f.next_float
:
f = 0.0 # 0x0000000000000000 f.next_float # 0x0000000000000001 f = 0.01 # 0x3f847ae147ae147b f.next_float # 0x3f847ae147ae147c
In the remaining examples here, the output is shown in the usual way (result to_s
):
0.01.next_float # => 0.010000000000000002 1.0.next_float # => 1.0000000000000002 100.0.next_float # => 100.00000000000001 f = 0.01 (0..3).each_with_index {|i| printf "%2d %-20a %s\n", i, f, f.to_s; f = f.next_float }
Output:
0 0x1.47ae147ae147bp-7 0.01 1 0x1.47ae147ae147cp-7 0.010000000000000002 2 0x1.47ae147ae147dp-7 0.010000000000000004 3 0x1.47ae147ae147ep-7 0.010000000000000005 f = 0.0; 100.times { f += 0.1 } f # => 9.99999999999998 # should be 10.0 in the ideal world. 10-f # => 1.9539925233402755e-14 # the floating point error. 10.0.next_float-10 # => 1.7763568394002505e-15 # 1 ulp (unit in the last place). (10-f)/(10.0.next_float-10) # => 11.0 # the error is 11 ulp. (10-f)/(10*Float::EPSILON) # => 8.8 # approximation of the above. "%a" % 10 # => "0x1.4p+3" "%a" % f # => "0x1.3fffffffffff5p+3" # the last hex digit is 5. 16 - 5 = 11 ulp.
Related: Float#prev_float
Returns the next-smaller representable Float.
These examples show the internally stored values (64-bit hexadecimal) for each Float f
and for the corresponding f.pev_float
:
f = 5e-324 # 0x0000000000000001 f.prev_float # 0x0000000000000000 f = 0.01 # 0x3f847ae147ae147b f.prev_float # 0x3f847ae147ae147a
In the remaining examples here, the output is shown in the usual way (result to_s
):
0.01.prev_float # => 0.009999999999999998 1.0.prev_float # => 0.9999999999999999 100.0.prev_float # => 99.99999999999999 f = 0.01 (0..3).each_with_index {|i| printf "%2d %-20a %s\n", i, f, f.to_s; f = f.prev_float }
Output:
0 0x1.47ae147ae147bp-7 0.01 1 0x1.47ae147ae147ap-7 0.009999999999999998 2 0x1.47ae147ae1479p-7 0.009999999999999997 3 0x1.47ae147ae1478p-7 0.009999999999999995
Related: Float#next_float
.
Like backtrace
, but returns each line of the execution stack as a Thread::Backtrace::Location
. Accepts the same arguments as backtrace
.
f = Fiber.new { Fiber.yield } f.resume loc = f.backtrace_locations.first loc.label #=> "yield" loc.path #=> "test.rb" loc.lineno #=> 1
Returns default external encoding.
The default external encoding is used by default for strings created from the following locations:
CSV
File
data read from disk
SDBM
While strings created from these locations will have this encoding, the encoding may not be valid. Be sure to check String#valid_encoding?
.
File
data written to disk will be transcoded to the default external encoding when written, if default_internal
is not nil.
The default external encoding is initialized by the -E option. If -E isn’t set, it is initialized to UTF-8 on Windows and the locale on other operating systems.
Sets default external encoding. You should not set Encoding::default_external
in ruby code as strings created before changing the value may have a different encoding from strings created after the value was changed., instead you should use ruby -E
to invoke ruby with the correct default_external.
See Encoding::default_external
for information on how the default external encoding is used.