Sets the process title that appears on the ps(1) command. Not necessarily effective on all platforms. No exception will be raised regardless of the result, nor will NotImplementedError
be raised even if the platform does not support the feature.
Calling this method does not affect the value of $0.
Process.setproctitle('myapp: worker #%d' % worker_id)
This method first appeared in Ruby 2.1 to serve as a global variable free means to change the process title.
Invoked as a callback whenever a singleton method is undefined in the receiver.
module Chatty def Chatty.singleton_method_undefined(id) puts "Undefining #{id.id2name}" end def Chatty.one() end class << self undef_method(:one) end end
produces:
Undefining one
Returns the current execution stack—an array containing backtrace location objects.
See Thread::Backtrace::Location
for more information.
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.
Returns a new array with the concatenated results of running block once for every element in enum.
If no block is given, an enumerator is returned instead.
[1, 2, 3, 4].flat_map { |e| [e, -e] } #=> [1, -1, 2, -2, 3, -3, 4, -4] [[1, 2], [3, 4]].flat_map { |e| e + [100] } #=> [1, 2, 100, 3, 4, 100]
Returns the source file origin from the given object
.
See ::trace_object_allocations
for more information and examples.
Specifies a Proc
object proc
to determine completion behavior. It should take input string and return an array of completion candidates.
The default completion is used if proc
is nil.
The String
that is passed to the Proc
depends on the Readline.completer_word_break_characters
property. By default the word under the cursor is passed to the Proc
. For example, if the input is “foo bar” then only “bar” would be passed to the completion Proc
.
Upon successful completion the Readline.completion_append_character
will be appended to the input so the user can start working on their next argument.
require 'readline' LIST = [ 'search', 'download', 'open', 'help', 'history', 'quit', 'url', 'next', 'clear', 'prev', 'past' ].sort comp = proc { |s| LIST.grep(/^#{Regexp.escape(s)}/) } Readline.completion_append_character = " " Readline.completion_proc = comp while line = Readline.readline('> ', true) p line end
require 'readline' Readline.completion_append_character = " " Readline.completion_proc = Proc.new do |str| Dir[str+'*'].grep(/^#{Regexp.escape(str)}/) end while line = Readline.readline('> ', true) p line end
When working with auto-complete there are some strategies that work well. To get some ideas you can take a look at the completion.rb file for irb.
The common strategy is to take a list of possible completions and filter it down to those completions that start with the user input. In the above examples Enumerator.grep
is used. The input is escaped to prevent Regexp
special characters from interfering with the matching.
It may also be helpful to use the Abbrev
library to generate completions.
Raises ArgumentError
if proc
does not respond to the call method.
Returns the completion Proc
object.
Enters exclusive section and executes the block. Leaves the exclusive section automatically when the block exits. See example under MonitorMixin
.
Returns the destination encoding as an encoding object.
Returns the destination encoding as an encoding object.
Returns the destination encoding as an Encoding
object.
Unmounts dir
Specifies a character to be appended on completion. Nothing will be appended if an empty string (“”) or nil is specified.
For example:
require "readline" Readline.readline("> ", true) Readline.completion_append_character = " "
Result:
> Input "/var/li". > /var/li Press TAB key. > /var/lib Completes "b" and appends " ". So, you can continuously input "/usr". > /var/lib /usr
NOTE: Only one character can be specified. When “string” is specified, sets only “s” that is the first.
require "readline" Readline.completion_append_character = "string" p Readline.completion_append_character # => "s"
Raises NotImplementedError
if the using readline library does not support.
Returns a string containing a character to be appended on completion. The default is a space (“ ”).
Raises NotImplementedError
if the using readline library does not support.
When called during a completion (e.g. from within your completion_proc
), it will return a string containing the character used to quote the argument being completed, or nil if the argument is unquoted.
When called at other times, it will always return nil.
Note that Readline.completer_quote_characters
must be set, or this method will always return nil.
Returns an array of the names of singleton methods for obj. If the optional all parameter is true, the list will include methods in modules included in obj. Only public and protected singleton methods are returned.
module Other def three() end end class Single def Single.four() end end a = Single.new def a.one() end class << a include Other def two() end end Single.singleton_methods #=> [:four] a.singleton_methods(false) #=> [:two, :one] a.singleton_methods #=> [:two, :one, :three]
Similar to method, searches singleton method only.
class Demo def initialize(n) @iv = n end def hello() "Hello, @iv = #{@iv}" end end k = Demo.new(99) def k.hi "Hi, @iv = #{@iv}" end m = k.singleton_method(:hi) m.call #=> "Hi, @iv = 99" m = k.singleton_method(:hello) #=> NameError
Hadamard product
Matrix[[1,2], [3,4]].hadamard_product(Matrix[[1,2], [3,2]]) => 1 4 9 8
Returns the inner product of this vector with the other.
Vector[4,7].inner_product Vector[10,1] => 47
Returns the cross product of this vector with the others.
Vector[1, 0, 0].cross_product Vector[0, 1, 0] => Vector[0, 0, 1]
It is generalized to other dimensions to return a vector perpendicular to the arguments.
Vector[1, 2].cross_product # => Vector[-2, 1] Vector[1, 0, 0, 0].cross_product( Vector[0, 1, 0, 0], Vector[0, 0, 1, 0] ) #=> Vector[0, 0, 0, 1]
Returns an Array
of method names which have the option opt
.
p FileUtils.collect_method(:preserve) #=> ["cp", "cp_r", "copy", "install"]