Ruby 'gets' that works over multiple lines
Asked Answered
G

4

13

Using the IRB, I want to enter a multiple line string in order to strip certain characters from it. "gets" only allows a single line - is there a similar function for multiple lines.

ASCII_project.rb(main):002:0* puts = "What's the text you want to strip?"
=> "What's the text you want to strip?"
ASCII_project.rb(main):003:0> str = gets

I now want to paste in a section of text - because of the new lines it doesn't function. This is why I want to collect over multiple lines

Here is the code

# encoding: CP850
puts = "What's the text you want to strip?"
str = gets
str.gsub!(/\P{ASCII}/, '')
puts str
Gathers answered 12/12, 2012 at 12:36 Comment(2)
hum, just call gets several times ?Anisotropic
If not by newline character, how should the method know when to stop reading?Overdevelop
P
9

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.

Preclinical answered 12/12, 2012 at 12:42 Comment(3)
This is the only viable solution in RubySkyeskyhigh
Can you explain what $/ does?Gathers
@Gathers that's a ruby global variable and used as separator by gets and readline.Scolopendrid
T
10

You can use this method, it accepts text until the first empty line

def multi_gets all_text=""
  while (text = gets) != "\n"
    all_text << text
  end
  all_text
end

or this one, you can replace the \n\n with any end character you define

def multi_gets all_text=""
  while all_text << STDIN.gets
    return all_text if all_text["\n\n"]
  end
end
Trinette answered 12/12, 2012 at 13:37 Comment(1)
This is a good encapsulation of the termination character process.Skyeskyhigh
P
9

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.

Preclinical answered 12/12, 2012 at 12:42 Comment(3)
This is the only viable solution in RubySkyeskyhigh
Can you explain what $/ does?Gathers
@Gathers that's a ruby global variable and used as separator by gets and readline.Scolopendrid
O
9

You could use readlines() on $stdin like so

> $stdin.readlines
Mit Wohnungen, mit Bergen, Hügeln, Flüssen,
Solang ichs deutlich sah, ein Schatz der Freuden;
Zuletzt im Blauen blieb ein Augenweiden
An fernentwichnen lichten Finsternissen.

# ^D
 => ["Mit Wohnungen, mit Bergen, Hügeln, Flüssen,\n",
 "Solang ichs deutlich sah, ein Schatz der Freuden;\n",
 "Zuletzt im Blauen blieb ein Augenweiden\n",
 "An fernentwichnen lichten Finsternissen.\n"]
Objection answered 30/9, 2013 at 21:45 Comment(2)
Your sample code uses the wrong variable name for the standard in. I would have edited it but its under the 6 character limitHigh
@High Thanks, that went unnoticed.Objection
P
-4
str = <<-EOF
Your multi line
text goes here
.....
EOF

But the catch is you'll have to end with EOF

Party answered 12/12, 2012 at 13:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.