Returns array of WIN32OLE_VARIABLE
objects which represent variables defined in OLE class.
tobj = WIN32OLE_TYPE.new('Microsoft Excel 9.0 Object Library', 'XlSheetType') vars = tobj.variables vars.each do |v| puts "#{v.name} = #{v.value}" end The result of above sample script is follows: xlChart = -4109 xlDialogSheet = -4116 xlExcel4IntlMacroSheet = 4 xlExcel4MacroSheet = 3 xlWorksheet = -4167
Writes string if inplace mode.
Writes the given object(s) to ios. Returns nil
.
The stream must be opened for writing. Each given object that isn’t a string will be converted by calling its to_s
method. When called without arguments, prints the contents of $_
.
If the output field separator ($,
) is not nil
, it is inserted between objects. If the output record separator ($\
) is not nil
, it is appended to the output.
$stdout.print("This is ", 100, " percent.\n")
produces:
This is 100 percent.
Formats and writes to ios, converting parameters under control of the format string. See Kernel#sprintf
for details.
Set
the handling of the ordering of options and arguments. A RuntimeError
is raised if option processing has already started.
The supplied value must be a member of GetoptLong::ORDERINGS
. It alters the processing of options as follows:
REQUIRE_ORDER :
Options are required to occur before non-options.
Processing of options ends as soon as a word is encountered that has not been preceded by an appropriate option flag.
For example, if -a and -b are options which do not take arguments, parsing command line arguments of ‘-a one -b two’ would result in ‘one’, ‘-b’, ‘two’ being left in ARGV, and only (‘-a’, ”) being processed as an option/arg pair.
This is the default ordering, if the environment variable POSIXLY_CORRECT is set. (This is for compatibility with GNU getopt_long.)
PERMUTE :
Options can occur anywhere in the command line parsed. This is the default behavior.
Every sequence of words which can be interpreted as an option (with or without argument) is treated as an option; non-option words are skipped.
For example, if -a does not require an argument and -b optionally takes an argument, parsing ‘-a one -b two three’ would result in (‘-a’,”) and (‘-b’, ‘two’) being processed as option/arg pairs, and ‘one’,‘three’ being left in ARGV.
If the ordering is set to PERMUTE but the environment variable POSIXLY_CORRECT is set, REQUIRE_ORDER is used instead. This is for compatibility with GNU getopt_long.
RETURN_IN_ORDER :
All words on the command line are processed as options. Words not preceded by a short or long option flag are passed as arguments with an option of ” (empty string).
For example, if -a requires an argument but -b does not, a command line of ‘-a one -b two three’ would result in option/arg pairs of (‘-a’, ‘one’) (‘-b’, ”), (”, ‘two’), (”, ‘three’) being processed.
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.
Returns true
if this is a symmetric matrix. Raises an error if matrix is not square.
Returns true
if this is an antisymmetric matrix. Raises an error if matrix is not square.
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.
Returns the array of captures; equivalent to mtch.to_a[1..-1]
.
f1,f2,f3,f4 = /(.)(.)(\d+)(\d)/.match("THX1138.").captures f1 #=> "H" f2 #=> "X" f3 #=> "113" f4 #=> "8"
Returns a frozen copy of the string passed in to match
.
m = /(.)(.)(\d+)(\d)/.match("THX1138.") m.string #=> "THX1138."
Returns true if value
is a prime number, else returns false. Integer#prime?
is much more performant.
value
an arbitrary integer to be checked.
generator
optional. A pseudo-prime generator.
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 a curried proc. If the optional arity argument is given, it determines the number of arguments. A curried proc receives some arguments. If a sufficient number of arguments are supplied, it passes the supplied arguments to the original proc and returns the result. Otherwise, returns another curried proc that takes the rest of arguments.
b = proc {|x, y, z| (x||0) + (y||0) + (z||0) } p b.curry[1][2][3] #=> 6 p b.curry[1, 2][3, 4] #=> 6 p b.curry(5)[1][2][3][4][5] #=> 6 p b.curry(5)[1, 2][3, 4][5] #=> 6 p b.curry(1)[1] #=> 1 b = proc {|x, y, z, *w| (x||0) + (y||0) + (z||0) + w.inject(0, &:+) } p b.curry[1][2][3] #=> 6 p b.curry[1, 2][3, 4] #=> 10 p b.curry(5)[1][2][3][4][5] #=> 15 p b.curry(5)[1, 2][3, 4][5] #=> 15 p b.curry(1)[1] #=> 1 b = lambda {|x, y, z| (x||0) + (y||0) + (z||0) } p b.curry[1][2][3] #=> 6 p b.curry[1, 2][3, 4] #=> wrong number of arguments (given 4, expected 3) p b.curry(5) #=> wrong number of arguments (given 5, expected 3) p b.curry(1) #=> wrong number of arguments (given 1, expected 3) b = lambda {|x, y, z, *w| (x||0) + (y||0) + (z||0) + w.inject(0, &:+) } p b.curry[1][2][3] #=> 6 p b.curry[1, 2][3, 4] #=> 10 p b.curry(5)[1][2][3][4][5] #=> 15 p b.curry(5)[1, 2][3, 4][5] #=> 15 p b.curry(1) #=> wrong number of arguments (given 1, expected 3) b = proc { :foo } p b.curry[] #=> :foo
Returns a curried proc based on the method. When the proc is called with a number of arguments that is lower than the method’s arity, then another curried proc is returned. Only when enough arguments have been supplied to satisfy the method signature, will the method actually be called.
The optional arity argument should be supplied when currying methods with variable arguments to determine how many arguments are needed before the method is called.
def foo(a,b,c) [a, b, c] end proc = self.method(:foo).curry proc2 = proc.call(1, 2) #=> #<Proc> proc2.call(3) #=> [1,2,3] def vararg(*args) args end proc = self.method(:vararg).curry(4) proc2 = proc.call(:x) #=> #<Proc> proc3 = proc2.call(:y, :z) #=> #<Proc> proc3.call(:a) #=> [:x, :y, :z, :a]
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 a string, using platform providing features. Returned value is expected to be a cryptographically secure pseudo-random number in binary form. This method raises a RuntimeError
if the feature provided by platform failed to prepare the result.
In 2017, Linux manpage random(7) writes that “no cryptographic primitive available today can hope to promise more than 256 bits of security”. So it might be questionable to pass size > 32 to this method.
Random.urandom(8) #=> "\x78\x41\xBA\xAF\x7D\xEA\xD8\xEA"
Equivalent to:
io.write(sprintf(string, obj, ...))
or
$stdout.write(sprintf(string, obj, ...))
Prints each object in turn to $stdout
. If the output field separator ($,
) is not nil
, its contents will appear between each field. If the output record separator ($\
) is not nil
, it will be appended to the output. If no arguments are given, prints $_
. Objects that aren’t strings will be converted by calling their to_s
method.
print "cat", [1,2,3], 99, "\n" $, = ", " $\ = "\n" print "cat", [1,2,3], 99
produces:
cat12399 cat, 1, 2, 3, 99
Returns the string resulting from applying format_string to any additional arguments. Within the format string, any characters other than format sequences are copied to the result.
The syntax of a format sequence is as follows.
%[flags][width][.precision]type
A format sequence consists of a percent sign, followed by optional flags, width, and precision indicators, then terminated with a field type character. The field type controls how the corresponding sprintf
argument is to be interpreted, while the flags modify that interpretation.
The field type characters are:
Field | Integer Format ------+-------------------------------------------------------------- b | Convert argument as a binary number. | Negative numbers will be displayed as a two's complement | prefixed with `..1'. B | Equivalent to `b', but uses an uppercase 0B for prefix | in the alternative format by #. d | Convert argument as a decimal number. i | Identical to `d'. o | Convert argument as an octal number. | Negative numbers will be displayed as a two's complement | prefixed with `..7'. u | Identical to `d'. x | Convert argument as a hexadecimal number. | Negative numbers will be displayed as a two's complement | prefixed with `..f' (representing an infinite string of | leading 'ff's). X | Equivalent to `x', but uses uppercase letters. Field | Float Format ------+-------------------------------------------------------------- e | Convert floating point argument into exponential notation | with one digit before the decimal point as [-]d.dddddde[+-]dd. | The precision specifies the number of digits after the decimal | point (defaulting to six). E | Equivalent to `e', but uses an uppercase E to indicate | the exponent. f | Convert floating point argument as [-]ddd.dddddd, | where the precision specifies the number of digits after | the decimal point. g | Convert a floating point number using exponential form | if the exponent is less than -4 or greater than or | equal to the precision, or in dd.dddd form otherwise. | The precision specifies the number of significant digits. G | Equivalent to `g', but use an uppercase `E' in exponent form. a | Convert floating point argument as [-]0xh.hhhhp[+-]dd, | which is consisted from optional sign, "0x", fraction part | as hexadecimal, "p", and exponential part as decimal. A | Equivalent to `a', but use uppercase `X' and `P'. Field | Other Format ------+-------------------------------------------------------------- c | Argument is the numeric code for a single character or | a single character string itself. p | The valuing of argument.inspect. s | Argument is a string to be substituted. If the format | sequence contains a precision, at most that many characters | will be copied. % | A percent sign itself will be displayed. No argument taken.
The flags modifies the behavior of the formats. The flag characters are:
Flag | Applies to | Meaning ---------+---------------+----------------------------------------- space | bBdiouxX | Leave a space at the start of | aAeEfgG | non-negative numbers. | (numeric fmt) | For `o', `x', `X', `b' and `B', use | | a minus sign with absolute value for | | negative values. ---------+---------------+----------------------------------------- (digit)$ | all | Specifies the absolute argument number | | for this field. Absolute and relative | | argument numbers cannot be mixed in a | | sprintf string. ---------+---------------+----------------------------------------- # | bBoxX | Use an alternative format. | aAeEfgG | For the conversions `o', increase the precision | | until the first digit will be `0' if | | it is not formatted as complements. | | For the conversions `x', `X', `b' and `B' | | on non-zero, prefix the result with ``0x'', | | ``0X'', ``0b'' and ``0B'', respectively. | | For `a', `A', `e', `E', `f', `g', and 'G', | | force a decimal point to be added, | | even if no digits follow. | | For `g' and 'G', do not remove trailing zeros. ---------+---------------+----------------------------------------- + | bBdiouxX | Add a leading plus sign to non-negative | aAeEfgG | numbers. | (numeric fmt) | For `o', `x', `X', `b' and `B', use | | a minus sign with absolute value for | | negative values. ---------+---------------+----------------------------------------- - | all | Left-justify the result of this conversion. ---------+---------------+----------------------------------------- 0 (zero) | bBdiouxX | Pad with zeros, not spaces. | aAeEfgG | For `o', `x', `X', `b' and `B', radix-1 | (numeric fmt) | is used for negative numbers formatted as | | complements. ---------+---------------+----------------------------------------- * | all | Use the next argument as the field width. | | If negative, left-justify the result. If the | | asterisk is followed by a number and a dollar | | sign, use the indicated argument as the width.
Examples of flags:
# `+' and space flag specifies the sign of non-negative numbers. sprintf("%d", 123) #=> "123" sprintf("%+d", 123) #=> "+123" sprintf("% d", 123) #=> " 123" # `#' flag for `o' increases number of digits to show `0'. # `+' and space flag changes format of negative numbers. sprintf("%o", 123) #=> "173" sprintf("%#o", 123) #=> "0173" sprintf("%+o", -123) #=> "-173" sprintf("%o", -123) #=> "..7605" sprintf("%#o", -123) #=> "..7605" # `#' flag for `x' add a prefix `0x' for non-zero numbers. # `+' and space flag disables complements for negative numbers. sprintf("%x", 123) #=> "7b" sprintf("%#x", 123) #=> "0x7b" sprintf("%+x", -123) #=> "-7b" sprintf("%x", -123) #=> "..f85" sprintf("%#x", -123) #=> "0x..f85" sprintf("%#x", 0) #=> "0" # `#' for `X' uses the prefix `0X'. sprintf("%X", 123) #=> "7B" sprintf("%#X", 123) #=> "0X7B" # `#' flag for `b' add a prefix `0b' for non-zero numbers. # `+' and space flag disables complements for negative numbers. sprintf("%b", 123) #=> "1111011" sprintf("%#b", 123) #=> "0b1111011" sprintf("%+b", -123) #=> "-1111011" sprintf("%b", -123) #=> "..10000101" sprintf("%#b", -123) #=> "0b..10000101" sprintf("%#b", 0) #=> "0" # `#' for `B' uses the prefix `0B'. sprintf("%B", 123) #=> "1111011" sprintf("%#B", 123) #=> "0B1111011" # `#' for `e' forces to show the decimal point. sprintf("%.0e", 1) #=> "1e+00" sprintf("%#.0e", 1) #=> "1.e+00" # `#' for `f' forces to show the decimal point. sprintf("%.0f", 1234) #=> "1234" sprintf("%#.0f", 1234) #=> "1234." # `#' for `g' forces to show the decimal point. # It also disables stripping lowest zeros. sprintf("%g", 123.4) #=> "123.4" sprintf("%#g", 123.4) #=> "123.400" sprintf("%g", 123456) #=> "123456" sprintf("%#g", 123456) #=> "123456."
The field width is an optional integer, followed optionally by a period and a precision. The width specifies the minimum number of characters that will be written to the result for this field.
Examples of width:
# padding is done by spaces, width=20 # 0 or radix-1. <------------------> sprintf("%20d", 123) #=> " 123" sprintf("%+20d", 123) #=> " +123" sprintf("%020d", 123) #=> "00000000000000000123" sprintf("%+020d", 123) #=> "+0000000000000000123" sprintf("% 020d", 123) #=> " 0000000000000000123" sprintf("%-20d", 123) #=> "123 " sprintf("%-+20d", 123) #=> "+123 " sprintf("%- 20d", 123) #=> " 123 " sprintf("%020x", -123) #=> "..ffffffffffffffff85"
For numeric fields, the precision controls the number of decimal places displayed. For string fields, the precision determines the maximum number of characters to be copied from the string. (Thus, the format sequence %10.10s
will always contribute exactly ten characters to the result.)
Examples of precisions:
# precision for `d', 'o', 'x' and 'b' is # minimum number of digits <------> sprintf("%20.8d", 123) #=> " 00000123" sprintf("%20.8o", 123) #=> " 00000173" sprintf("%20.8x", 123) #=> " 0000007b" sprintf("%20.8b", 123) #=> " 01111011" sprintf("%20.8d", -123) #=> " -00000123" sprintf("%20.8o", -123) #=> " ..777605" sprintf("%20.8x", -123) #=> " ..ffff85" sprintf("%20.8b", -11) #=> " ..110101" # "0x" and "0b" for `#x' and `#b' is not counted for # precision but "0" for `#o' is counted. <------> sprintf("%#20.8d", 123) #=> " 00000123" sprintf("%#20.8o", 123) #=> " 00000173" sprintf("%#20.8x", 123) #=> " 0x0000007b" sprintf("%#20.8b", 123) #=> " 0b01111011" sprintf("%#20.8d", -123) #=> " -00000123" sprintf("%#20.8o", -123) #=> " ..777605" sprintf("%#20.8x", -123) #=> " 0x..ffff85" sprintf("%#20.8b", -11) #=> " 0b..110101" # precision for `e' is number of # digits after the decimal point <------> sprintf("%20.8e", 1234.56789) #=> " 1.23456789e+03" # precision for `f' is number of # digits after the decimal point <------> sprintf("%20.8f", 1234.56789) #=> " 1234.56789000" # precision for `g' is number of # significant digits <-------> sprintf("%20.8g", 1234.56789) #=> " 1234.5679" # <-------> sprintf("%20.8g", 123456789) #=> " 1.2345679e+08" # precision for `s' is # maximum number of characters <------> sprintf("%20.8s", "string test") #=> " string t"
Examples:
sprintf("%d %04x", 123, 123) #=> "123 007b" sprintf("%08b '%4s'", 123, 123) #=> "01111011 ' 123'" sprintf("%1$*2$s %2$d %1$s", "hello", 8) #=> " hello 8 hello" sprintf("%1$*2$s %2$d", "hello", -8) #=> "hello -8" sprintf("%+g:% g:%-g", 1.23, 1.23, 1.23) #=> "+1.23: 1.23:1.23" sprintf("%u", -123) #=> "-123"
For more complex formatting, Ruby supports a reference by name. %<name>s style uses format style, but %{name} style doesn’t.
Examples:
sprintf("%<foo>d : %<bar>f", { :foo => 1, :bar => 2 }) #=> 1 : 2.000000 sprintf("%{foo}f", { :foo => 1 }) # => "1f"
Returns arg as a String
.
First tries to call its to_str
method, then its to_s
method.
String(self) #=> "main" String(self.class) #=> "Object" String(123456) #=> "123456"