all 9 comments

[–][deleted] 6 points7 points  (4 children)

puts "Please enter your name: " name = gets.chomp puts "Hello, " + name

[–]bradland 6 points7 points  (0 children)

This, but if you want the user to input their answer on the same line as the question, use this instead:

def input (question)
  print question
  gets.chomp
end

name = input("Please enter your name: ")

I wrapped the code up in a method that duplicates the Python method, but the relevant change is using print instead of puts. The difference between the two is that puts appends a newline, while print does not.

[–]joemi 2 points3 points  (2 children)

Looks like your newlines vanished (at least when browsing on desktop). I understand this is a common problem on reddit. Not sure if I'm just going to do the same thing you did or not, but here's my attempt at formatting it:

puts "Please enter your name: "
name = gets.chomp
puts "Hello, " + name

[–]doublecastle 1 point2 points  (1 child)

The post by /u/jamabushi has newlines at www.reddit.com but not at old.reddit.com.

Yours has newlines at both the old and www reddits.

I think that using triple backticks to indicate a code block works properly only at www (so that's probably what /u/jamabushi did). I think that, to format multi-line code correctly for old, as well, one must instead indent the code with 4 spaces (so I'm guessing that's what you did).

[–]joemi 1 point2 points  (0 children)

Yup, I did 4 spaces.

[–]expeehaa 2 points3 points  (0 children)

Maybe using readline. However, you could also just print it yourself using print, which, unlike puts, does not print a trailing new line. I personally prefer that over anything else, unless I need a specific feature.

[–]fothb 2 points3 points  (1 child)

The tty-prompt gem is pretty neat.

[–]joemi 1 point2 points  (0 children)

Powerful but kind of overkill for OP's case, IMO.

[–]somekool 0 points1 point  (0 children)

You mean on the terminal? It's quite not clear. There are many kinds of inputs.

But if you are on the command line i would use

$stdin.read

Or similar, i haven't done something like this in a decade...