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:

Now, how do I change this

Into this?

The answer is:

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?

I’ve previously seen this solution:

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:

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:

The answer is:

In your case it’s better to use instance_eval

And for your code:

11 Replies to “StackOverflow cool Ruby questions”

  1. Woah! You used my Stack Overflow question! And I just stumbleupon’d it here! Mine was the first one, the instance_eval one.


  2. I’m pretty sure question #3 is actually asking: “How do I call a *String* of methods on an object?”

    In which case my_object.instance_eval{“some_string.of.methods”} doesn’t actually work.

    I updated the stack-overflow ticket with my suggestion.

  3. Good quality and style of the bag is very good, the future will focus on your family’s bags! Bags quality and looks very good. That his wife does not like the camel need another bag of quality and workmanship are very Bang Bang da. Simple and elegant, it is like oh. Bags of quality and workmanship are very good, great bag space bags can hold a lot of things are good quality and workmanship, the service is also very good, satisfying a shopping bag quality is great, the seller attitude is also very good. Support, but also in the past. Bag quality is very good. Feeling good. Seller service attitude is also very good. Bag quality is very good. Nice color styles, exquisite workmanship, very much.
    thomas burberry jeans

Leave a Reply

Your email address will not be published. Required fields are marked *