When invoked with a block, yield all repeated permutations of length n
of the elements of the array, then return the array itself.
The implementation makes no guarantees about the order in which the repeated permutations are yielded.
If no block is given, an Enumerator
is returned instead.
Examples:
a = [1, 2] a.repeated_permutation(1).to_a #=> [[1], [2]] a.repeated_permutation(2).to_a #=> [[1,1],[1,2],[2,1],[2,2]] a.repeated_permutation(3).to_a #=> [[1,1,1],[1,1,2],[1,2,1],[1,2,2], # [2,1,1],[2,1,2],[2,2,1],[2,2,2]] a.repeated_permutation(0).to_a #=> [[]] # one permutation of length 0
Scans the current string until the match is exhausted yielding each match as it is encountered in the string. A block is not necessary as the results will simply be aggregated into the final array.
"123 456".block_scanf("%d") # => [123, 456]
If a block is given, the value from that is returned from the yield is added to an output array.
"123 456".block_scanf("%d) do |digit,| # the ',' unpacks the Array digit + 100 end # => [223, 556]
See Scanf
for details on creating a format string.
You will need to require ‘scanf’ to use String#block_scanf
Try to convert obj into a String, using to_str
method. Returns converted string or nil if obj cannot be converted for any reason.
String.try_convert("str") #=> "str" String.try_convert(/re/) #=> nil
Returns the next representable floating-point number.
Float::MAX.next_float and Float::INFINITY.next_float is Float::INFINITY
.
Float::NAN.next_float is Float::NAN
.
For example:
p 0.01.next_float #=> 0.010000000000000002 p 1.0.next_float #=> 1.0000000000000002 p 100.0.next_float #=> 100.00000000000001 p 0.01.next_float - 0.01 #=> 1.734723475976807e-18 p 1.0.next_float - 1.0 #=> 2.220446049250313e-16 p 100.0.next_float - 100.0 #=> 1.4210854715202004e-14 f = 0.01; 20.times { printf "%-20a %s\n", f, f.to_s; f = f.next_float } #=> 0x1.47ae147ae147bp-7 0.01 # 0x1.47ae147ae147cp-7 0.010000000000000002 # 0x1.47ae147ae147dp-7 0.010000000000000004 # 0x1.47ae147ae147ep-7 0.010000000000000005 # 0x1.47ae147ae147fp-7 0.010000000000000007 # 0x1.47ae147ae148p-7 0.010000000000000009 # 0x1.47ae147ae1481p-7 0.01000000000000001 # 0x1.47ae147ae1482p-7 0.010000000000000012 # 0x1.47ae147ae1483p-7 0.010000000000000014 # 0x1.47ae147ae1484p-7 0.010000000000000016 # 0x1.47ae147ae1485p-7 0.010000000000000018 # 0x1.47ae147ae1486p-7 0.01000000000000002 # 0x1.47ae147ae1487p-7 0.010000000000000021 # 0x1.47ae147ae1488p-7 0.010000000000000023 # 0x1.47ae147ae1489p-7 0.010000000000000024 # 0x1.47ae147ae148ap-7 0.010000000000000026 # 0x1.47ae147ae148bp-7 0.010000000000000028 # 0x1.47ae147ae148cp-7 0.01000000000000003 # 0x1.47ae147ae148dp-7 0.010000000000000031 # 0x1.47ae147ae148ep-7 0.010000000000000033 f = 0.0 100.times { f += 0.1 } p f #=> 9.99999999999998 # should be 10.0 in the ideal world. p 10-f #=> 1.9539925233402755e-14 # the floating-point error. p(10.0.next_float-10) #=> 1.7763568394002505e-15 # 1 ulp (units in the last place). p((10-f)/(10.0.next_float-10)) #=> 11.0 # the error is 11 ulp. p((10-f)/(10*Float::EPSILON)) #=> 8.8 # approximation of the above. p "%a" % f #=> "0x1.3fffffffffff5p+3" # the last hex digit is 5. 16 - 5 = 11 ulp.
Returns the previous representable floating-point number.
(-Float::MAX).prev_float and (-Float::INFINITY).prev_float is -Float::INFINITY.
Float::NAN.prev_float is Float::NAN
.
For example:
p 0.01.prev_float #=> 0.009999999999999998 p 1.0.prev_float #=> 0.9999999999999999 p 100.0.prev_float #=> 99.99999999999999 p 0.01 - 0.01.prev_float #=> 1.734723475976807e-18 p 1.0 - 1.0.prev_float #=> 1.1102230246251565e-16 p 100.0 - 100.0.prev_float #=> 1.4210854715202004e-14 f = 0.01; 20.times { printf "%-20a %s\n", f, f.to_s; f = f.prev_float } #=> 0x1.47ae147ae147bp-7 0.01 # 0x1.47ae147ae147ap-7 0.009999999999999998 # 0x1.47ae147ae1479p-7 0.009999999999999997 # 0x1.47ae147ae1478p-7 0.009999999999999995 # 0x1.47ae147ae1477p-7 0.009999999999999993 # 0x1.47ae147ae1476p-7 0.009999999999999992 # 0x1.47ae147ae1475p-7 0.00999999999999999 # 0x1.47ae147ae1474p-7 0.009999999999999988 # 0x1.47ae147ae1473p-7 0.009999999999999986 # 0x1.47ae147ae1472p-7 0.009999999999999985 # 0x1.47ae147ae1471p-7 0.009999999999999983 # 0x1.47ae147ae147p-7 0.009999999999999981 # 0x1.47ae147ae146fp-7 0.00999999999999998 # 0x1.47ae147ae146ep-7 0.009999999999999978 # 0x1.47ae147ae146dp-7 0.009999999999999976 # 0x1.47ae147ae146cp-7 0.009999999999999974 # 0x1.47ae147ae146bp-7 0.009999999999999972 # 0x1.47ae147ae146ap-7 0.00999999999999997 # 0x1.47ae147ae1469p-7 0.009999999999999969 # 0x1.47ae147ae1468p-7 0.009999999999999967
Returns default external encoding.
The default external encoding is used by default for strings created from the following locations:
File
data read from disk
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.
The default external encoding is initialized by the locale or -E option.
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.
Returns default internal encoding. Strings will be transcoded to the default internal encoding in the following places if the default internal encoding is not nil:
File
data read from disk
Strings returned from Readline
Strings returned from SDBM
Values from ENV
Values in ARGV including $PROGRAM_NAME
Additionally String#encode
and String#encode!
use the default internal encoding if no encoding is given.
The locale encoding (__ENCODING__), not default_internal
, is used as the encoding of created strings.
Encoding::default_internal
is initialized by the source file’s internal_encoding or -E option.
Sets default internal encoding or removes default internal encoding when passed nil. You should not set Encoding::default_internal
in ruby code as strings created before changing the value may have a different encoding from strings created after the change. Instead you should use ruby -E
to invoke ruby with the correct default_internal.
See Encoding::default_internal
for information on how the default internal encoding is used.
Returns the locale charmap name. It returns nil if no appropriate information.
Debian GNU/Linux LANG=C Encoding.locale_charmap #=> "ANSI_X3.4-1968" LANG=ja_JP.EUC-JP Encoding.locale_charmap #=> "EUC-JP" SunOS 5 LANG=C Encoding.locale_charmap #=> "646" LANG=ja Encoding.locale_charmap #=> "eucJP"
The result is highly platform dependent. So Encoding.find(Encoding.locale_charmap)
may cause an error. If you need some encoding object even for unknown locale, Encoding.find
(“locale”) can be used.
Returns any backtrace associated with the exception. This method is similar to Exception#backtrace
, but the backtrace is an array of
Thread::Backtrace::Location.
Now, this method is not affected by Exception#set_backtrace()
.
Return a list of the local variable names defined where this NameError
exception was raised.
Internal use only.
Creates instance variables and corresponding methods that return the value of each instance variable. Equivalent to calling “attr
:name” on each name in turn. String arguments are converted to symbols.
Creates an accessor method to allow assignment to the attribute symbol.id2name
. String arguments are converted to symbols.
Checks for a constant with the given name in mod. If inherit
is set, the lookup will also search the ancestors (and Object
if mod is a Module
).
The value of the constant is returned if a definition is found, otherwise a NameError
is raised.
Math.const_get(:PI) #=> 3.14159265358979
This method will recursively look up constant names if a namespaced class name is provided. For example:
module Foo; class Bar; end end Object.const_get 'Foo::Bar'
The inherit
flag is respected on each lookup. For example:
module Foo class Bar VAL = 10 end class Baz < Bar; end end Object.const_get 'Foo::Baz::VAL' # => 10 Object.const_get 'Foo::Baz::VAL', false # => NameError
If the argument is not a valid constant name a NameError
will be raised with a warning “wrong constant name”.
Object.const_get 'foobar' #=> NameError: wrong constant name foobar
Returns true if the given week date is valid, and false if not.
Date.valid_commercial?(2001,5,6) #=> true Date.valid_commercial?(2001,5,8) #=> false
See also jd and commercial.
Try to convert obj into an IO
, using to_io
method. Returns converted IO
or nil
if obj cannot be converted for any reason.
IO.try_convert(STDOUT) #=> STDOUT IO.try_convert("STDOUT") #=> nil require 'zlib' f = open("/tmp/zz.gz") #=> #<File:/tmp/zz.gz> z = Zlib::GzipReader.open(f) #=> #<Zlib::GzipReader:0x81d8744> IO.try_convert(z) #=> #<File:/tmp/zz.gz>
Closes the read end of a duplex I/O stream (i.e., one that contains both a read and a write stream, such as a pipe). Will raise an IOError
if the stream is not duplexed.
f = IO.popen("/bin/sh","r+") f.close_read f.readlines
produces:
prog.rb:3:in `readlines': not opened for reading (IOError) from prog.rb:3
Closes the write end of a duplex I/O stream (i.e., one that contains both a read and a write stream, such as a pipe). Will raise an IOError
if the stream is not duplexed.
f = IO.popen("/bin/sh","r+") f.close_write f.print "nowhere"
produces:
prog.rb:3:in `write': not opened for writing (IOError) from prog.rb:3:in `print' from prog.rb:3
Returns the Encoding
object that represents the encoding of the file. If io is in write mode and no encoding is specified, returns nil
.
Returns the Encoding
of the internal string if conversion is specified. Otherwise returns nil
.
Reads at most maxlen bytes from ios using the read(2) system call after O_NONBLOCK is set for the underlying file descriptor.
If the optional outbuf argument is present, it must reference a String, which will receive the data. The outbuf will contain only the received data after the method call even if it is not empty at the beginning.
read_nonblock
just calls the read(2) system call. It causes all errors the read(2) system call causes: Errno::EWOULDBLOCK, Errno::EINTR, etc. The caller should care such errors.
If the exception is Errno::EWOULDBLOCK or Errno::EAGAIN, it is extended by IO::WaitReadable
. So IO::WaitReadable
can be used to rescue the exceptions for retrying read_nonblock.
read_nonblock
causes EOFError
on EOF.
If the read byte buffer is not empty, read_nonblock
reads from the buffer like readpartial. In this case, the read(2) system call is not called.
When read_nonblock
raises an exception kind of IO::WaitReadable
, read_nonblock
should not be called until io is readable for avoiding busy loop. This can be done as follows.
# emulates blocking read (readpartial). begin result = io.read_nonblock(maxlen) rescue IO::WaitReadable IO.select([io]) retry end
Although IO#read_nonblock
doesn’t raise IO::WaitWritable
. OpenSSL::Buffering#read_nonblock
can raise IO::WaitWritable
. If IO
and SSL should be used polymorphically, IO::WaitWritable
should be rescued too. See the document of OpenSSL::Buffering#read_nonblock
for sample code.
Note that this method is identical to readpartial except the non-blocking flag is set.
By specifying ‘exception: false`, the options hash allows you to indicate that read_nonblock
should not raise an IO::WaitReadable
exception, but return the symbol :wait_readable instead.
Writes the given string to ios using the write(2) system call after O_NONBLOCK is set for the underlying file descriptor.
It returns the number of bytes written.
write_nonblock
just calls the write(2) system call. It causes all errors the write(2) system call causes: Errno::EWOULDBLOCK, Errno::EINTR, etc. The result may also be smaller than string.length (partial write). The caller should care such errors and partial write.
If the exception is Errno::EWOULDBLOCK or Errno::EAGAIN, it is extended by IO::WaitWritable
. So IO::WaitWritable
can be used to rescue the exceptions for retrying write_nonblock.
# Creates a pipe. r, w = IO.pipe # write_nonblock writes only 65536 bytes and return 65536. # (The pipe size is 65536 bytes on this environment.) s = "a" #100000 p w.write_nonblock(s) #=> 65536 # write_nonblock cannot write a byte and raise EWOULDBLOCK (EAGAIN). p w.write_nonblock("b") # Resource temporarily unavailable (Errno::EAGAIN)
If the write buffer is not empty, it is flushed at first.
When write_nonblock
raises an exception kind of IO::WaitWritable
, write_nonblock
should not be called until io is writable for avoiding busy loop. This can be done as follows.
begin result = io.write_nonblock(string) rescue IO::WaitWritable, Errno::EINTR IO.select(nil, [io]) retry end
Note that this doesn’t guarantee to write all data in string. The length written is reported as result and it should be checked later.
On some platforms such as Windows, write_nonblock
is not supported according to the kind of the IO
object. In such cases, write_nonblock
raises Errno::EBADF
.
By specifying ‘exception: false`, the options hash allows you to indicate that write_nonblock
should not raise an IO::WaitWritable
exception, but return the symbol :wait_writable instead.
Try to convert obj into a Regexp
, using to_regexp method. Returns converted regexp or nil if obj cannot be converted for any reason.
Regexp.try_convert(/re/) #=> /re/ Regexp.try_convert("re") #=> nil o = Object.new Regexp.try_convert(o) #=> nil def o.to_regexp() /foo/ end Regexp.try_convert(o) #=> /foo/