On 20/01/06, Tom A. [email protected] wrote:
I’m still reading “the book” and am trying to see if I got this right.
Sometimes the book isn’t direct enough on “this is how you will be programming
in ruby”.
This is, in part, because not everyone programs in Ruby quite the same
way. I know that my style is very distinctive from others’ style (and
my style is pretty “mainstream,” nonetheless).
Is there anything like perl strict pragma in ruby?
Not quite, but Ruby is more strict than Perl in any case. Running ruby
-w (even in your bangpath line) will give you more information.
Scope. It seems that there are some different approaches to scope. For
example, objects created in a loop remain in scope after you leave the scope of
the iterator. So my favorite temporary variables of i, x, foo, and bar are all
now permanent variable elements in my code blocks from their first use.
True/False?
I believe that if you do a “for” loop, your statement is true.
irb(main):001:0> (1…10).each { |i| }
=> 1…10
irb(main):002:0> i
NameError: undefined local variable or method `i’ for main:Object
from (irb):2
irb(main):003:0> for i in 1…10
irb(main):004:1> end
=> 1…10
irb(main):005:0> i
10
The idiomatic way to loop in Ruby is with #each or other iterator items.
Now, if you’ve defined i outside of your iterator, current Ruby will
overwrite it.
irb(main):001:0> i = 0
=> 0
irb(main):002:0> (1…10).each { |i| }
=> 1…10
irb(main):003:0> i
=> 10
I believe that there will be changes to this moving forward, but I
can’t remember the exact scope changes.
-austin