Using gets and split

I’m working on a challenge that tells me that the input is given at the
beginning via STDIN. Thus, these are the first few lines of my program:

all_input = gets
all_input = all_input.to_s.split("\n")

The input is going to be split by separate lines, and I want to put each
into a separate element in an array. Now, this code works in irb
(modified
to look more like str = soandso, str = str.to_s.split…), but when I
run
it in my program, everything gets thrown into one element in the array.
Can
someone tell me why?

hi Reese,

all_input = gets
all_input = all_input.to_s.split("\n")

the to_s method is unnecessary here, as gets returns a String
already.

if you’re splitting on new lines ("\n"), everything you get with
gets will be in one element of the array, as there is only one new
line… similarly, if you change the string to be “soandsoandsoandso,”
it will still wind up as only one element of the array, as there is only
one new line…

for comparison, try:

str = “soandsoandso”
arr = str.split(“o”)

p arr

=> [“s”, “ands”, “ands”]

  • j

Ok, that does work- so the problem is with how the data is being
inputted,
not with the program?

That’s a good point & idea. I will take that into consideration and will
most likely use the loop like you suggested. Thanks.

On Sun, Nov 13, 2011 at 12:27 PM, Dave A. <

On Sat, Nov 12, 2011 at 21:42, Reese C. [email protected]
wrote:

Ok, that does work- so the problem is with how the data is being inputted,
not with the program?

No, it’s the program, if you do indeed want to use gets. Thing is,
you’re just using gets once, and gets just reads one line. I don’t
see how you could have wound up with multi-line results from one gets
call in irb. Possibly the modifications you glossed over, were doing
a good bit more than that. Anyway, there are a couple alternate
suggestions that spring to mind.

One is to use a different input reading function that will read until
end-of-file, and then split it as you were doing. The user must press
^Z when done (^D on Mac or Linux). However, typical non-technical
end-users might not understand things like “press ^Z”. (I’m guessing
this is really just an academic exercise so that wouldn’t really
matter, but it’s still a good idea to get into the habit of thinking
about what your users will understand or not.) Also, reading until
EOF in one chunk can be a dangerous habit to get into, as some joker
(or malicious attacker) may enter much more data than your program can
really handle. (Again, good habit to get into thinking about security
and scalability.)

A much better way is to put gets in a loop with some condition to end
it, such as entering a blank line. How to initialize the data and
execute that loop, are left as exercises for the reader.

-Dave

-----Messaggio originale-----
Da: Reese C. [mailto:[email protected]]
Inviato: luned 14 novembre 2011 00:04
A: ruby-talk ML
Oggetto: Re: Using gets and split

That’s a good point & idea. I will take that into consideration and will
most likely use the loop like you suggested. Thanks.

On Sun, Nov 13, 2011 at 12:27 PM, Dave A. <
[email protected]> wrote:

a good bit more than that. Anyway, there are a couple alternate
(or malicious attacker) may enter much more data than your program can
LOOKING FOR WORK! What: Ruby (on/off Rails), Python, other modern
languages.
Where: Northern Virginia, Washington DC (near Orange Line), and remote
work.
See: davearonson.com (main) * codosaur.us (code) *
dare2xl.com(excellence).
Specialization is for insects. (Heinlein) - Have Pun, Will Babble!
(Aronson)


Reese C.


Caselle da 1GB, trasmetti allegati fino a 3GB e in piu’ IMAP, POP3 e
SMTP autenticato? GRATIS solo con Email.it http://www.email.it/f

Sponsor:
Riccione Hotel 3 stelle in centro: Pacchetto Capodanno mezza pensione,
animazione bimbi, zona relax, parcheggio. Scopri l’offerta solo per
oggi…
Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid982&d)-12

-----Messaggio originale-----
Da: Reese C. [mailto:[email protected]]
Inviato: domenica 13 novembre 2011 01:21
A: ruby-talk ML
Oggetto: Using gets and split

I’m working on a challenge that tells me that the input is given at the
beginning via STDIN. Thus, these are the first few lines of my program:

all_input = gets
all_input = all_input.to_s.split("\n")

The input is going to be split by separate lines, and I want to put each
into a separate element in an array. Now, this code works in irb
(modified
to look more like str = soandso, str = str.to_s.split…), but when I
run
it in my program, everything gets thrown into one element in the array.
Can
someone tell me why?


Reese C.


Caselle da 1GB, trasmetti allegati fino a 3GB e in piu’ IMAP, POP3 e
SMTP autenticato? GRATIS solo con Email.it http://www.email.it/f

Sponsor:
Capodanno a Riccione, Pacchetto Relax: Mezza Pensione + bagno turco +
solarium + massaggio. Wifi e parcheggio gratis. 2 giorni euro 199 a
persona
Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid=11979&d=29-12

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs