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

  • Home
  • SEARCH
  • 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 196909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:48:27+00:00 2026-05-11T16:48:27+00:00

I want to write a simple Ruby DSL to translate some statements and expressions

  • 0

I want to write a simple Ruby DSL to translate some statements and expressions into another language. A basic example would be:

some_function {
    t + 2
}

Here, t is not a ruby variable and thus the block can’t (and must not!) be evaluated by Ruby. So my best bet would be to use the parsing output (or AST) to do the translation myself. To do so, I can use ParseTree and ruby2ruby. However, I have other constructs that I would like to use, for example:

1.upto(10) {|x|
    some_function {
        t + x
    }
}

Here, I have a local variable and I need to get its value in order to do my translation. However, in the function that does the block evaluation, I don’t have access to the local variables of the calling block. If it were a global variable ($x), I could check if its name exists in the global_variables array and in the worst case use eval, but how could I do so for a local variable, if possible at all?

Update:

Just to clear up things. Like I said originally, I’m using ruby2ruby (and hence ParseTree) to get the AST (using to_sexp) corresponding to the block. But when using a local variable inside my block, I encounter the following:

[:dvar, :x]

And thus, I would need to get the value of a variable from its name as a string/symbol. And I can’t use method_missing or instance_eval, because I want to translate the whole expression to another language or syntax (like a RPN).

Another solution not based on ParseTree would be welcome nonetheless, since it apparently is not fully supported with Ruby 1.9.

  • 1 1 Answer
  • 1 View
  • 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-05-11T16:48:28+00:00Added an answer on May 11, 2026 at 4:48 pm

    To get the variable values, use the proc’s binding:

    def some_function(&block)
      b = block.binding
      p [b.eval("t"), b.eval("x")]
    end
    
    t = 1
    1.upto(10) {|x|
      some_function {
        t + x
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 181k
  • Answers 181k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yes. Write a console application that calls the WCF service,… May 12, 2026 at 4:10 pm
  • Editorial Team
    Editorial Team added an answer Your solution sounds fine. The nature of your problem is… May 12, 2026 at 4:10 pm
  • Editorial Team
    Editorial Team added an answer You can get the button pressed from the mouse event.… May 12, 2026 at 4:10 pm

Related Questions

I want to write a simple chat-client in Ruby for the terminal. The Problem
I'm writing a sample test with Watir where I navigate around a site with
I would like to write simple scripts in C#. Stuff I would normally use
I'm trying to write two batch files that will allow me to switch the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.