Because code says more the thousand words:
##################
open(“test.txt”,“r”).each do end # doesn’t close file
FileUtils.rm “test.txt” # will fail: Permission denied
##################
(it doesn’t matter if the “do end” is filled or omitted)
Because code says more the thousand words:
##################
open(“test.txt”,“r”).each do end # doesn’t close file
FileUtils.rm “test.txt” # will fail: Permission denied
##################
(it doesn’t matter if the “do end” is filled or omitted)
Bug, ‘Feature’ or my mistake?
open will only close if you pass it a block directly.
open(“test.txt”,“r”).each do end # doesn’t close file
FileUtils.rm “test.txt” # will fail: Permission denied
…
open will only close if you pass it a block directly.
open(‘test.txt’){|file| file.each{ … } }
So just “do end” doesn’t count as a block? Seems (at least to me)
like it should, albeit not usually a very useful one…
On Mon, Dec 5, 2011 at 10:54 PM, Ankush Ganatra [email protected]wrote:
while true
i’m doing things…
Thanks.
Regards
Ankush
You got this out of a book? I’d be hesitant to continue learning from it
(convention is 2 spaces for indentation, aNum should be a_num or
better
yet number or maybe even integer depending on intent, the i=0 does
nothing, and if aNum isn’t zero, it will get caught in an infinite loop)
Anyway, it works this way because the catch block is executed first. If
the
catch block had thrown the symbol, it would have been caught. But it
didn’t
throw the symbol, so it continued execution. Then, outside of the block,
when dothings(0) is called, it prints the statement then throws the
symbol. But this doesn’t happen in a catch block, so it goes uncaught
and
ultimately raises an error.
Run it with these three locations to see the difference:
catch :go_for_tea do
dothings 0
puts “caught here”
dothings 0
end
dothings 0
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.