nil is not nil... (because j != j)

Hi!

i=0; 10.times { j=1; p i+=1 } # -> j=nil!
# So setting j ten times doesn't even help: j==nil.... :(((((
# ALSO when doing it many times (=> after shown that j==nil) !!!!
# shouldn't that behaviour being changed?

But: i=0; j=nil; 10.times..... # => j==1 #OK (as expected)

Why to not use Ruby???

Opti

Hi,

Not sure what your question is but:

i=0; 10.times { j=1; p i+=1 } # -> j=nil!

In this example j does not even exist outside the scope the block passed to
times, so it is not nil but you will get a NameError when you execute this
in the irb

i=0; j=nil; 10.times { j=1; p i+=1 }

This of course works since j is defined.

···

On Wed, May 25, 2022 at 10:23 AM Die Optimisten <inform@die-optimisten.net> wrote:

Hi!

i=0; 10.times { j=1; p i+=1 } # -> j=nil!
# So setting j ten times doesn't even help: j==nil.... :(((((
# ALSO when doing it many times (=> after shown that j==nil) !!!!
# shouldn't that behaviour being changed?

But: i=0; j=nil; 10.times..... # => j==1 #OK (as expected)

Why to not use Ruby???

Opti

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Right, following the same line that Lars said before, your variables have
different scopes considering where you're declaring them. If you're
declaring a variable inside a block or method it'll be visible into that
scope only.

···

On Wed, May 25, 2022 at 8:50 AM Lars Vonk <lars.vonk@gmail.com> wrote:

Hi,

Not sure what your question is but:

i=0; 10.times { j=1; p i+=1 } # -> j=nil!

In this example j does not even exist outside the scope the block passed
to times, so it is not nil but you will get a NameError when you execute
this in the irb

i=0; j=nil; 10.times { j=1; p i+=1 }

This of course works since j is defined.

On Wed, May 25, 2022 at 10:23 AM Die Optimisten <inform@die-optimisten.net> > wrote:

Hi!

i=0; 10.times { j=1; p i+=1 } # -> j=nil!
# So setting j ten times doesn't even help: j==nil.... :(((((
# ALSO when doing it many times (=> after shown that j==nil) !!!!
# shouldn't that behaviour being changed?

But: i=0; j=nil; 10.times..... # => j==1 #OK (as expected)

Why to not use Ruby???

Opti

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

--
Grato!
Wender Freese

Yes, BUT it (the inner variable j) is "visible" to the outside:
it exists and is set to NIL, which is confusing -
it shoudn't exist outside, or it should have the correct value (1 in
this case)!

This should be corrected?

···

Am 25.05.22 um 14:49 schrieb Wender Freese:

Right, following the same line that Lars said before, your variables
have different scopes considering where you're declaring them. If
you're declaring a variable inside a block or method it'll be visible
into that scope only.

You probably have already defined j somewhere.
Try it in a new context, such as a new irb session:

3.1.1 :001 > i=0; 10.times { j=1; p i+=1 }
1
2
3
4
5
6
7
8
9
10
=> 10
3.1.1 :002 > j
(irb):2:in `<main>': undefined local variable or method `j' for main:Object
(NameError)

-gf-

···

On Wed, May 25, 2022 at 12:31 PM Die Optimisten <inform@die-optimisten.net> wrote:

Am 25.05.22 um 14:49 schrieb Wender Freese:
> Right, following the same line that Lars said before, your variables
> have different scopes considering where you're declaring them. If
> you're declaring a variable inside a block or method it'll be visible
> into that scope only.
>
Yes, BUT it (the inner variable j) is "visible" to the outside:
it exists and is set to NIL, which is confusing -
it shoudn't exist outside, or it should have the correct value (1 in
this case)!

This should be corrected?

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Seems it is set to nil by pry, not a problem of Ruby itself (who has
thought of this?!)

···

Am 25.05.22 um 18:31 schrieb Die Optimisten:

Am 25.05.22 um 14:49 schrieb Wender Freese:

Right, following the same line that Lars said before, your variables
have different scopes considering where you're declaring them. If
you're declaring a variable inside a block or method it'll be visible
into that scope only.

Yes, BUT it (the inner variable j) is "visible" to the outside:
it exists and is set to NIL, which is confusing -
it shoudn't exist outside, or it should have the correct value (1 in
this case)!

This should be corrected?

The evidence is accumulating...

In pry:
[1] pry(main)> i=0; 10.times { j=1; p i+=1 }

                                                1
2
3
4
5
6
7
8
9
10
=> 10
[2] pry(main)> j

                                                NameError: undefined local
variable or method `j' for main:Object
from (pry):2:in `__pry__'
[3] pry(main)>

-gf-

···

On Wed, May 25, 2022 at 12:48 PM Die Optimisten <inform@die-optimisten.net> wrote:

Seems it is set to nil by pry, not a problem of Ruby itself (who has
thought of this?!)

Am 25.05.22 um 18:31 schrieb Die Optimisten:
> Am 25.05.22 um 14:49 schrieb Wender Freese:
>> Right, following the same line that Lars said before, your variables
>> have different scopes considering where you're declaring them. If
>> you're declaring a variable inside a block or method it'll be visible
>> into that scope only.
>>
> Yes, BUT it (the inner variable j) is "visible" to the outside:
> it exists and is set to NIL, which is confusing -
> it shoudn't exist outside, or it should have the correct value (1 in
> this case)!
>
> This should be corrected?
>

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;