I’m trying to practice and learn by making my first program. I just
started learning about classes so I decided to convert the whole program
so it has a class in it.
It was successful (kinda), but in the end I don’t know why but I’m
getting a nill message between the lines.
Doesn’t nill mean when an outputs value is 0?
Thank you for the help, I appreciate every suggestion!
I’m really enjoying ruby so far.
If you’re using IRB you’ll get “nil” as a return value when you perform
certain actions like creating a method. This is normal, it just shows
you that nothing is returned.
If you’re referring to nil outputs somewhere else, can you demonstrate?
I’m referring to the bill between the monthly results and the overall
results. That’s why I’m wondering because the method has already been
run with results and it still puts out the nil.
Could it be because I’m using the p not puts (making a method for
.to_s)?
Welcome to your credit card payment calculator!
Please tell me your credit card balance.
50
Please enter your APR!
18
How much $ would you like to pay every month?
10
The last action in the method “calculate” is “puts”. This returns nil.
To elucidate: you have the line p debt.calculate, where you probably
want
simply debt.calculate. The way it is now, you’re calling the method,
which does its own printing, and then you send the method’s return value
(nil) to p, which prints the nil.
If you’re using IRB you’ll get “nil” as a return value when you perform
certain actions like creating a method. This is normal, it just shows
you that nothing is returned.
Wrong. Nil is a value just like everything else. Something was returned:
nil.
In fact, in your program you correctly initialize your calculator
instance with float values, but you really shouldn’t rely on that.
The calculator should work properly with integer values, or you
have a rather difficult to find bug (no error messages but possibly
wrong results).
Regards,
Marcus
BTW. Using global variables (like $m_counter) is usually frowned upon.
You should use instance variables for those, too.
If you’re using IRB you’ll get “nil” as a return value when you perform
certain actions like creating a method. This is normal, it just shows
you that nothing is returned.
Wrong. Nil is a value just like everything else. Something was returned:
nil.
Fair point.
In Ruby 2.1 you get a symbol back when you define a method!
On Mon, Oct 28, 2013 at 7:52 PM, Calvin Bornhofen [email protected] wrote:
On 28.10.2013 19:36, Greg H. wrote:
Wow… didn’t know that. Could you explain it how it works?
the method at work here is #% which formats the receiver by substituting the
placeholders with the arguments it gets. You can read about all formatting
options over in the docs of Kernel#sprintf (String#% uses the same rules): Module: Kernel (Ruby 2.0.0)
anytime. Much more straightforward. IMHO String#% is best used with a
single argument. As soon as you start having multiple values you need
an explicit Array as show above. But then one can also use (s)printf.
Wow… didn’t know that. Could you explain it how it works?
the method at work here is #% which formats the receiver by substituting
the placeholders with the arguments it gets. You can read about all
formatting options over in the docs of Kernel#sprintf (String#% uses the
same rules):
Regards,
Calvin
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.