Most of the fanfare around Ruby 2.2 has focused on garbage collection (GC) upgrades. The GC will now clean up symbols and has a new incremental algorithm that reduces the pause time. These changes are very exciting, but there were some new methods added as well. Let’s see what other new toys 2.2 delivered.
Good stuff, Aaron. The surprise win for me was that you took me on a sidetrack to finally grok the === operator, which I’m embarrassed to say I hadn’t really understood until now.
You’ll find the slice_* methods used often when parsing text files, such as device configurations, where there are blocks of commands or settings that need to be grouped.
I’ve written several such device parsers, and slice_before made it almost trivial.
In general, yes. But as of ruby 1.9+, there is a shortcut for Enumerable#inject to only pass a symbol (which is nice, since this is the most common use for the method!).
In that particular example it’s serving the same purpose as self. The difference is that a binding can be passed around.
class A
def get_binding
binding
end
end
> a = A.new
# => #<A:0x007fe9b44cd880>
> b = a.get_binding
# => #<Binding:0x007fe9b44fcfe0>
> b.receiver
# => #<A:0x007fe9b44cd880>
Bindings are a window into a particular scope. Now it’s easier to find where that scope came from. Does that help clarify it?