The DidYouMean
gem adds functionality to suggest possible method/class names upon errors such as NameError
and NoMethodError
. In Ruby 2.3 or later, it is automatically activated during startup.
@example
methosd # => NameError: undefined local variable or method `methosd' for main:Object # Did you mean? methods # method OBject # => NameError: uninitialized constant OBject # Did you mean? Object @full_name = "Yuki Nishijima" first_name, last_name = full_name.split(" ") # => NameError: undefined local variable or method `full_name' for main:Object # Did you mean? @full_name @@full_name = "Yuki Nishijima" @@full_anme # => NameError: uninitialized class variable @@full_anme in Object # Did you mean? @@full_name full_name = "Yuki Nishijima" full_name.starts_with?("Y") # => NoMethodError: undefined method `starts_with?' for "Yuki Nishijima":String # Did you mean? start_with? hash = {foo: 1, bar: 2, baz: 3} hash.fetch(:fooo) # => KeyError: key not found: :fooo # Did you mean? :foo
did_you_mean
Occasionally, you may want to disable the did_you_mean
gem for e.g. debugging issues in the error object itself. You can disable it entirely by specifying --disable-did_you_mean
option to the ruby
command:
$ ruby --disable-did_you_mean -e "1.zeor?" -e:1:in `<main>': undefined method `zeor?' for 1:Integer (NameError)
When you do not have direct access to the ruby
command (e.g. +rails console+, irb
), you could applyoptions using the RUBYOPT
environment variable:
$ RUBYOPT='--disable-did_you_mean' irb irb:0> 1.zeor? # => NoMethodError (undefined method `zeor?' for 1:Integer)
Sometimes, you do not want to disable the gem entirely, but need to get the original error message without suggestions (e.g. testing). In this case, you could use the original_message method on the error object:
no_method_error = begin 1.zeor? rescue NoMethodError => error error end no_method_error.message # => NoMethodError (undefined method `zeor?' for 1:Integer) # Did you mean? zero? no_method_error.original_message # => NoMethodError (undefined method `zeor?' for 1:Integer)
Timeout
long-running blocks
require 'timeout' status = Timeout.timeout(5) { # Something that should be interrupted if it takes more than 5 seconds... }
Timeout
provides a way to auto-terminate a potentially long-running operation if it hasn’t finished in a fixed amount of time.
© 2000 Network Applied Communication Laboratory, Inc.
© 2000 Information-technology Promotion Agency, Japan
YAML
Ain’t Markup Language
This module provides a Ruby interface for data serialization in YAML
format.
The YAML
module is an alias of Psych
, the YAML
engine for Ruby.
Working with YAML
can be very simple, for example:
require 'yaml' # Parse a YAML string YAML.load("--- foo") #=> "foo" # Emit some YAML YAML.dump("foo") # => "--- foo\n...\n" { :a => 'b'}.to_yaml # => "---\n:a: b\n"
As the implementation is provided by the Psych
library, detailed documentation can be found in that library’s docs (also part of standard library).
Do not use YAML
to load untrusted data. Doing so is unsafe and could allow malicious input to execute arbitrary code inside your application. Please see doc/security.rdoc for more information.
Syck was the original YAML
implementation in Ruby’s standard library developed by why the lucky stiff.
You can still use Syck, if you prefer, for parsing and emitting YAML
, but you must install the ‘syck’ gem now in order to use it.
In older Ruby versions, ie. <= 1.9, Syck is still provided, however it was completely removed with the release of Ruby 2.0.0.
For more advanced details on the implementation see Psych
, and also check out yaml.org for spec details and other helpful information.
Psych
is maintained by Aaron Patterson on github: github.com/ruby/psych
Syck can also be found on github: github.com/ruby/syck
Many operating systems allow signals to be sent to running processes. Some signals have a defined effect on the process, while others may be trapped at the code level and acted upon. For example, your process may trap the USR1 signal and use it to toggle debugging, and may use TERM to initiate a controlled shutdown.
pid = fork do Signal.trap("USR1") do $debug = !$debug puts "Debug now: #$debug" end Signal.trap("TERM") do puts "Terminating..." shutdown() end # . . . do some work . . . end Process.detach(pid) # Controlling program: Process.kill("USR1", pid) # ... Process.kill("USR1", pid) # ... Process.kill("TERM", pid)
produces:
Debug now: true Debug now: false Terminating...
The list of available signal names and their interpretation is system dependent. Signal
delivery semantics may also vary between systems; in particular signal delivery may not always be reliable.
Enumerator::ArithmeticSequence
is a subclass of Enumerator
, that is a representation of sequences of numbers with common difference. Instances of this class can be generated by the Range#step
and Numeric#step
methods.
The class can be used for slicing Array
(see Array#slice
) or custom collections.
Raised by Encoding
and String
methods when the source encoding is incompatible with the target encoding.
WIN32OLE::Method
objects represent OLE method information.
WIN32OLE::Param
objects represent param information of the OLE method.
Subclass of Zlib::Error
When zlib returns a Z_STREAM_END is return if the end of the compressed data has been reached and all uncompressed out put has been produced.
Subclass of Zlib::Error
When zlib returns a Z_STREAM_ERROR, usually if the stream state was inconsistent.
Subclass of Zlib::Error
When zlib returns a Z_MEM_ERROR, usually if there was not enough memory.
A custom InputMethod class used by XMP
for evaluating string io.
Response class for Unauthorized
responses (status code 401).
Authentication is required, but either was not provided or failed.
References:
Response class for Payment Required
responses (status code 402).
Reserved for future use.
References:
Response class for Method Not Allowed
responses (status code 405).
The request method is not supported for the requested resource.
References:
Response class for Proxy Authentication Required
responses (status code 407).
The client must first authenticate itself with the proxy.
References:
Response class for Unsupported Media Type
responses (status code 415).
The request entity has a media type which the server or resource does not support.
References:
Response class for Gateway Timeout
responses (status code 504).
The server was acting as a gateway or proxy and did not receive a timely response from the upstream server.
References:
Response class for Gateway Timeout
responses (status code 504).
The server was acting as a gateway or proxy and did not receive a timely response from the upstream server.
References:
Response class for Network Authentication Required
responses (status code 511).
The client needs to authenticate to gain network access.
References:
Raises when the given argument word can’t be completed uniquely.
The dispatcher class fires events for nodes that are found while walking an AST to all registered listeners. It’s useful for performing different types of analysis on the AST while only having to walk the tree once.
To use the dispatcher, you would first instantiate it and register listeners for the events you’re interested in:
class OctalListener def on_integer_node_enter(node) if node.octal? && !node.slice.start_with?("0o") warn("Octal integers should be written with the 0o prefix") end end end listener = OctalListener.new dispatcher = Prism::Dispatcher.new dispatcher.register(listener, :on_integer_node_enter)
Then, you can walk any number of trees and dispatch events to the listeners:
result = Prism.parse("001 + 002 + 003") dispatcher.dispatch(result.value)
Optionally, you can also use ‘#dispatch_once` to dispatch enter and leave events for a single node without recursing further down the tree. This can be useful in circumstances where you want to reuse the listeners you already have registers but want to stop walking the tree at a certain point.
integer = result.value.statements.body.first.receiver.receiver dispatcher.dispatch_once(integer)
Represents the use of the ‘alias` keyword to alias a method.
alias foo bar ^^^^^^^^^^^^^
Represents an array pattern in pattern matching.
foo in 1, 2 ^^^^^^^^^^^ foo in [1, 2] ^^^^^^^^^^^^^ foo in *bar ^^^^^^^^^^^ foo in Bar[] ^^^^^^^^^^^^ foo in Bar[1, 2, 3] ^^^^^^^^^^^^^^^^^^^