From: Abder-Rahman Ali on 13 Jul 2010 11:59 Bruno Antunes wrote: > On 13-07-2010 17:30, Abder-Rahman Ali wrote: >> >> Thanks. > > a = [1,2,3].find {|x| x > 3} > > puts a > > > In this (maybe too simple) example, x is garbage-collected once flow > leaves the block. Thanks Bruno. But, I'm always getting as output: 1 2 3 Even if I type {|x| x < 3} Isn't your program trying to say find the values > 3 ? In other words, why don't I get some sort of error since I'm requesting something not here? > 3 Thanks. -- Posted via http://www.ruby-forum.com/.
From: Abder-Rahman Ali on 13 Jul 2010 12:06 Thanks a lot James for your clarification. Actually, I;m familiar with scoping as I dealt with it in Java, and new to Ruby, and my point was the garbage collection here. So, can I say that "word" in puts "#{word}" after the do-block is garbage collected since I know more have access to it? -- Posted via http://www.ruby-forum.com/.
From: James Harrison on 13 Jul 2010 12:14 On Jul 13, 2010, at 9:59 AM 7/13/10, Abder-Rahman Ali wrote: > Bruno Antunes wrote: >> On 13-07-2010 17:30, Abder-Rahman Ali wrote: >>> >>> Thanks. >> >> a = [1,2,3].find {|x| x > 3} >> >> puts a >> >> >> In this (maybe too simple) example, x is garbage-collected once flow >> leaves the block. > > Thanks Bruno. > > But, I'm always getting as output: > 1 > 2 > 3 > > Even if I type {|x| x < 3} > > Isn't your program trying to say find the values > 3 ? > > In other words, why don't I get some sort of error since I'm requesting > something not here? > 3 > > Thanks The point of the example is that x doesn't exist any more outside of the block. The array correctly returns the output "nil", as no values inside the array are greater than 3. I'm not sure what you're doing wrong: probably a typo somewhere. metatron:~ james$ irb >> a = [1,2,3].find {|x| x > 3} => nil >> puts a nil => nil >> puts x NameError: undefined local variable or method `x' for main:Object from (irb):3
From: James Harrison on 13 Jul 2010 12:15 On Jul 13, 2010, at 10:06 AM 7/13/10, Abder-Rahman Ali wrote: > Thanks a lot James for your clarification. > > Actually, I;m familiar with scoping as I dealt with it in Java, and new > to Ruby, and my point was the garbage collection here. > > So, can I say that "word" in puts "#{word}" after the do-block is > garbage collected since I know more have access to it? > -- > Posted via http://www.ruby-forum.com/. > Java's garbage collected, too. So it's just the same as in there. I imagine you've missed a central feature of Java somewhere ;) The simple difference between garbage collection and non-garbage-collection is what happens when something falls out of scope. In C++, C, other languages that have manual memory management only, you have to free up the RAM assigned to the variable. In Java, Ruby, Python, Perl, those languages, the variable falls out of scope and the interpreter or compiler automatically frees the memory up for you. Go read up on garbage collection. http://en.wikipedia.org/wiki/Garbage_collection_(computer_science) isn't atrocious, but maybe pick up a cheap computer science textbook or something. The relationship you're proposing is essentially correct, though. You no longer have access to "word", so it's garbage collected. The memory associated with it is freed up and your computer goes on living its quiet, binary life, sadly contemplating a time when its memory was full of ones and zeroes in orderly, useful fashion. Best James
From: Abder-Rahman Ali on 13 Jul 2010 12:19 Thanks James. It didn't work me when I wrote your script as follows: a = [1, 2, 3] a.find {|x| x > 3} puts a puts x Why doesn't that give the same output as in the format written by you, while I can see that the logic here is correct? -- Posted via http://www.ruby-forum.com/.
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 5 Prev: Shouldn't this be called a global constant? Next: cheap wholesale ARMANI watch |