How to get access to the comparison passed

If I have a method like this:

def my_method(comp)
#I know this is not working but to understand what I want
puts comp.to_s
return comp


my_method will return false and print out 4==6

#my_method will return true and print out b<456

#my_method will return true and print out c>=b

#my_method will return false and print out life

is that possible?

When you invoke


the expression 4==6 is of course evaluated BEFORE my_method is invoked
(after all, we don’t have call-by-name semantics), so every information
about how the result of the expression (nil in this case) has been
constructed, is lost.

You could pass the expression as a string, i.e.


and eval it inside my_method, which would give you access to the
constituents of the expression, but this obviously breaks down, if local
variables are involved.

For local variables, you could perhaps use a so called binding, i.e.


and define the printing method like this:

def my_method(b, expr)
r = b.eval(expr)
puts “#{expr} results in #{r}”

but this is not really convenient either.

What would really be needed, is a built-in macro feature for Ruby. Of
course you could use some standard macro processor (such as M4), but
this would mean that you have to run your Ruby program through it before
you can execute the program.

is that possible?

Yes it is possible.

You can test this - simply assign a variable to the method call like:

x = your_method_here()

Then print out the value of x.

It should contain the value that the method has returned (if you used
the return statement; if you omit return, it will use the last value
returned or better, it will return the last statement in this case; e.
g. if you put nil at the last line, your method would return nil; you
can use this to gather data in an array that is returned too by the

This will work for almost every use case you have; a slight exception
exists for some setter methods, reason being because they need to work
in chain setups too like object.method1=(5).method2 - but this is rare
that it will bite you. For most of the cases, the above just works fine.

What would really be needed, is a built-in macro feature for Ruby.

I am not sure that this is required in the context of the question - he
really only wanted to make use of the comparison returned and also
output it.

Mario R. - you could also determine this when you invoke your method,
just by adding an extra argument that has a default value such as:

be_verbose = true

if be_verbose
puts ‘The value is 6*4 = 24’

Mario, basically you can’t.

An argument needs to point to somewhere in memory if you want to use it
in a method (function).

Using metaprogramminy, you may be able to come up with a limited dsl
that can do what you want, but it would be like making sausages: You
don’t want to see how that’s done.

You should probably rethink your idea. Don’t go through, go around :wink:

I don’t know if I’m following you, I don’t see how it is gonna be
printed the comparison we supply

Robert H. wrote in post #1184902: