On Fri Jun 16, 2006 at 20:34:12 +1000, O Plameras wrote:
>Benno wrote:
>>On Fri Jun 16, 2006 at 20:00:29 +1000, O Plameras wrote:
>>  
>>>Matthew Palmer wrote:
>>>
>>>    
>>>>Reading the Fine Manual, we discover that your code probably won't work
>>>>anyway:
>>>>      
>>>Yes, the two yield exactly the same results as shown below.
>>>
>>>#cat t1.rb
>>>#!/usr/bin/env ruby
>>>puts $stdin.readline while not $stdin.eof
>>>
>>>#cat t2.rb
>>>#!/usr/bin/env ruby
>>>puts $stdin.readlines while not $stdin.eof
>>>    
>>
>>I think the key difference here is the number of times the loop is
>>execute. That is going to be different, and understanding that
>>difference is probably extremely important to those in the learning
>>Ruby SIG.
>>  
>
>So, does the difference matters when the results are the same ?  
>Programs worked
>for me when I get the results I want.
>
>Can anyone illustrate an instance where the results are different 
>between readline and
>readlines in the context of the original post ?


find ./ | t1.rb
find ./ | t2.rb

They are different. t1.rb will start displaying straight away, t2.rb
will buffer everything and then display. Whether that matters totally
depends on the program requirements.

In context of the original post, you can sort the array return be
readlines, but doesn't make sense to sort the string returned by
readline.

I think t1.rb is a much better implementation, than t2.rb, it is
certainly more effecient. (Which is not what I had been led to believe
by other peoples posts earlier in the thread). But I think t2.rb is
bad code, because *it will never loop*, so having a loop condition
there is confusing and obfuscates the code making it more difficult to
maintain.

t2.rb is the same as:

#!/usr/bin/env ruby
puts $stdin.readlines

I think the above is simpler to understand than t2.rb


Cheers,

Benno
_______________________________________________
coders mailing list
coders@slug.org.au
http://lists.slug.org.au/listinfo/coders

Reply via email to