StackOverflow cool Ruby questions

I have participated heavily during the last 2 weeks in answering Ruby tagged questions on stackoverflow. That took place mainly after Ryan Bates tweet wishing to see more Ruby developers on stackoverflow. Since then I have enjoyed reading various kinds of questions that range in level from basic to professional ones and which cover various stuff like normal questions, tricks, meta programming, best practices and language specific features.

So, I'm announcing in this post an endless series called "Stackoverflow cool Ruby questions" which will target cool Ruby questions on stackoverflow. I strongly recommend that you visit Stackoverflow on daily basis and try to participate if you have the time, but if you don't, then I hope that you will enjoy this educational series.

There are currently more than 4,640 tagged Ruby questions, I'll try to mix old + recent questions. I won't cover basic questions here unless the question gets high votes. The covered questions will be related to new, tricky and controversial topics.

Please don't hesitate to give any notes regarding the idea and the way the questions are picked and presented.

Let's start this series, with 3 questions for this post:

'Instance_eval' instead of 'yield(self)'

The question title is: Instance Eval Within Block

The question text is:

class Builder
    def initialize
        @lines = []
    end

    def lines
        block_given? ? yield(self) : @lines
    end

    def add_line( text )
        @lines << text
    end
end

Now, how do I change this

my_builder = Builder.new
my_builder.lines { |b|
    b.add_line "foo"
    b.add_line "bar"
}
p my_builder.lines # => ["foo", "bar"]

Into this?

my_builder = Builder.new
my_builder.lines {
    add_line "foo"
    add_line "bar"
}
p my_builder.lines # => ["foo", "bar"]

The answer is:

class Builder
    def initialize
        @lines = []
    end

    def lines(&block)
        block_given? ? instance_eval(&block) : @lines
    end

    def add_line( text )
        @lines << text
    end
end

my_builder = Builder.new
my_builder.lines {
    add_line "foo"
    add_line "bar"
}
p my_builder.lines # => ["foo", "bar"]

The trick here is to use instance_eval which evaluates the passed block in the context of the current object called on.

Determining whether a variable is any one of a list of values

The question title is: Shortest/most idiomatic way of determining whether a variable is any one of a list of values

The question text is:

This seems a ridiculously simple question to be asking, but what's the shortest/most idiomatic way of rewriting this in Ruby?

if variable == :a or variable == :b or variable == :c or variable == :d # etc.

I've previously seen this solution:

if [:a, :b, :c, :d].include? variable

but this isn't always functionally equivalent - I believe Array#include? actually looks to see if the variable object is contained in the list; it doesn't take into account that the object may implement its own equality test with def ==(other).

Take, for example, Rails' Mime::Type implementation: request.format == :html may return true, but [:html].include?(request.format) will return false, as request.format is an instance of Mime::Type, not a symbol.

The answer:
Actually, #include? does use ==. The problem arises from the fact that if you do [:a].include? foo, it checks :a == foo, not foo == :a. That is, it uses the == method defined on the objects in the Array, not the variable. Therefore you can use it so long as you make sure the objects in the array have a proper == method.

In cases where that won't work, you can simplify your statement by removing the select statement and passing the block directly to any:

if [:a, :b, :c, :d].any? {|f| variable == f}

Using 'instance_eval' instead of 'send'

The question title is: How to evaluate multiple methods per send command?

The question text is:

Let's say I have an XML::Element...I want to do something like:

my_xml_element.send("parent.next_sibling.next_sibling")

The answer is:

In your case it's better to use instance_eval

"Test".instance_eval{chop!.chop!} #=> "Te"

And for your code:

my_xml_element.instance_eval{parent.next_sibling.next_sibling}