Returns the name at the definition of the current method as a Symbol
. If called outside of a method, it returns nil
.
Returns the called name of the current method as a Symbol
. If called outside of a method, it returns nil
.
Suspends the current thread for duration seconds (which may be any number, including a Float
with fractional seconds). Returns the actual number of seconds slept (rounded), which may be less than that asked for if another thread calls Thread#run
. Called without an argument, sleep() will sleep forever.
Time.new #=> 2008-03-08 19:56:19 +0900 sleep 1.2 #=> 1 Time.new #=> 2008-03-08 19:56:20 +0900 sleep 1.9 #=> 2 Time.new #=> 2008-03-08 19:56:22 +0900
Returns the current execution stack—an array containing strings in the form file:line
or file:line: in `method'
.
The optional start parameter determines the number of initial stack entries to omit from the top of the stack.
A second optional length
parameter can be used to limit how many entries are returned from the stack.
Returns nil
if start is greater than the size of current execution stack.
Optionally you can pass a range, which will return an array containing the entries within the specified range.
def a(skip) caller(skip) end def b(skip) a(skip) end def c(skip) b(skip) end c(0) #=> ["prog:2:in `a'", "prog:5:in `b'", "prog:8:in `c'", "prog:10:in `<main>'"] c(1) #=> ["prog:5:in `b'", "prog:8:in `c'", "prog:11:in `<main>'"] c(2) #=> ["prog:8:in `c'", "prog:12:in `<main>'"] c(3) #=> ["prog:13:in `<main>'"] c(4) #=> [] c(5) #=> nil
Combines all elements of enum by applying a binary operation, specified by a block or a symbol that names a method or operator.
The inject and reduce methods are aliases. There is no performance benefit to either.
If you specify a block, then for each element in enum the block is passed an accumulator value (memo) and the element. If you specify a symbol instead, then each element in the collection will be passed to the named method of memo. In either case, the result becomes the new value for memo. At the end of the iteration, the final value of memo is the return value for the method.
If you do not explicitly specify an initial value for memo, then the first element of collection is used as the initial value of memo.
# Sum some numbers (5..10).reduce(:+) #=> 45 # Same using a block and inject (5..10).inject { |sum, n| sum + n } #=> 45 # Multiply some numbers (5..10).reduce(1, :*) #=> 151200 # Same using a block (5..10).inject(1) { |product, n| product * n } #=> 151200 # find the longest word longest = %w{ cat sheep bear }.inject do |memo, word| memo.length > word.length ? memo : word end longest #=> "sheep"
Calls block for each element of enum repeatedly n times or forever if none or nil
is given. If a non-positive number is given or the collection is empty, does nothing. Returns nil
if the loop has finished without getting interrupted.
Enumerable#cycle
saves elements in an internal array so changes to enum after the first pass have no effect.
If no block is given, an enumerator is returned instead.
a = ["a", "b", "c"] a.cycle { |x| puts x } # print, a, b, c, a, b, c,.. forever. a.cycle(2) { |x| puts x } # print, a, b, c, a, b, c.
Returns a hash that contains filename as key and coverage array as value. If clear
is true, it clears the counters to zero. If stop
is true, it disables coverage measurement.
Dumps obj
as a JSON string, i.e. calls generate on the object and returns the result.
The default options can be changed via method JSON.dump_default_options
.
Argument io
, if given, should respond to method write
; the JSON String is written to io
, and io
is returned. If io
is not given, the JSON String is returned.
Argument limit
, if given, is passed to JSON.generate
as option max_nesting
.
When argument io
is not given, returns the JSON String generated from obj
:
obj = {foo: [0, 1], bar: {baz: 2, bat: 3}, bam: :bad} json = JSON.dump(obj) json # => "{\"foo\":[0,1],\"bar\":{\"baz\":2,\"bat\":3},\"bam\":\"bad\"}"
When argument io
is given, writes the JSON String to io
and returns io
:
path = 't.json' File.open(path, 'w') do |file| JSON.dump(obj, file) end # => #<File:t.json (closed)> puts File.read(path)
Output:
{"foo":[0,1],"bar":{"baz":2,"bat":3},"bam":"bad"}
Convert self
to locale encoding
Convert self
to locale encoding
Dump the contents of a ruby object as JSON
.
This method is only expected to work with C Ruby. This is an experimental method and is subject to change. In particular, the function signature and output format are not guaranteed to be compatible in future versions of ruby.
Dump Ruby object o
to a YAML
string. Optional options
may be passed in to control the output format. If an IO
object is passed in, the YAML
will be dumped to that IO
object.
Currently supported options are:
:indentation
Number of space characters used to indent. Acceptable value should be in 0..9
range, otherwise option is ignored.
Default: 2
.
:line_width
Max character to wrap line at.
Default: 0
(meaning “wrap at 81”).
:canonical
Write “canonical” YAML
form (very verbose, yet strictly formal).
Default: false
.
:header
Write %YAML [version]
at the beginning of document.
Default: false
.
Example:
# Dump an array, get back a YAML string Psych.dump(['a', 'b']) # => "---\n- a\n- b\n" # Dump an array to an IO object Psych.dump(['a', 'b'], StringIO.new) # => #<StringIO:0x000001009d0890> # Dump an array with indentation set Psych.dump(['a', ['b']], indentation: 3) # => "---\n- a\n- - b\n" # Dump an array to an IO with indentation set Psych.dump(['a', ['b']], StringIO.new, indentation: 3)
Calculates Adler-32 checksum for string
, and returns updated value of adler
. If string
is omitted, it returns the Adler-32 initial value. If adler
is omitted, it assumes that the initial value is given to adler
. If string
is an IO
instance, reads from the IO
until the IO
returns nil and returns Adler-32 of all read data.
Example usage:
require "zlib" data = "foo" puts "Adler32 checksum: #{Zlib.adler32(data).to_s(16)}" #=> Adler32 checksum: 2820145
Returns true
if the named file is readable by the effective user and group id of this process. See eaccess(3).
Note that some OS-level security features may cause this to return true even though the file is not readable by the effective user/group.
Returns true
if the named file is writable by the effective user and group id of this process. See eaccess(3).
Note that some OS-level security features may cause this to return true even though the file is not writable by the effective user/group.
Returns true
if the named file is executable by the effective user and group id of this process. See eaccess(3).
Windows does not support execute permissions separately from read permissions. On Windows, a file is only considered executable if it ends in .bat, .cmd, .com, or .exe.
Note that some OS-level security features may cause this to return true even though the file is not executable by the effective user/group.
Returns true
if the named file
exists and is a regular file.
file
can be an IO
object.
If the file
argument is a symbolic link, it will resolve the symbolic link and use the file referenced by the link.
Enables garbage collection, returning true
if garbage collection was previously disabled.
GC.disable #=> false GC.enable #=> true GC.enable #=> false
Disables garbage collection, returning true
if garbage collection was already disabled.
GC.disable #=> false GC.disable #=> true
Returns the Base64-decoded version of str
. This method complies with RFC 2045. Characters outside the base alphabet are ignored.
require 'base64' str = 'VGhpcyBpcyBsaW5lIG9uZQpUaGlzIG' + 'lzIGxpbmUgdHdvClRoaXMgaXMgbGlu' + 'ZSB0aHJlZQpBbmQgc28gb24uLi4K' puts Base64.decode64(str)
Generates:
This is line one This is line two This is line three And so on...
Returns true if new
is newer than all old_list
. Non-existent files are older than any file.
FileUtils.uptodate?('hello.o', %w(hello.c hello.h)) or \ system 'make hello.o'
Returns true if new
is newer than all old_list
. Non-existent files are older than any file.
FileUtils.uptodate?('hello.o', %w(hello.c hello.h)) or \ system 'make hello.o'