##################
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)
open will only close if you pass it a block directly.
open('test.txt'){|file| file.each{ ... } }
···
On Sun, Dec 4, 2011 at 1:47 PM, Mc Ben <viertelvor12@gmx.net> wrote:
On Ruby 1.9.3 (win7(64))
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)
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....
-Dave
···
On Sun, Dec 4, 2011 at 08:19, Michael Fellinger <m.fellinger@gmail.com> wrote:
On Sun, Dec 4, 2011 at 1:47 PM, Mc Ben <viertelvor12@gmx.net> wrote:
--
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)
D'oh! You're right, sorry, I glossed right over that. That must be
why the "directly" in the previous message, as opposed this being
indirect....
-Dave
···
On Sun, Dec 4, 2011 at 21:47, Joel VanderWerf <joelvanderwerf@gmail.com> wrote:
In your case, the "do end" block is passed to the #each call, not to the #open call.
--
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)
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
···
On Mon, Dec 5, 2011 at 10:54 PM, Ankush Ganatra <ankush_ganatra@yahoo.in>wrote:
Hi
I was just going through various concepts fo Ruby and found one
interesting (but not so useful in real world ) concept of Ruby, i.e. catch
and throw
I tried on of the example in the book..
def dothings( aNum )
i = 0
while true
puts( "I'm doing things..." )
throw( :go_for_tea ) if (i == aNum )
end
end
catch( :go_for_tea ){ puts "caught here"}
dothings(0)