evaluate and print an expression

Discussion in 'Ruby' started by Piergiuliano Bossi, Feb 9, 2004.

  1. I hope that what I'm asking doesn't sound too weird, but I'm trying to
    understand if in ruby is possible to print and evaluate an expression at
    the same time. I know for example that I can do like this:

    irb(main):001:0> expr = "1+2==3"
    => "1+2==3"
    irb(main):002:0> puts expr + ' ==> ' + (eval expr).to_s
    1+2==3 ==> true
    => nil

    What I don't like is to specify the expression as a string. I'd rather
    prefer to manage code directly, but I don't know exactly how to print it
    and evaluate it at the same time. Ideally, I'd like to change the
    following code in order to print block body:

    irb(main):012:0> def evaluate(&code)
    irb(main):013:1> p code
    irb(main):014:1> code.call
    irb(main):015:1> end
    => nil
    irb(main):016:0> evaluate { 1+2==3 }
    #<Proc:0x02a4ea78@(irb):16>
    => true

    Do you see what I mean? Instead of having:
    #<Proc:0x02a4ea78@(irb):16>
    I'd like to get:
    { 1+2==3 }
    or something like that.

    I know that in lisp this can be easily done due to its nature (that is,
    code is data and data is code), but in ruby?

    Thanks for your help!
    Ciao, Giuliano

    --
    If you want to send me an email in the address you have to write 'p',
    then a dot, followed by 'bossi' at 'quinary', another dot and 'com' at last
     
    Piergiuliano Bossi, Feb 9, 2004
    #1
    1. Advertising

  2. def evaluate(code)
    puts "#{code}==>"
    eval code
    end

    evaluate("1+2==3")

    1+2==3 ==>
    true




    Piergiuliano Bossi wrote:

    > I hope that what I'm asking doesn't sound too weird, but I'm trying to
    > understand if in ruby is possible to print and evaluate an expression
    > at the same time. I know for example that I can do like this:
    >
    > irb(main):001:0> expr = "1+2==3"
    > => "1+2==3"
    > irb(main):002:0> puts expr + ' ==> ' + (eval expr).to_s
    > 1+2==3 ==> true
    > => nil
    >
    > What I don't like is to specify the expression as a string. I'd rather
    > prefer to manage code directly, but I don't know exactly how to print
    > it and evaluate it at the same time. Ideally, I'd like to change the
    > following code in order to print block body:
    >
    > irb(main):012:0> def evaluate(&code)
    > irb(main):013:1> p code
    > irb(main):014:1> code.call
    > irb(main):015:1> end
    > => nil
    > irb(main):016:0> evaluate { 1+2==3 }
    > #<Proc:0x02a4ea78@(irb):16>
    > => true
    >
    > Do you see what I mean? Instead of having:
    > #<Proc:0x02a4ea78@(irb):16>
    > I'd like to get:
    > { 1+2==3 }
    > or something like that.
    >
    > I know that in lisp this can be easily done due to its nature (that
    > is, code is data and data is code), but in ruby?
    >
    > Thanks for your help!
    > Ciao, Giuliano
    >
     
    Bermejo, Rodrigo, Feb 9, 2004
    #2
    1. Advertising

  3. Bermejo, Rodrigo wrote:
    >
    >
    > def evaluate(code)
    > puts "#{code}==>"
    > eval code
    > end
    >
    > evaluate("1+2==3")
    >
    > 1+2==3 ==>
    > true


    :)

    Ok, I understand that I haven't explained myself clearly.

    The point is not how to make it work with an expression as a String
    ("1+2==3" for example), but working directly with code.

    At the end the question is: is it possible to print the code of an
    expression?

    Remember that I'm trying to do something like this:

    irb(main):012:0> def evaluate(&code)
    irb(main):013:1> p code
    irb(main):014:1> code.call
    irb(main):015:1> end
    => nil
    irb(main):016:0> evaluate { 1+2==3 }
    #<Proc:0x02a4ea78@(irb):16>
    => true

    Instead of having:
    #<Proc:0x02a4ea78@(irb):16>
    I'd like to get:
    { 1+2==3 }
    or something like that.

    I think that the answer in ruby is: no, it's not possible.
    But I may be wrong.

    Ciao, Giuliano


    --
    If you want to send me an email in the address you have to write 'p',
    then a dot, followed by 'bossi' at 'quinary', another dot and 'com' at last
     
    Piergiuliano Bossi, Feb 10, 2004
    #3
  4. "Piergiuliano Bossi" <> schrieb im
    Newsbeitrag news:c08kj7$hnm$...
    > I hope that what I'm asking doesn't sound too weird, but I'm trying to
    > understand if in ruby is possible to print and evaluate an expression at
    > the same time. I know for example that I can do like this:
    >
    > irb(main):001:0> expr = "1+2==3"
    > => "1+2==3"
    > irb(main):002:0> puts expr + ' ==> ' + (eval expr).to_s
    > 1+2==3 ==> true
    > => nil
    >
    > What I don't like is to specify the expression as a string. I'd rather
    > prefer to manage code directly, but I don't know exactly how to print it
    > and evaluate it at the same time. Ideally, I'd like to change the
    > following code in order to print block body:
    >
    > irb(main):012:0> def evaluate(&code)
    > irb(main):013:1> p code
    > irb(main):014:1> code.call
    > irb(main):015:1> end
    > => nil
    > irb(main):016:0> evaluate { 1+2==3 }
    > #<Proc:0x02a4ea78@(irb):16>
    > => true
    >
    > Do you see what I mean? Instead of having:
    > #<Proc:0x02a4ea78@(irb):16>
    > I'd like to get:
    > { 1+2==3 }
    > or something like that.
    >
    > I know that in lisp this can be easily done due to its nature (that is,
    > code is data and data is code), but in ruby?


    You can't without manually parsing the source file. The closest you might
    get without extra parsing might be this, but still "code" is a String:

    code=<<CODE
    1 + 2 == 3
    CODE
    puts "#{code} --> #{eval code}"

    Regards

    robert
     
    Robert Klemme, Feb 10, 2004
    #4
  5. Piergiuliano Bossi <> wrote
    > The point is not how to make it work with an expression as a String
    > ("1+2==3" for example), but working directly with code.
    >
    > At the end the question is: is it possible to print the code of an
    > expression?


    Nope, you can't. I've read a rationale for this somewhere, but I don't
    remember the details.
     
    jesse rudolph, Feb 10, 2004
    #5
  6. jesse rudolph wrote:
    > Piergiuliano Bossi <> wrote
    >
    >>The point is not how to make it work with an expression as a String
    >>("1+2==3" for example), but working directly with code.
    >>
    >>At the end the question is: is it possible to print the code of an
    >>expression?

    >
    >
    > Nope, you can't. I've read a rationale for this somewhere, but I don't
    > remember the details.


    :)

    Ok, this is what I thought.

    Thanks, Giuliano

    --
    If you want to send me an email in the address you have to write 'p',
    then a dot, followed by 'bossi' at 'quinary', another dot and 'com' at last
     
    Piergiuliano Bossi, Feb 10, 2004
    #6
  7. Robert Klemme wrote:

    >>I know that in lisp this can be easily done due to its nature (that is,
    >>code is data and data is code), but in ruby?

    >
    >
    > You can't without manually parsing the source file. The closest you might
    > get without extra parsing might be this, but still "code" is a String:
    >
    > code=<<CODE
    > 1 + 2 == 3
    > CODE
    > puts "#{code} --> #{eval code}"
    >


    Thanks Robert, I understand what you mean: it looks like code, but it's
    a String.

    Smart, but I was looking for real code.

    Thanks anyway.
    Ciao, Giuliano

    --
    If you want to send me an email in the address you have to write 'p',
    then a dot, followed by 'bossi' at 'quinary', another dot and 'com' at last
     
    Piergiuliano Bossi, Feb 10, 2004
    #7
  8. Julio Fernández, Feb 10, 2004
    #8
  9. Piergiuliano Bossi wrote:

    > Bermejo, Rodrigo wrote:
    >
    > I think that the answer in ruby is: no, it's not possible.
    > But I may be wrong.
    >
    > Ciao, Giuliano


    take a look at irb/xmp - print exemple.

    <-------------
    require 'irb/xmp'

    xmp <<END
    1+2==3
    END
    ------------->
    1+2==3
    ==>true


    >
    >
     
    Bermejo, Rodrigo, Feb 11, 2004
    #9
  10. Bermejo, Rodrigo wrote:
    >
    >
    > Piergiuliano Bossi wrote:
    >
    >> Bermejo, Rodrigo wrote:
    >>
    >> I think that the answer in ruby is: no, it's not possible.
    >> But I may be wrong.
    >>
    >> Ciao, Giuliano

    >
    >
    > take a look at irb/xmp - print exemple.
    >
    > <-------------
    > require 'irb/xmp'
    >
    > xmp <<END
    > 1+2==3
    > END
    > ------------->
    > 1+2==3 ==>true


    Neat!

    But it is still working on strings, doesn't it?

    Thanks, Giuliano

    --
    If you want to send me an email in the address you have to write 'p',
    then a dot, followed by 'bossi' at 'quinary', another dot and 'com' at last
     
    Piergiuliano Bossi, Feb 12, 2004
    #10
  11. "Piergiuliano Bossi" <> schrieb im
    Newsbeitrag news:c0g6qb$hba$...
    > Bermejo, Rodrigo wrote:
    > >
    > >
    > > Piergiuliano Bossi wrote:
    > >
    > >> Bermejo, Rodrigo wrote:
    > >>
    > >> I think that the answer in ruby is: no, it's not possible.
    > >> But I may be wrong.
    > >>
    > >> Ciao, Giuliano

    > >
    > >
    > > take a look at irb/xmp - print exemple.
    > >
    > > <-------------
    > > require 'irb/xmp'
    > >
    > > xmp <<END
    > > 1+2==3
    > > END
    > > ------------->
    > > 1+2==3 ==>true

    >
    > Neat!
    >
    > But it is still working on strings, doesn't it?


    Yes, it does. It's basically the same as

    eval <<END
    1+2==3
    END

    Regards

    robert
     
    Robert Klemme, Feb 12, 2004
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. evan
    Replies:
    1
    Views:
    1,109
    John Harrison
    Jun 28, 2003
  2. evan
    Replies:
    0
    Views:
    680
  3. Jason luo
    Replies:
    3
    Views:
    560
    Peter Ammon
    Aug 19, 2004
  4. keto
    Replies:
    0
    Views:
    998
  5. Jack Frillman
    Replies:
    2
    Views:
    2,734
    Martin Honnen
    Mar 17, 2011
Loading...

Share This Page