Ruby 'Gets' That Works Over Multiple Lines

Ruby 'gets' that works over multiple lines

You can do this in following way,

$/ = "END"  
user_input = STDIN.gets
puts user_input

make sure to type END keyword when you think the input is finished,

As well this will only work with actual interpreter not irb.

Ruby: Can I write multi-line string with no concatenation?

There are pieces to this answer that helped me get what I needed (easy multi-line concatenation WITHOUT extra whitespace), but since none of the actual answers had it, I'm compiling them here:

str = 'this is a multi-line string'\
' using implicit concatenation'\
' to prevent spare \n\'s'

=> "this is a multi-line string using implicit concatenation to eliminate spare
\\n's"

As a bonus, here's a version using funny HEREDOC syntax (via this link):

p <<END_SQL.gsub(/\s+/, " ").strip
SELECT * FROM users
ORDER BY users.id DESC
END_SQL
# >> "SELECT * FROM users ORDER BY users.id DESC"

The latter would mostly be for situations that required more flexibility in the processing. I personally don't like it, it puts the processing in a weird place w.r.t. the string (i.e., in front of it, but using instance methods that usually come afterward), but it's there. Note that if you are indenting the last END_SQL identifier (which is common, since this is probably inside a function or module), you will need to use the hyphenated syntax (that is, p <<-END_SQL instead of p <<END_SQL). Otherwise, the indenting whitespace causes the identifier to be interpreted as a continuation of the string.

This doesn't save much typing, but it looks nicer than using + signs, to me.

Also (I say in an edit, several years later), if you're using Ruby 2.3+, the operator <<~ is also available, which removes extra indentation from the final string. You should be able to remove the .gsub invocation, in that case (although it might depend on both the starting indentation and your final needs).

EDIT: Adding one more:

p %{
SELECT * FROM users
ORDER BY users.id DESC
}.gsub(/\s+/, " ").strip
# >> "SELECT * FROM users ORDER BY users.id DESC"

Ruby Multi-Line Input for Only One Input

IO#gets has an optional parameter that allows you to specify a separator. Here's an example:

puts "Enter Response"
response = gets.chomp

puts "Enter a multi line response ending with a tab"
response = gets("\t\n").chomp

Output:

Enter Response
hello
Enter a multi line response ending with a tab
ok
how
is
this

Preferred style for displaying multiple lines using `puts`

If the lines to be printed are short enough to be put together on a single line in the source, then I would go with your first option:

puts "This", "is", "fairly", "easy"

If they are long, then I would use a heredoc:

puts <<_.unindent
This Blah Blah ...
Seems Blah Blah ...
Straightforward. Blah Blah ...
_

where unindent is a method to unindent an indented heredoc along the lines suggested in Ruby indented multiline strings. Notice that in future versions of Ruby, it is likely that there will be a simpler way to unindent a heredoc, so this option will become more useful.

I see no point in using your second or fourth option. The third may be used, but it looks ugly.

Breaking up long strings on multiple lines in Ruby without stripping newlines

Three years later, there is now a solution in Ruby 2.3: The squiggly heredoc.

class Subscription
def warning_message
<<~HEREDOC
Subscription expiring soon!
Your free trial will expire in #{days_until_expiration} days.
Please update your billing information.
HEREDOC
end
end

Blog post link: https://infinum.co/the-capsized-eight/articles/multiline-strings-ruby-2-3-0-the-squiggly-heredoc

The indentation of the least-indented line will be
removed from each line of the content.

How to get multiple-line user input in Ruby?

Try this:

$/ = "END"
user_input = STDIN.gets
puts user_input

The user can then keep entering a multiple line input and ends his input by typing in END. Keep in mind that IRB doesn't handle this code snippet well, so make sure you use the actual ruby interpreter.

Also the user_input stores the final END typed in by the user so you'll need to strip it out. You can also use any other string in the place of END for the user to indicate that they've finished inputting.

Ruby convention for chaining calls over multiple lines

There is actually a section on that in the Ruby style guide:

Adopt a consistent multi-line method chaining style. There are two
popular styles in the Ruby community, both of which are considered
good - leading . (Option A) and trailing . (Option B).

  • (Option A) When continuing a chained method invocation on
    another line keep the . on the second line.

    # bad - need to consult first line to understand second line
    one.two.three.
    four

    # good - it's immediately clear what's going on the second line
    one.two.three
    .four
  • (Option B) When continuing a chained method invocation on another line,
    include the . on the first line to indicate that the
    expression continues.

    # bad - need to read ahead to the second line to know that the chain continues
    one.two.three
    .four

    # good - it's immediately clear that the expression continues beyond the first line
    one.two.three.
    four

A discussion on the merits of both alternative styles can be found
here.

Building multi-line strings, programmatically, in Ruby

This would be one way:

code = []
code << "next line of code #{something}"
code << "another line #{some_included_expression}"
code.join("\n")


Related Topics



Leave a reply



Submit