It’s pretty trivial, but it doesn’t have a well-delimited “end” to it.
In the forums, they indicate one should test for end of file. I have no
idea how to do that in Ruby.
I mean - I paste my code in a box, tell it to run and then they tell me
if it worked or not.
I can only suppose they’re using the “-i” argument … If so, how do I
test for when that file has run out of stuff?
It’s pretty trivial, but it doesn’t have a well-delimited “end” to it.
In the forums, they indicate one should test for end of file. I have no
idea how to do that in Ruby.
gets will return nil on end of file.
while line = $stdin.gets
line.chomp!
puts “I got #{line} !!”
end
I can only suppose they’re using the “-i” argument … If so, how do I
test for when that file has run out of stuff?
huh? -i is “in-place edit mode”. It makes ruby read in one or more files
(whose names are listed on the command line), and write them back out
again with modifications under the original names, optionally renaming
the original file to a different name. You’d use it if you were using
ruby to do search-and-replace type operations.
Ugh, sat down and played with it, and now I remember why I gave up on
SPOJ,
they tell you you got it wrong, but don’t tell you why.
It usually boils down to obscure undocumented output formatting
differences
(ie how much whitespace and where), but you can’t tell whether it’s due
to
formatting error, or an actual wrong answer.
Also, codechef.com is basically SPOJ with a prettier interface, active
administration, and a community behind it. But I have had difficulty
getting
Ruby to work there, in the past. Not sure why.
Use #each_line if you want to do that. Ruby 1.9 removed #each from
Strings, but strangely decided to leave it on Files. To be safe, don’t
assume that ‘each’ means ‘each line’.
Hmm, both #each and #each_line work on 1.8.7 and 1.9.1. But you’re
right, #each_line is more to the point. Thanks for the heads up,
Brian!
$stdin.each do |line|
line.chomp!
puts “I got #{line} !!”
end
because that is the more Ruby typical idiom for iterating all lines.
Use #each_line if you want to do that. Ruby 1.9 removed #each from
Strings, but strangely decided to leave it on Files. To be safe, don’t
assume that ‘each’ means ‘each line’.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.