Creates, updates, or deletes the named environment variable, returning the value. Both name
and value
may be instances of String
. See Valid Names and Values.
If the named environment variable does not exist:
If value
is nil
, does nothing.
ENV.clear ENV['foo'] = nil # => nil ENV.include?('foo') # => false ENV.store('bar', nil) # => nil ENV.include?('bar') # => false
If value
is not nil
, creates the environment variable with name
and value
:
# Create 'foo' using ENV.[]=. ENV['foo'] = '0' # => '0' ENV['foo'] # => '0' # Create 'bar' using ENV.store. ENV.store('bar', '1') # => '1' ENV['bar'] # => '1'
If the named environment variable exists:
If value
is not nil
, updates the environment variable with value value
:
# Update 'foo' using ENV.[]=. ENV['foo'] = '2' # => '2' ENV['foo'] # => '2' # Update 'bar' using ENV.store. ENV.store('bar', '3') # => '3' ENV['bar'] # => '3'
If value
is nil
, deletes the environment variable:
# Delete 'foo' using ENV.[]=. ENV['foo'] = nil # => nil ENV.include?('foo') # => false # Delete 'bar' using ENV.store. ENV.store('bar', nil) # => nil ENV.include?('bar') # => false
Raises an exception if name
or value
is invalid. See Invalid Names and Values.
Writes each of the given objects
if inplace mode.
Writes the given objects to the stream; returns nil
. Appends the output record separator $OUTPUT_RECORD_SEPARATOR
($\
), if it is not nil
. See Line IO.
With argument objects
given, for each object:
Converts via its method to_s
if not a string.
Writes to the stream.
If not the last object, writes the output field separator $OUTPUT_FIELD_SEPARATOR
($,
) if it is not nil
.
With default separators:
f = File.open('t.tmp', 'w+') objects = [0, 0.0, Rational(0, 1), Complex(0, 0), :zero, 'zero'] p $OUTPUT_RECORD_SEPARATOR p $OUTPUT_FIELD_SEPARATOR f.print(*objects) f.rewind p f.read f.close
Output:
nil nil "00.00/10+0izerozero"
With specified separators:
$\ = "\n" $, = ',' f.rewind f.print(*objects) f.rewind p f.read
Output:
"0,0.0,0/1,0+0i,zero,zero\n"
With no argument given, writes the content of $_
(which is usually the most recent user input):
f = File.open('t.tmp', 'w+') gets # Sets $_ to the most recent user input. f.print f.close
Formats and writes objects
to the stream.
For details on format_string
, see Format Specifications.
Sets the current file to the next file in ARGV. If there aren’t any more files it has no effect.
For example:
$ ruby argf.rb foo bar ARGF.filename #=> "foo" ARGF.skip ARGF.filename #=> "bar"
Returns true if the ipaddr is an IPv4 address.
Returns true if the ipaddr is an IPv6 address.
Returns true if the ipaddr is a private address. IPv4 addresses in 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16 as defined in RFC 1918 and IPv6 Unique Local Addresses in fc00::/7 as defined in RFC 4193 are considered private. Private IPv4 addresses in the IPv4-mapped IPv6 address range are also considered private.
Puts option summary into to
and returns to
. Yields each line if a block is given.
to
Output destination, which must have method <<. Defaults to [].
width
Width of left side, defaults to @summary_width.
max
Maximum length allowed for left side, defaults to width
- 1.
indent
Indentation, defaults to @summary_indent.
Increases left margin after newline with indent
for line breaks added in the block.
Opens a transaction block for the store. See Transactions.
With argument read_only
as false
, the block may both read from and write to the store.
With argument read_only
as true
, the block may not include calls to transaction
, []=
, or delete
.
Raises an exception if called within a transaction block.
Returns the number of mandatory arguments. If the block is declared to take no arguments, returns 0. If the block is known to take exactly n arguments, returns n. If the block has optional arguments, returns -n-1, where n is the number of mandatory arguments, with the exception for blocks that are not lambdas and have only a finite number of optional arguments; in this latter case, returns n. Keyword arguments will be considered as a single additional argument, that argument being mandatory if any keyword argument is mandatory. A proc
with no argument declarations is the same as a block declaring ||
as its arguments.
proc {}.arity #=> 0 proc { || }.arity #=> 0 proc { |a| }.arity #=> 1 proc { |a, b| }.arity #=> 2 proc { |a, b, c| }.arity #=> 3 proc { |*a| }.arity #=> -1 proc { |a, *b| }.arity #=> -2 proc { |a, *b, c| }.arity #=> -3 proc { |x:, y:, z:0| }.arity #=> 1 proc { |*a, x:, y:0| }.arity #=> -2 proc { |a=0| }.arity #=> 0 lambda { |a=0| }.arity #=> -1 proc { |a=0, b| }.arity #=> 1 lambda { |a=0, b| }.arity #=> -2 proc { |a=0, b=0| }.arity #=> 0 lambda { |a=0, b=0| }.arity #=> -1 proc { |a, b=0| }.arity #=> 1 lambda { |a, b=0| }.arity #=> -2 proc { |(a, b), c=0| }.arity #=> 1 lambda { |(a, b), c=0| }.arity #=> -2 proc { |a, x:0, y:0| }.arity #=> 1 lambda { |a, x:0, y:0| }.arity #=> -2
Returns an indication of the number of arguments accepted by a method. Returns a nonnegative integer for methods that take a fixed number of arguments. For Ruby methods that take a variable number of arguments, returns -n-1, where n is the number of required arguments. Keyword arguments will be considered as a single additional argument, that argument being mandatory if any keyword argument is mandatory. For methods written in C, returns -1 if the call takes a variable number of arguments.
class C def one; end def two(a); end def three(*a); end def four(a, b); end def five(a, b, *c); end def six(a, b, *c, &d); end def seven(a, b, x:0); end def eight(x:, y:); end def nine(x:, y:, **z); end def ten(*a, x:, y:); end end c = C.new c.method(:one).arity #=> 0 c.method(:two).arity #=> 1 c.method(:three).arity #=> -1 c.method(:four).arity #=> 2 c.method(:five).arity #=> -3 c.method(:six).arity #=> -3 c.method(:seven).arity #=> -3 c.method(:eight).arity #=> 1 c.method(:nine).arity #=> 1 c.method(:ten).arity #=> -2 "cat".method(:size).arity #=> 0 "cat".method(:replace).arity #=> 1 "cat".method(:squeeze).arity #=> -1 "cat".method(:count).arity #=> -1
Returns an indication of the number of arguments accepted by a method. Returns a nonnegative integer for methods that take a fixed number of arguments. For Ruby methods that take a variable number of arguments, returns -n-1, where n is the number of required arguments. Keyword arguments will be considered as a single additional argument, that argument being mandatory if any keyword argument is mandatory. For methods written in C, returns -1 if the call takes a variable number of arguments.
class C def one; end def two(a); end def three(*a); end def four(a, b); end def five(a, b, *c); end def six(a, b, *c, &d); end def seven(a, b, x:0); end def eight(x:, y:); end def nine(x:, y:, **z); end def ten(*a, x:, y:); end end c = C.new c.method(:one).arity #=> 0 c.method(:two).arity #=> 1 c.method(:three).arity #=> -1 c.method(:four).arity #=> 2 c.method(:five).arity #=> -3 c.method(:six).arity #=> -3 c.method(:seven).arity #=> -3 c.method(:eight).arity #=> 1 c.method(:nine).arity #=> 1 c.method(:ten).arity #=> -2 "cat".method(:size).arity #=> 0 "cat".method(:replace).arity #=> 1 "cat".method(:squeeze).arity #=> -1 "cat".method(:count).arity #=> -1
Returns an array of all existing Thread
objects that belong to this group.
ThreadGroup::Default.list #=> [#<Thread:0x401bdf4c run>]
Basically the same as ::new
. However, if class Thread
is subclassed, then calling start
in that subclass will not invoke the subclass’s initialize
method.
Stops execution of the current thread, putting it into a “sleep” state, and schedules execution of another thread.
a = Thread.new { print "a"; Thread.stop; print "c" } sleep 0.1 while a.status!='sleep' print "b" a.run a.join #=> "abc"
Returns an array of Thread
objects for all threads that are either runnable or stopped.
Thread.new { sleep(200) } Thread.new { 1000000.times {|i| i*i } } Thread.new { Thread.stop } Thread.list.each {|t| p t}
This will produce:
#<Thread:0x401b3e84 sleep> #<Thread:0x401b3f38 run> #<Thread:0x401b3fb0 sleep> #<Thread:0x401bdf4c run>
Returns the status of thr
.
"sleep"
Returned if this thread is sleeping or waiting on I/O
"run"
When this thread is executing
"aborting"
If this thread is aborting
false
When this thread is terminated normally
nil
If terminated with an exception.
a = Thread.new { raise("die now") } b = Thread.new { Thread.stop } c = Thread.new { Thread.exit } d = Thread.new { sleep } d.kill #=> #<Thread:0x401b3678 aborting> a.status #=> nil b.status #=> "sleep" c.status #=> false d.status #=> "aborting" Thread.current.status #=> "run"
Returns true
if thr
is dead or sleeping.
a = Thread.new { Thread.stop } b = Thread.current a.stop? #=> true b.stop? #=> false
Returns the current backtrace of the target thread.
Returns internal information of TracePoint
.
The contents of the returned value are implementation specific. It may be changed in future.
This method is only for debugging TracePoint
itself.
A convenience method for TracePoint.new
, that activates the trace automatically.
trace = TracePoint.trace(:call) { |tp| [tp.lineno, tp.event] } #=> #<TracePoint:enabled> trace.enabled? #=> true
Performs a test on one or both of the filesystem entities at the given paths path0
and path1
:
Each path path0
or path1
points to a file, directory, device, pipe, etc.
Character char
selects a specific test.
The tests:
Each of these tests operates only on the entity at path0
, and returns true
or false
; for a non-existent entity, returns false
(does not raise exception):
Character | Test |
---|---|
'b' |
Whether the entity is a block device. |
'c' |
Whether the entity is a character device. |
'd' |
Whether the entity is a directory. |
'e' |
Whether the entity is an existing entity. |
'f' |
Whether the entity is an existing regular file. |
'g' |
Whether the entity's setgid bit is set. |
'G' |
Whether the entity's group ownership is equal to the caller's. |
'k' |
Whether the entity's sticky bit is set. |
'l' |
Whether the entity is a symbolic link. |
'o' |
Whether the entity is owned by the caller's effective uid. |
'O' |
Like 'o' , but uses the real uid (not the effective uid). |
'p' |
Whether the entity is a FIFO device (named pipe). |
'r' |
Whether the entity is readable by the caller's effecive uid/gid. |
'R' |
Like 'r' , but uses the real uid/gid (not the effective uid/gid). |
'S' |
Whether the entity is a socket. |
'u' |
Whether the entity's setuid bit is set. |
'w' |
Whether the entity is writable by the caller's effective uid/gid. |
'W' |
Like 'w' , but uses the real uid/gid (not the effective uid/gid). |
'x' |
Whether the entity is executable by the caller's effective uid/gid. |
'X' |
Like 'x' , but uses the real uid/gid (not the effecive uid/git). |
'z' |
Whether the entity exists and is of length zero. |
This test operates only on the entity at path0
, and returns an integer size or nil
:
Character | Test |
---|---|
's' |
Returns positive integer size if the entity exists and has non-zero length, nil otherwise. |
Each of these tests operates only on the entity at path0
, and returns a Time
object; raises an exception if the entity does not exist:
Character | Test |
---|---|
'A' |
Last access time for the entity. |
'C' |
Last change time for the entity. |
'M' |
Last modification time for the entity. |
Each of these tests operates on the modification time (mtime
) of each of the entities at path0
and path1
, and returns a true
or false
; returns false
if either entity does not exist:
Character | Test |
---|---|
'<' |
Whether the 'mtime` at `path0` is less than that at `path1`. |
'=' |
Whether the 'mtime` at `path0` is equal to that at `path1`. |
'>' |
Whether the 'mtime` at `path0` is greater than that at `path1`. |
This test operates on the content of each of the entities at path0
and path1
, and returns a true
or false
; returns false
if either entity does not exist:
Character | Test |
---|---|
'-' |
Whether the entities exist and are identical. |
Equivalent to:
io.write(sprintf(format_string, *objects))
For details on format_string
, see Format Specifications.
With the single argument format_string
, formats objects
into the string, then writes the formatted string to $stdout:
printf('%4.4d %10s %2.2f', 24, 24, 24.0)
Output (on $stdout):
0024 24 24.00#
With arguments io
and format_string
, formats objects
into the string, then writes the formatted string to io
:
printf($stderr, '%4.4d %10s %2.2f', 24, 24, 24.0)
Output (on $stderr):
0024 24 24.00# => nil
With no arguments, does nothing.