Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7853549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:43:56+00:00 2026-06-02T19:43:56+00:00

Using the ruby_parser and Ruby2Ruby gems, I’m writing code that keeps track of what

  • 0

Using the ruby_parser and Ruby2Ruby gems, I’m writing code that keeps track of what conditions have been evaluated and what their results and parameters were. In order to keep this as simple as possible, I sometimes rewrite the AST a bit. Of course, I can only do that if I’m sure the result functions exactly the same as the original.

Am I correct in asserting that the following three Ruby snippets are equivalent in function, assuming the triple dots are replaced by a valid Ruby expression? Am I overlooking any edge cases?

case var
  when foo 
    something
  when ... 
    another_thing
  else 
    something_else
end

if foo === var 
  something
elsif ... === var
  another_thing
else  
  something_else
end

case
  when foo === var 
    something
  when ... === var 
    another_thing
  else 
    something_else
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T19:43:57+00:00Added an answer on June 2, 2026 at 7:43 pm

    Those three snippets are equivalent iff var is idempotent, i.e. evaluating var multiple times has the same side-effects as evaluating it once.

    So, if var really is a variable, you are safe, but remember that it can be an arbitrary expression, including a message send to a side-effecting method (like puts).

    E.g.

    case puts('Hello')
    when 1
    when 2
    

    is not the same as

    if 1 === puts('Hello')
    elsif 2 === puts('Hello')
    

    because in the latter case, “Hello” will be printed twice.

    A better translation might be:

    __randomly_generated_guaranteed_unique_local_variable_jhggfq45g345 = var
    
    if foo === __randomly_generated_guaranteed_unique_local_variable_jhggfq45g345 
      something
    elsif ... === __randomly_generated_guaranteed_unique_local_variable_jhggfq45g345
      another_thing
    else  
      something_else
    end
    
    case
      when foo === __randomly_generated_guaranteed_unique_local_variable_jhggfq45g345
        something
      when ... === __randomly_generated_guaranteed_unique_local_variable_jhggfq45g345
        another_thing
      else 
        something_else
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been using MongoDB and the Ruby driver and Mongoid, and lines db.things.find({j:
Using Ruby 1.8.7-p358, Rails 3.0.12, gem responder, gem simple-form; jquery_ujs.js I have a form
With reXml using Ruby, I have a particular element and I want to completely
I have unfortunately wandered into a situation where I need regex using Ruby. Basically
I have been playing around with Scala parser combinators for some time now, and
I'm writing a Rails application, using the 'json' gem, and its behavior where it
I have a strange situation where the code: c = Class.new { eval parser
I have a client that is sending XML for my site to parse. I
I have defined simple grammar for parsing string and number using Treetop as below.
I have defined simple grammar for parsing string and number using Treetop as below.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.