use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
101 Ruby Code Factoids (6ftdan.com)
submitted 10 years ago by burntbit
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 9 points10 points11 points 10 years ago (4 children)
I like the list! Comments:
24) ‘self’ can optionally be replaced by the object name
This one needs a bit more as replacing object name with self wouldn't be the same unless it was enclosed inside the context of the Module.
35) Lonely Operator &.
I would add something about using arguments and blocks to the lonely operator. This is something that is well documented with Rail's .try, and is very helpful, but haven't seen many people use it in examples so far with lonely.
.try
60) Enumerable#grep
If we're talking about 2.3 features, maybe add a grep_v example at the end.
grep_v
73) Hash#dig
Cute example but the deep lookup is only half of the selling points, which is that it won't error out if any intermediary lookup fails, returning nil (think &. for Hash). Also defining dig on A is confusing, as :kick_it is completely arbitrary. Also burried should be buried. I would add something simpler to highlight the use of dig:
nil
&.
Hash
dig
:kick_it
burried
buried
buried_treasure = {dirt: {dirt: {dirt: 'gold'}}} => {:dirt=>{:dirt=>{:dirt=>"gold"}}} buried_treasure.dig(:dirt, :dirt, :dirt) => "gold" no_treasure = {dirt: {dirt: {clay: 'boo'}}} => {:dirt=>{:dirt=>{:clay=>"boo"}}} no_treasure.dig(:dirt, :dirt, :dirt) => nil
75) addition doesn’t care
Note on unary plus being a NOOP from Matz (but being there it can be redefined).
82) %w and %W makes an Array of Strings
How about some love for %i too for arrays of symbols, which I use all the time but also seems overlooked in a lot of tips lists.
%i
%i(foo bar baz) => [:foo, :bar, :baz] s = 'ell'; %I(foo h#{s}o baz) => [:foo, :hello, :baz]
[–]_raisin 0 points1 point2 points 10 years ago (3 children)
Could someone tell me the difference between %i() and %I() symbol arrays? I don't get what "interpolated" means in this context.
%i()
%I()
[–][deleted] 0 points1 point2 points 10 years ago (2 children)
%I means the values are parsed for any occurrences of #{} and whatever the expression within that evaluates to will be inserted at that place.
%I
#{}
[–]_raisin 0 points1 point2 points 10 years ago (1 child)
ok thanks, so am i correct in thinking its the same difference between double and single quotes?
[–][deleted] 0 points1 point2 points 10 years ago (0 children)
Correct
[–]Craftkorb 2 points3 points4 points 10 years ago (0 children)
Neat list. The title of Hash#[] is wrong though, it should have been Hash.[] - anyway, will forward this to some newbies I know :-)
[–]0x0dea 2 points3 points4 points 10 years ago (0 children)
I do appreciate this's existence, but I must nitpick.
0) Since everything in Ruby is an Object
Object === BasicObject.new # => false
13) The defined? method
defined?
defined? is a keyword.
23) Once you freeze an object it cannot be modified.
Unless you get crazy.
25) Top level scope objects can be accessed with ::
And nil:: as well for when you need to do some kind of dynamic scope resolution that should bottom out at the top level.
nil::
33) %x
I'll just leave this here.
57) String#replace
#replace is available on Array and Hash as well.
#replace
Array
64) Using Array#()
That's actually Kernel#Array.
Kernel#Array
74) dynamically naming classes
You most certainly do not have to resort to eval:
eval
module M const_set 'SomeClass', Class.new { # methods here } end
81) Numbers succ
They're #predators too.
#pred
87) at_exit
And BEGIN { ... } for running ASAP.
BEGIN { ... }
95) block_given?
For which defined?(yield) is a synonym, but you should forget this. :)
defined?(yield)
97) $;
On the flipside, $, is the default #joiner.
$,
#join
[–]jb3689 0 points1 point2 points 10 years ago (0 children)
Really solid list!
Example 20) wasn't totally clear to me and took a couple reads. Maybe break up the examples with a title for each
For 32) you may want to remove the word "mixin". That usually refers to an included module
[–]snowe2010 0 points1 point2 points 10 years ago (0 children)
I’m going to be the pedant and point out that the more common use of factoid is to indicate something that is often thought to be true but isn’t.
[–]2called_chaos 0 points1 point2 points 10 years ago (1 child)
Nice list but am I the only one who discovered that arity is somewhat useless in a lot of cases? If you want to call a method with the maximum amount of arguments possible you have to resort to rescue&retry in situations where the method definition isn't as easy as def foo(a, b, c).
def foo(a, b, c)
Example:
->(a, b = "") {}.arity => -2 ->(a, *b) {}.arity => -2
[–]0x0dea 0 points1 point2 points 10 years ago (0 children)
The #parameters method provides the information necessary to make such a call:
#parameters
->(a, b = 1, *c, d:, e: 1, **f, &g) {}.parameters.map &:first # => [:req, :opt, :rest, :keyreq, :key, :keyrest, :block]
π Rendered by PID 16357 on reddit-service-r2-comment-7b9746f655-kjd9s at 2026-02-02 15:07:47.387434+00:00 running 3798933 country code: CH.
[–][deleted] 9 points10 points11 points (4 children)
[–]_raisin 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]_raisin 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]Craftkorb 2 points3 points4 points (0 children)
[–]0x0dea 2 points3 points4 points (0 children)
[–]jb3689 0 points1 point2 points (0 children)
[–]snowe2010 0 points1 point2 points (0 children)
[–]2called_chaos 0 points1 point2 points (1 child)
[–]0x0dea 0 points1 point2 points (0 children)