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:
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@:16(irb)
=> true
Do you see what I mean? Instead of having:
#Proc:0x02a4ea78@:16(irb)
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
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:
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@:16(irb)
=> true
Do you see what I mean? Instead of having:
#Proc:0x02a4ea78@:16(irb)
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?
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:
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@:16(irb)
=> true
Do you see what I mean? Instead of having:
#Proc:0x02a4ea78@:16(irb)
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:
Cool! I thought I was the only one who’d read Babel-17!
Delaney is one of the most interesting scifi writers ever. Two other
novels dealing with how language affects thought are 1984 (of course),
and SnowCrash, by Neal Stephenson.