Now you can use obj.in?(array) instead of array.include?(obj)

I just wrote a tiny gem called in_enumerable. It extends Ruby’s Object type with the tasty ‘in?’ method, which returns true if an object is included in a list or other enumerable value.

So you can do this:


1.in? [1,2]          # => true
3.in? [1,2]          # => false

instead of the slightly more awkward:


[1,2].include?(1)    # => true
[1,2].include?(3)    # => false

Despite its name, in_enumerable doesn’t require an enumerable type.  It uses duck typing to work with any type that has an ‘include?’ method, such as Array, Hash, String, Range, Set, and even Module.

Installation


gem install in_enumerable

Usage


require 'rubygems'
require 'in_enumerable'

Side Note

I noticed that typing ri Object#in? reveals that a similar method supposedly already exists on the Object type. And if you google it you can find the source code for it. (It’s almost the same as mine.) But it doesn’t actually seem to be implemented, at least not in Ruby 1.8.6. So I made my own.

2 Cool Ways to Call Google Analytics from Rails Apps

I Like Stuff That’s Tracked

I just read two great suggestions for using Google Analytics to track page views in places where it might normally be hard:

  1. How to track a goal that doesn’t end in a unique page. E.g., where the goal is the “Show” page in a RESTful app, but you only want Google to track a goal conversion when you get there through the “create” action.
  2. How to track a goal that is reached via AJAX instead of via a full page refresh.