Ruby Gets/Puts Only for Strings

Ruby gets/puts only for strings?

If you are using to_i, then chomp before that is redundant. So you can do:

puts 'Hello there, Can you tell me your favourite number?'
num = gets.to_i
puts 'Your favourite number is ' + num.to_s + '?'
puts 'Well its not bad but ' + (num * 10).to_s + ' is literally 10 times better!'

But generally, using "#{}" is better since you do not have to care about to_s, and it runs faster, and is easier to see. The method String#+ is particularly very slow.

puts 'Hello there, Can you tell me your favourite number?'
num = gets.to_i
puts "Your favourite number is #{num}?"
puts "Well its not bad but #{num * 10} is literally 10 times better!"

Explanation of 'gets' method: String being printed without any user input?

gets actually has nothing to do with user input. Neither in this case, nor in the "usual" form you might be used to:

puts "what's your answer?"
answer = gets.chomp

In general, it is a method on IO objects which reads a string ("string" being defined as "all characters from current position up to (and including) a linebreak").

In your example it's being called on a File object (and, therefore, reads content from the opened file, line by line). The "naked" form reads lines from files, passed via command line arguments or (if no files were passed) from standard input. Note that standard input is not necessarily read from the keyboard (which is what you'd call "user input"). Input data can be piped into your program.

but just using using "f" without any attached methods doesn't print the lines from the text file

f is a reference to a file object. It does not represent any useful printable content. But you can use it to read some content from the file, which you do (f.gets).

How to use gets and gets.chomp in Ruby

gets lets the user input a line and returns it as a value to your program. This value includes the trailing line break. If you then call chomp on that value, this line break is cut off. So no, what you have there is incorrect, it should rather be:

  1. gets gets a line of text, including a line break at the end.
    • This is the user input
  2. gets returns that line of text as a string value.
  3. Calling chomp on that value removes the line break

The fact that you see the line of text on the screen is only because you entered it there in the first place. gets does not magically suppress output of things you entered.

Using gets.chomp to pass list of strings as arguments

"... gets.chomp seems to chuck the strings into an array"

the split method does this

Based on the way you've defined order you either need to do
order(*placed_order, total)
or redefine the order signature to def order food, total (a name that conveys that food expects an array, such as foods, might be "in order")

Slightly off-topic, you can shorten the order method as follows:

def order *food, total
cash = menu.values_at(*food).inject(&:+)
#...

Also, shouldn't "total" and "cash" be swapped (total == how much money you want; cash == how much they've given you)?

Ruby gets() not returning correct string

I think the gets includes the newline at the end of the input. try using gets.chomp instead

irb(main):001:0> input = $stdin.gets
hello
=> "hello\n"
irb(main):002:0> input = $stdin.gets.chomp
hello
=> "hello"


Related Topics



Leave a reply



Submit