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
Returns true
if yield
would execute a block in the current context. The iterator?
form is mildly deprecated.
def try if block_given? yield else "no block" end end try #=> "no block" try { "hello" } #=> "hello" try do "hello" end #=> "hello"
Repeatedly executes the block.
If no block is given, an enumerator is returned instead.
loop do print "Input: " line = gets break if !line or line =~ /^qQ/ # ... end
StopIteration
raised in the block breaks the loop. In this case, loop returns the “result” value stored in the exception.
enum = Enumerator.new { |y| y << "one" y << "two" :ok } result = loop { puts enum.next } #=> :ok
Returns true
if any member of enum equals obj. Equality is tested using ==
.
IO.constants.include? :SEEK_SET #=> true IO.constants.include? :SEEK_NO_FURTHER #=> false IO.constants.member? :SEEK_SET #=> true IO.constants.member? :SEEK_NO_FURTHER #=> false
Returns the /etc/passwd information for the user with the given integer uid
.
The information is returned as a Passwd
struct.
If uid
is omitted, the value from Passwd[:uid]
is returned instead.
See the unix manpage for getpwuid(3)
for more detail.
Etc.getpwuid(0) #=> #<struct Etc::Passwd name="root", passwd="x", uid=0, gid=0, gecos="root",dir="/root", shell="/bin/bash">
Returns the /etc/passwd information for the user with specified login name
.
The information is returned as a Passwd
struct.
See the unix manpage for getpwnam(3)
for more detail.
Etc.getpwnam('root') #=> #<struct Etc::Passwd name="root", passwd="x", uid=0, gid=0, gecos="root",dir="/root", shell="/bin/bash">
Returns an entry from the /etc/passwd file.
The first time it is called it opens the file and returns the first entry; each successive call returns the next entry, or nil
if the end of the file has been reached.
To close the file when processing is complete, call ::endpwent
.
Each entry is returned as a Passwd
struct.
Returns information about the group with specified integer group_id
, as found in /etc/group.
The information is returned as a Group
struct.
See the unix manpage for getgrgid(3)
for more detail.
Etc.getgrgid(100) #=> #<struct Etc::Group name="users", passwd="x", gid=100, mem=["meta", "root"]>
Returns information about the group with specified name
, as found in /etc/group.
The information is returned as a Group
struct.
See the unix manpage for getgrnam(3)
for more detail.
Etc.getgrnam('users') #=> #<struct Etc::Group name="users", passwd="x", gid=100, mem=["meta", "root"]>
Returns an entry from the /etc/group file.
The first time it is called it opens the file and returns the first entry; each successive call returns the next entry, or nil
if the end of the file has been reached.
To close the file when processing is complete, call ::endgrent
.
Each entry is returned as a Group
struct
Allocate size
bytes of memory and return the integer memory address for the allocated memory.
Change the size of the memory allocated at the memory location addr
to size
bytes. Returns the memory address of the reallocated memory, which may be different than the address passed in.
Creates a new handler that opens library
, and returns an instance of Fiddle::Handle
.
If nil
is given for the library
, Fiddle::Handle::DEFAULT is used, which is the equivalent to RTLD_DEFAULT. See man 3 dlopen
for more.
lib = Fiddle.dlopen(nil)
The default is dependent on OS, and provide a handle for all libraries already loaded. For example, in most cases you can use this to access libc
functions, or ruby functions like rb_str_new
.
See Fiddle::Handle.new
for more.
Creates a new handler that opens library
, and returns an instance of Fiddle::Handle
.
If nil
is given for the library
, Fiddle::Handle::DEFAULT is used, which is the equivalent to RTLD_DEFAULT. See man 3 dlopen
for more.
lib = Fiddle.dlopen(nil)
The default is dependent on OS, and provide a handle for all libraries already loaded. For example, in most cases you can use this to access libc
functions, or ruby functions like rb_str_new
.
See Fiddle::Handle.new
for more.
Load a ruby data structure from a JSON
source and return it. A source can either be a string-like object, an IO-like object, or an object responding to the read method. If proc was given, it will be called with any nested Ruby object as an argument recursively in depth first order. To modify the default options pass in the optional options argument as well.
BEWARE: This method is meant to serialise data from trusted user input, like from your own database server or clients under your control, it could be dangerous to allow untrusted users to pass JSON
sources into it. The default options for the parser can be changed via the load_default_options
method.
This method is part of the implementation of the load/dump interface of Marshal
and YAML.
Convert self
to locale encoding
Convert self
to locale encoding
Returns a Digest
subclass by name
require 'openssl' OpenSSL::Digest("MD5") # => OpenSSL::Digest::MD5 Digest("Foo") # => NameError: wrong constant name Foo
Returns a Digest
subclass by name
require 'openssl' OpenSSL::Digest("MD5") # => OpenSSL::Digest::MD5 Digest("Foo") # => NameError: wrong constant name Foo
See any remaining errors held in queue.
Any errors you see here are probably due to a bug in Ruby’s OpenSSL
implementation.
Load yaml
in to a Ruby data structure. If multiple documents are provided, the object contained in the first document will be returned. filename
will be used in the exception message if any exception is raised while parsing.
Raises a Psych::SyntaxError
when a YAML syntax error is detected.
Example:
Psych.load("--- a") # => 'a' Psych.load("---\n - a\n - b") # => ['a', 'b'] begin Psych.load("--- `", "file.txt") rescue Psych::SyntaxError => ex ex.file # => 'file.txt' ex.message # => "(file.txt): found character that cannot start any token" end
When the optional symbolize_names
keyword argument is set to a true value, returns symbols for keys in Hash
objects (default: strings).
Psych.load("---\n foo: bar") # => {"foo"=>"bar"} Psych.load("---\n foo: bar", symbolize_names: true) # => {:foo=>"bar"}
Returns a default parser
Spawns the specified command on a newly allocated pty. You can also use the alias ::getpty
.
The command’s controlling tty is set to the slave device of the pty and its standard input/output/error is redirected to the slave device.
command
and command_line
are the full commands to run, given a String. Any additional arguments
will be passed to the command.
In the non-block form this returns an array of size three, [r, w, pid]
.
In the block form these same values will be yielded to the block:
Closes the syslog facility. Raises a runtime exception if it is not open.
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
.
Example usage:
require "zlib" data = "foo" puts "Adler32 checksum: #{Zlib.adler32(data).to_s(16)}" #=> Adler32 checksum: 2820145