the behavior I’m looking for is so I can define methods to get results like these:
> 2i
=> Complex(0,+2i)
> 2²
=> 4
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
For the first one, you could add a custom instance methods to
Fixnumlike this:And as J-_-L pointed out, you could do the same for the second one in Ruby 1.9, though your rb file will need
# encoding: utf-8at the top.And as DNNX pointed out, the issue of creating imaginary numbers in Ruby is already addressed by the
Complexclass: