Activates the trace.
Returns true
if trace was enabled. Returns false
if trace was disabled.
trace.enabled? #=> false trace.enable #=> false (previous state) # trace is enabled trace.enabled? #=> true trace.enable #=> true (previous state) # trace is still enabled
If a block is given, the trace will only be enabled within the scope of the block.
trace.enabled? #=> false trace.enable do trace.enabled? # only enabled for this block end trace.enabled? #=> false
target
, target_line
and target_thread
parameters are used to limit tracing only to specified code objects. target
should be a code object for which RubyVM::InstructionSequence.of
will return an instruction sequence.
t = TracePoint.new(:line) { |tp| p tp } def m1 p 1 end def m2 p 2 end t.enable(target: method(:m1)) m1 # prints #<TracePoint:line@test.rb:5 in `m1'> m2 # prints nothing
Note: You cannot access event hooks within the enable
block.
trace.enable { p tp.lineno } #=> RuntimeError: access from outside
Deactivates the trace
Return true if trace was enabled. Return false if trace was disabled.
trace.enabled? #=> true trace.disable #=> true (previous status) trace.enabled? #=> false trace.disable #=> false
If a block is given, the trace will only be disable within the scope of the block.
trace.enabled? #=> true trace.disable do trace.enabled? # only disabled for this block end trace.enabled? #=> true
Note: You cannot access event hooks within the block.
trace.disable { p tp.lineno } #=> RuntimeError: access from outside
The current status of the trace
Returns x+i*y;
Complex(1, 2) #=> (1+2i) Complex('1+2i') #=> (1+2i) Complex(nil) #=> TypeError Complex(1, nil) #=> TypeError Complex(1, nil, exception: false) #=> nil Complex('1+2', exception: false) #=> nil
Syntax of string form:
string form = extra spaces , complex , extra spaces ; complex = real part | [ sign ] , imaginary part | real part , sign , imaginary part | rational , "@" , rational ; real part = rational ; imaginary part = imaginary unit | unsigned rational , imaginary unit ; rational = [ sign ] , unsigned rational ; unsigned rational = numerator | numerator , "/" , denominator ; numerator = integer part | fractional part | integer part , fractional part ; denominator = digits ; integer part = digits ; fractional part = "." , digits , [ ( "e" | "E" ) , [ sign ] , digits ] ; imaginary unit = "i" | "I" | "j" | "J" ; sign = "-" | "+" ; digits = digit , { digit | "_" , digit }; digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ; extra spaces = ? \s* ? ;
See String#to_c
.
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.
If anIO (an IO-like object or an object that responds to the write method) was given, the resulting JSON
is written to it.
If the number of nested arrays or objects exceeds limit, an ArgumentError
exception is raised. This argument is similar (but not exactly the same!) to the limit argument in Marshal.dump
.
The default options for the generator can be changed via the dump_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
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
.
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'