Forum: Ruby how to get access to the comparison passed

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
97334b75c93574ed3e514f33849f0953?d=identicon&s=25 Mario Ruiz (tcblues)
on 2016-08-29 16:09
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
end


my_method(4==6)
# my_method will return false and print out 4==6

b=7
c=9
life=false
my_method(b<456)
#my_method will return true and print out b<456

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

my_method(life)
#my_method will return false and print out life

is that possible?
0fa73332c8e4a3b06ea439fd3f034322?d=identicon&s=25 Ronald Fischer (rovf)
on 2016-08-29 17:35
When you invoke

  my_method(4==6),

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.

  my_method('4==6')

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.

   b.local_variable_set(:x,5)
   my_method(b,'x+1')

and define the printing method like this:

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

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.
4828d528e2e46f7c8160c336eb332836?d=identicon&s=25 Robert Heiler (shevegen)
on 2016-08-29 22:00
> 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
way).

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 Ruiz - 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'
  end
97334b75c93574ed3e514f33849f0953?d=identicon&s=25 Mario Ruiz (tcblues)
on 2016-08-29 22:15
I don't know if I'm following you, I don't see how it is gonna be
printed the comparison we supply

Robert Heiler wrote in post #1184902:
>
6c7fb11d4f3fa354a51a3ea0375d895f?d=identicon&s=25 Joe Gain (timeout)
on 2016-08-30 08:15
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 ;)
This topic is locked and can not be replied to.