Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Can variables hold math signs???

Can variables hold math signs??? So, that for example:

@minus = "-" #(minus sign)

@x = 3 @minus 2


Is this possible??
Thanks in advance
 
Not really, you can use eval though:
Code:
@minus = " - "
x = eval(3.to_s + @minus + 2.to_s)

eval is evil, however.* And a better alternative is to store the operation in a procedure. Something like:
Code:
@minus = Proc.new { |a, b| a - b }
x = @minus.call(3, 2)

* If you are new to the language, I really suggest not using eval, there are often better ways to do things without using it. It's a security risk and a performance hit, and just bad coding style in general.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top