Passes elements to the block until the block returns nil
or false
, then stops iterating and returns an array of all prior elements.
If no block is given, an Enumerator
is returned instead.
See also Array#drop_while
a = [1, 2, 3, 4, 5, 0] a.take_while { |i| i < 3 } #=> [1, 2]
Returns an array of instance variable names for the receiver. Note that simply defining an accessor does not create the corresponding instance variable.
class Fred attr_accessor :a1 def initialize @iv = 3 end end Fred.new.instance_variables #=> [:@iv]
Returns true
if obj is an instance of the given class. See also Object#kind_of?
.
class A; end class B < A; end class C < B; end b = B.new b.instance_of? A #=> false b.instance_of? B #=> true b.instance_of? C #=> false
Numerics are immutable values, which should not be copied.
Any attempt to use this method on a Numeric
will raise a TypeError
.
Returns a normalized form of str
, using Unicode normalizations NFC, NFD, NFKC, or NFKD. The normalization form used is determined by form
, which is any of the four values :nfc, :nfd, :nfkc, or :nfkd. The default is :nfc.
If the string is not in a Unicode Encoding
, then an Exception
is raised. In this context, ‘Unicode Encoding’ means any of UTF-8, UTF-16BE/LE, and UTF-32BE/LE, as well as GB18030, UCS_2BE, and UCS_4BE. Anything else than UTF-8 is implemented by converting to UTF-8, which makes it slower than UTF-8.
Examples
"a\u0300".unicode_normalize #=> 'à' (same as "\u00E0") "a\u0300".unicode_normalize(:nfc) #=> 'à' (same as "\u00E0") "\u00E0".unicode_normalize(:nfd) #=> 'à' (same as "a\u0300") "\xE0".force_encoding('ISO-8859-1').unicode_normalize(:nfd) #=> Encoding::CompatibilityError raised
Destructive version of String#unicode_normalize
, doing Unicode normalization in place.
Checks whether str
is in Unicode normalization form form
, which is any of the four values :nfc, :nfd, :nfkc, or :nfkd. The default is :nfc.
If the string is not in a Unicode Encoding
, then an Exception
is raised. For details, see String#unicode_normalize
.
Examples
"a\u0300".unicode_normalized? #=> false "a\u0300".unicode_normalized?(:nfd) #=> true "\u00E0".unicode_normalized? #=> true "\u00E0".unicode_normalized?(:nfd) #=> false "\xE0".force_encoding('ISO-8859-1').unicode_normalized? #=> Encoding::CompatibilityError raised
Replaces the contents and taintedness of str with the corresponding values in other_str.
s = "hello" #=> "hello" s.replace "world" #=> "world"
Returns true if str
starts with one of the prefixes
given.
"hello".start_with?("hell") #=> true # returns true if one of the prefixes matches. "hello".start_with?("heaven", "hell") #=> true "hello".start_with?("heaven", "paradise") #=> false
Returns true for a string which is encoded correctly.
"\xc2\xa1".force_encoding("UTF-8").valid_encoding? #=> true "\xc2".force_encoding("UTF-8").valid_encoding? #=> false "\x80".force_encoding("UTF-8").valid_encoding? #=> false
Returns true for a string which has only ASCII characters.
"abc".force_encoding("UTF-8").ascii_only? #=> true "abc\u{6666}".force_encoding("UTF-8").ascii_only? #=> false
Returns true
if the named file is readable by the real user and group id of this process. See access(3).
If file_name is writable by others, returns an integer representing the file permission bits of file_name. Returns nil
otherwise. The meaning of the bits is platform dependent; on Unix systems, see stat(2)
.
file_name can be an IO
object.
File.world_writable?("/tmp") #=> 511 m = File.world_writable?("/tmp") sprintf("%o", m) #=> "777"
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 the next object as an array in the enumerator, and move the internal position forward. When the position reached at the end, StopIteration
is raised.
This method can be used to distinguish yield
and yield nil
.
o = Object.new def o.each yield yield 1 yield 1, 2 yield nil yield [1, 2] end e = o.to_enum p e.next_values p e.next_values p e.next_values p e.next_values p e.next_values e = o.to_enum p e.next p e.next p e.next p e.next p e.next ## yield args next_values next # yield [] nil # yield 1 [1] 1 # yield 1, 2 [1, 2] [1, 2] # yield nil [nil] nil # yield [1, 2] [[1, 2]] [1, 2]
Note that next_values
does not affect other non-external enumeration methods unless underlying iteration method itself has side-effect, e.g. IO#each_line
.
Returns the next object as an array, similar to Enumerator#next_values
, but doesn’t move the internal position forward. If the position is already at the end, StopIteration
is raised.
o = Object.new def o.each yield yield 1 yield 1, 2 end e = o.to_enum p e.peek_values #=> [] e.next p e.peek_values #=> [1] p e.peek_values #=> [1] e.next p e.peek_values #=> [1, 2] e.next p e.peek_values # raises StopIteration
Return a list of the local variable names defined where this NameError
exception was raised.
Internal use only.
Returns an array containing the names of the public and protected instance methods in the receiver. For a module, these are the public and protected methods; for a class, they are the instance (not singleton) methods. If the optional parameter is false
, the methods of any ancestors are not included.
module A def method1() end end class B include A def method2() end end class C < B def method3() end end A.instance_methods(false) #=> [:method1] B.instance_methods(false) #=> [:method2] B.instance_methods(true).include?(:method1) #=> true C.instance_methods(false) #=> [:method3] C.instance_methods.include?(:method2) #=> true
Makes a list of existing constants public.
Makes a list of existing constants private.