Ruby why does puts return nil




















Ruby is awesome" How do the puts and print methods actually output text to your console? To prevent the nil as the output of print, you can print a line break after it, and optionally put some other value as the last statement of your code, then the Console will show it instead of nil: print "hey, Ruby is awesome!

Previous Post Difference between map, select, collect, inject, detect and each in ruby Ruby on rails. Leave a Reply Cancel reply Your email address will not be published. Recent Posts class eval and instance eval in Ruby — Meta programming Includes and joins in active record queries rails.

Categories activerecord Artificial intelligence git heroku python ruby Ruby on rails. Optimizer WordPress Theme. Ask Question. Asked 8 years, 9 months ago. Active 6 years, 9 months ago. Viewed 8k times. Matt Perejda Matt Perejda 5 5 silver badges 14 14 bronze badges. Add a comment. Active Oldest Votes. Because that is the return value of puts : puts obj, I would add that every instruction in ruby returns something.

Every method, attribution, declaration of classes, methods. And in irb that's what you see, the returned value or evaluation. So the methods that dosen't make sense to return anything they just return nil — Ismael. Thank you. This is very helpful to me. You are correct, I am using irb. I get the sense that I would be better off programming in a normal application. A return value is the data returned to the program by the execution of a method, the assignment of a variable, actually This is because puts and print may print the value you want, but instead of returning that value, they return nil.

Methods are like vending machines. When you use a vending machine you just put in two arguments, the number C7 and your money. We already know how to use arguments, but then your vending machine might do two things. One, it will make a noise saying that everything worked, beep beep.

Then it gives you the soda. The soda is the return type. But those beeps? Are you able to do anything with them?

That's like puts: it just tells you stuff and then goes into the ether! Gone forever. Every method in Ruby returns a value by default. This returned value will be the value of the last statement. The return value of the restaurant method is "Welcome to Flavor Town!

Say you're the best chef in the world, Guy Fieri. To make a method that just prints your name and returns nil , you could write:. The method would instead print "Guy Fieri" and return nil. This is because the last line that was evaluated was puts There is one other way to return a value from a method and that is to use the return keyword.

What do you expect the return value of the above method to be? Go into IRB, copy and paste the above method and call it. Go on, I'll wait. You may have expected the return value to be "Guy Fieri". His name is the last line of the method and it is true that he does have the best style you can't beat sunglasses on the back of the head, sorry.

Note: if you aren't getting "Martha Stewart" as the return value remember you need to call the method in IRB.



0コメント

  • 1000 / 1000