Reads one character from the stream. Returns nil if called at end of file.
Reads a one-character string from the stream. Raises an EOFError
at end of file.
Pushes character c back onto the stream such that a subsequent buffered character read will return it.
Unlike IO#getc
multiple bytes may be pushed back onto the stream.
Has no effect on unbuffered reads (such as sysread).
Return true
if the PRNG has been seeded with enough data, false
otherwise.
Generate a Checkbox Input element as a string.
The attributes of the element can be specified as three arguments, name
, value
, and checked
. checked
is a boolean value; if true, the CHECKED attribute will be included in the element.
Alternatively, the attributes can be specified as a hash.
checkbox("name") # = checkbox("NAME" => "name") checkbox("name", "value") # = checkbox("NAME" => "name", "VALUE" => "value") checkbox("name", "value", true) # = checkbox("NAME" => "name", "VALUE" => "value", "CHECKED" => true)
Returns “true” if the “transfer-encoding” header is present and set to “chunked”. This is an HTTP/1.1 feature, allowing the content to be sent in “chunks” without at the outset stating the entire content length.
returns a charset parameter in Content-Type field. It is downcased for canonicalization.
If charset parameter is not given but a block is given, the block is called and its result is returned. It can be used to guess charset.
If charset parameter and block is not given, nil is returned except text type. In that case, “utf-8” is returned as defined by RFC6838 4.2.1
Random::Formatter#choose
generates a string that randomly draws from a source array of characters.
The argument source specifies the array of characters from which to generate the string. The argument n specifies the length, in characters, of the string to be generated.
The result may contain whatever characters are in the source array.
require 'random/formatter' prng.choose([*'l'..'r'], 16) #=> "lmrqpoonmmlqlron" prng.choose([*'0'..'9'], 5) #=> "27309"
Simple deprecation method that deprecates name
by wrapping it up in a dummy method. It warns on each call to the dummy method telling the user of repl
(unless repl
is :none) and the year/month that it is planned to go away.
A Zlib::Inflate#inflate
wrapper
Iterates over array indexes.
When a block given, passes each successive array index to the block; returns self
:
a = [:foo, 'bar', 2] a.each_index {|index| puts "#{index} #{a[index]}" }
Output:
0 foo 1 bar 2 2
Allows the array to be modified during iteration:
a = [:foo, 'bar', 2] a.each_index {|index| puts index; a.clear if index > 0 }
Output:
0 1
When no block given, returns a new Enumerator:
a = [:foo, 'bar', 2] e = a.each_index e # => #<Enumerator: [:foo, "bar", 2]:each_index> a1 = e.each {|index| puts "#{index} #{a[index]}"}
Output:
0 foo 1 bar 2 2
Related: each
, reverse_each
.
Iterates backwards over array elements.
When a block given, passes, in reverse order, each element to the block; returns self
:
a = [:foo, 'bar', 2] a.reverse_each {|element| puts "#{element.class} #{element}" }
Output:
Integer 2 String bar Symbol foo
Allows the array to be modified during iteration:
a = [:foo, 'bar', 2] a.reverse_each {|element| puts element; a.clear if element.to_s.start_with?('b') }
Output:
2 bar
When no block given, returns a new Enumerator:
a = [:foo, 'bar', 2] e = a.reverse_each e # => #<Enumerator: [:foo, "bar", 2]:reverse_each> a1 = e.each {|element| puts "#{element.class} #{element}" }
Output:
Integer 2 String bar Symbol foo
Related: each
, each_index
.
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.
Searches self
as described at method bsearch
, but returns the index of the found element instead of the element itself.
Returns the list of private methods accessible to obj. If the all parameter is set to false
, only those methods in the receiver will be listed.
Splits str using the supplied parameter as the record separator ($/
by default), passing each substring in turn to the supplied block. If a zero-length record separator is supplied, the string is split into paragraphs delimited by multiple successive newlines.
If chomp
is true
, separator
will be removed from the end of each line.
If no block is given, an enumerator is returned instead.
"hello\nworld".each_line {|s| p s} # prints: # "hello\n" # "world" "hello\nworld".each_line('l') {|s| p s} # prints: # "hel" # "l" # "o\nworl" # "d" "hello\n\n\nworld".each_line('') {|s| p s} # prints # "hello\n\n" # "world" "hello\nworld".each_line(chomp: true) {|s| p s} # prints: # "hello" # "world" "hello\nworld".each_line('l', chomp: true) {|s| p s} # prints: # "he" # "" # "o\nwor" # "d"
Passes each byte in str to the given block, or returns an enumerator if no block is given.
"hello".each_byte {|c| print c, ' ' }
produces:
104 101 108 108 111
Passes the Integer
ordinal of each character in str, also known as a codepoint when applied to Unicode strings to the given block. For encodings other than UTF-8/UTF-16(BE|LE)/UTF-32(BE|LE), values are directly derived from the binary representation of each character.
If no block is given, an enumerator is returned instead.
"hello\u0639".each_codepoint {|c| print c, ' ' }
produces:
104 101 108 108 111 1593