Precedence of Ruby operators
[ ] [ ]=
**
! ~ + -
* / %
+ -
>> <<
&
^ |
<= < > >=
<=> == === != =~ !~
&&
||
.. ...
? :
= %= { /= -= += |= &= >>= <<= *= &&= ||= **=
defined?
not
or and
if unless while until
begin/end
For more information see Table 18.4 Show archive.org snapshot in The Pragmatic Programmer's Guide Show archive.org snapshot .
Related cards:
Use the "retry" keyword to process a piece of Ruby code again.
Imagine you have a piece of code that tries to send a request to a remote server. Now the server is temporarily not available and raises an exception. In order to re-send the request you could use the following snippet:
def remote_request
...
Ruby bug: Symbolized Strings Break Keyword Arguments in Ruby 2.2
TL;DR Under certain circumstances, dynamically defined symbols may break keyword arguments in Ruby 2.2. This was fixed in Ruby 2.2.3 and 2.3.
Specifically, when …
- there is a method with several keyword arguments and a double-splat a...
Google Summer of Code winner: ActiveModel for Ruby on Rails
Finish the remainder of the ActiveModel todo list (observers, callbacks, validations, scoping, and serialization) in addition to associations. Also wire up ActiveModel up to ActiveRecord and ActiveResource.
Running external commands with Open3
There are various ways to run external commands from within Ruby, but the most powerful ones are Open3.capture3
and Open3.popen3
. Since those can do almost everything you would possibly need in a clean way, I prefer to simply...
Heads up: Ruby implicitly converts a hash to keyword arguments
When a method has keyword arguments, Ruby offers implicit conversion of a Hash
argument into keyword arguments. This conversion is perfo...
Changes to positional and keyword args in Ruby 3.0
Ruby 3.0 introduced a breaking change in how it treats keyword arguments.
There is an excellent blog post on the official Ruby blog going ...
Ruby: A small summary of what return, break and next means for blocks
Summary
- Use
return
to return from a method.return
accepts a value that will be the return value of the method call. - Use
break
to quit from a block and from the method that yielded to the block.break
accepts a value that suppl...
The many gotchas of Ruby class variables
TLDR: Ruby class variables (@@foo
) are dangerous in many ways. You should avoid them at all cost. See bottom of this card for alternatives.
Class variables are shared between a class hierarchy
---------------------------------------------------...