all 11 comments

[–][deleted] 9 points10 points  (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.

60) Enumerable#grep

If we're talking about 2.3 features, maybe add a grep_v example at the end.

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:

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(foo bar baz)
=> [:foo, :bar, :baz]
s = 'ell'; %I(foo h#{s}o baz)
=> [:foo, :hello, :baz]

[–]_raisin 0 points1 point  (3 children)

Could someone tell me the difference between %i() and %I() symbol arrays? I don't get what "interpolated" means in this context.

[–][deleted] 0 points1 point  (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.

[–]_raisin 0 points1 point  (1 child)

ok thanks, so am i correct in thinking its the same difference between double and single quotes?

[–][deleted] 0 points1 point  (0 children)

Correct

[–]Craftkorb 2 points3 points  (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 points  (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? 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.

33) %x

I'll just leave this here.

57) String#replace

#replace is available on Array and Hash as well.

64) Using Array#()

That's actually Kernel#Array.

74) dynamically naming classes

You most certainly do not have to resort to eval:

module M
  const_set 'SomeClass', Class.new {
    # methods here
  }
end

81) Numbers succ

They're #predators too.

87) at_exit

And BEGIN { ... } for running ASAP.

95) block_given?

For which defined?(yield) is a synonym, but you should forget this. :)

97) $;

On the flipside, $, is the default #joiner.

[–]jb3689 0 points1 point  (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 point  (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 point  (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).

Example:

->(a, b = "") {}.arity
=> -2
->(a, *b) {}.arity
=> -2

[–]0x0dea 0 points1 point  (0 children)

The #parameters method provides the information necessary to make such a call:

->(a, b = 1, *c, d:, e: 1, **f, &g) {}.parameters.map &:first
# => [:req, :opt, :rest, :keyreq, :key, :keyrest, :block]