Simple problem

Hi

I thought that should work?!
why do I get nils ('+' not for nil) ???

def xx(*z)
unless x then x=7 end
x=x+1; [x, z]
end

x=3; xx(z)

Thank you
Berg

You should read about scope in Ruby.

robert

···

On Tue, Mar 1, 2016 at 11:35 AM, A Berger <aberger7890@gmail.com> wrote:

Hi

I thought that should work?!
why do I get nils ('+' not for nil) ???

def xx(*z)
unless x then x=7 end
x=x+1; [x, z]
end

x=3; xx(z)

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

I am not really sure about what is being done here. But

  x = 7 unless x

seems to work for assigning if not set

Saurav Kothari

···

On Tue, Mar 1, 2016 at 4:08 PM, Robert Klemme <shortcutter@googlemail.com> wrote:

On Tue, Mar 1, 2016 at 11:35 AM, A Berger <aberger7890@gmail.com> wrote:
> Hi
>
> I thought that should work?!
> why do I get nils ('+' not for nil) ???
>
> def xx(*z)
> unless x then x=7 end
> x=x+1; [x, z]
> end
>
> x=3; xx(z)

You should read about scope in Ruby.

robert

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

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

There's still no point in doing that.

robert

···

On Tue, Mar 1, 2016 at 1:55 PM, SAURAV KOTHARI <sauravkothari2@gmail.com> wrote:

I am not really sure about what is being done here. But

  x = 7 unless x

seems to work for assigning if not set

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

ok, first mistake found :slight_smile:
but why does
   x= ... unless .. work, and
   unless x then x=... end does not?
Thats not what we expect? Suspect!
How to ask if it is set?
(x==nil )? has the same problem!)

Hoping we solve all these problems...
Thx Berg

···

Am 01.03.2016 13:56 schrieb "SAURAV KOTHARI" <sauravkothari2@gmail.com>:

I am not really sure about what is being done here. But

  x = 7 unless x

seems to work for assigning if not set

Saurav Kothari

On Tue, Mar 1, 2016 at 4:08 PM, Robert Klemme <shortcutter@googlemail.com> > wrote:

On Tue, Mar 1, 2016 at 11:35 AM, A Berger <aberger7890@gmail.com> wrote:
> Hi
>
> I thought that should work?!
> why do I get nils ('+' not for nil) ???
>
> def xx(*z)
> unless x then x=7 end
> x=x+1; [x, z]
> end
>
> x=3; xx(z)

You should read about scope in Ruby.

robert

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

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;

defined(x) is what you need to see if a variable is defined.
Some docs here:
http://ruby-doc.org/docs/keywords/1.9/Object.html#method-i-defined-3F

Carlos Alonso | Software Engineer | @calonso <https://twitter.com/calonso&gt;

···

On 1 March 2016 at 15:47, A Berger <aberger7890@gmail.com> wrote:

ok, first mistake found :slight_smile:
but why does
   x= ... unless .. work, and
   unless x then x=... end does not?
Thats not what we expect? Suspect!
How to ask if it is set?
(x==nil )? has the same problem!)

Hoping we solve all these problems...
Thx Berg
Am 01.03.2016 13:56 schrieb "SAURAV KOTHARI" <sauravkothari2@gmail.com>:

I am not really sure about what is being done here. But

  x = 7 unless x

seems to work for assigning if not set

Saurav Kothari

On Tue, Mar 1, 2016 at 4:08 PM, Robert Klemme <shortcutter@googlemail.com >> > wrote:

On Tue, Mar 1, 2016 at 11:35 AM, A Berger <aberger7890@gmail.com> wrote:
> Hi
>
> I thought that should work?!
> why do I get nils ('+' not for nil) ???
>
> def xx(*z)
> unless x then x=7 end
> x=x+1; [x, z]
> end
>
> x=3; xx(z)

You should read about scope in Ruby.

robert

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

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;

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

I agree that it does not correct the entirety of what is wrong here. But as
he pointed out separately, what is bothering him is that 'unless x then x=7
end' does not work as he wants it to.

Saurav Kothari

···

On Tue, Mar 1, 2016 at 7:26 PM, Robert Klemme <shortcutter@googlemail.com> wrote:

On Tue, Mar 1, 2016 at 1:55 PM, SAURAV KOTHARI <sauravkothari2@gmail.com> > wrote:
> I am not really sure about what is being done here. But
>
> x = 7 unless x
>
> seems to work for assigning if not set

There's still no point in doing that.

robert

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

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

Ok, thought defined would only be for methods
How to undef a var ?

Does anyone know details of the other problems? (Why not unless first, why
return doesnt work?)

Thanks
Berg

As to 'unless first': because the parser reads from left to right.

When it sees this:

    unless foo

​if it's not seen an assignment to 'foo' before, it has to assume it's a
function call.

However in this:

    foo = 1 unless foo

it *sees* the assignment before it *sees* the invocation, **even though
it's not executed in that order**, so it knows the 'foo' after 'unless' is
the local variable (not a function.)

The reason your whole function doesn't work is, as has already been stated
in this thread: functions aren't closures, they can't see local variables
in the surrounding scope. ('x' is undefined inside your 'xx') If you fix
the first line, it will always return an array containing: the number 8,
and the array of whatever arguments were passed in.

Cheers

···

On 2 March 2016 at 11:55, A Berger <aberger7890@gmail.com> wrote:

Ok, thought defined would only be for methods
How to undef a var ?

Does anyone know details of the other problems? (Why not unless first, why
return doesnt work?)

Thanks
Berg

--
  Matthew Kerwin
  http://matthew.kerwin.net.au/

As it (unless) is a modifier it should be the last part of the statement

···

On Mar 2, 2016 7:40 AM, "Matthew Kerwin" <matthew@kerwin.net.au> wrote:

On 2 March 2016 at 11:55, A Berger <aberger7890@gmail.com> wrote:

Ok, thought defined would only be for methods
How to undef a var ?

Does anyone know details of the other problems? (Why not unless first,
why return doesnt work?)

Thanks
Berg

As to 'unless first': because the parser reads from left to right.

When it sees this:

    unless foo

​if it's not seen an assignment to 'foo' before, it has to assume it's a
function call.

However in this:

    foo = 1 unless foo

it *sees* the assignment before it *sees* the invocation, **even though
it's not executed in that order**, so it knows the 'foo' after
'unless' is the local variable (not a function.)

The reason your whole function doesn't work is, as has already been stated
in this thread: functions aren't closures, they can't see local variables
in the surrounding scope. ('x' is undefined inside your 'xx') If you fix
the first line, it will always return an array containing: the number 8,
and the array of whatever arguments were passed in.

Cheers
--
  Matthew Kerwin
  http://matthew.kerwin.net.au/

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

​Except when it's a conditional branching control expression:

    unless thingy.empty?
      thingy.do_stuff
    end

... which is how it was written in the OP (except using the esoteric 'unless
<expr> then <expr+>; end' form) Sometimes I miss the colon from 1.8.

See also:
http://docs.ruby-lang.org/en/2.2.0/syntax/control_expressions_rdoc.html

Cheers

···

On 2 March 2016 at 12:14, gaurav lele <lelegaurav@gmail.com> wrote:

As it (unless) is a modifier it should be the last part of the statement

--
  Matthew Kerwin
  http://matthew.kerwin.net.au/

Hi
Does anyone know how to undefine a var?

Where to find the examples of
http://ruby-doc.org/core-2.3.0/sample/drb/README_rdoc.html ?

Thanks
Berg

Hi
Does anyone know how to undefine a var?

​You can't, probably because variables are 'defined' in the parser (not at
execution time.)​ You can set them to 'nil', but you can't make it like
they never existed. [Side note: the definition of variables can be seen as
a side-effect of the fact that variables and functions can have identical
syntax. The parser "defines" a variable when it sees the first assignment;
before which the variable name is interpreted as a function call. All
$global, @@class, and @instance variables are always "defined", even though
they're initially nil, because the unique syntax means the parser never has
to disambiguate.]

The question then becomes: why would you want to undefine a variable?
All that would give you is a parse error (at parse time, not at execution
time.)

Where to find the examples of

File: README.rdoc [Ruby 2.3.0] ?

https://github.com/ruby/ruby/tree/trunk/sample/drb​

Thanks

Berg

​No worries.​

···

On 4 March 2016 at 20:18, A Berger <aberger7890@gmail.com> wrote:

--
  Matthew Kerwin
  http://matthew.kerwin.net.au/

The question then becomes: why would you want to undefine a variable?

All that would give you is a parse error (at parse time, not at execution
time.)

Er, I mean, it would go back to interpreting the bareword as a function
call.

···

On 04/03/2016 9:53 PM, "Matthew Kerwin" <matthew@kerwin.net.au> wrote:

Well, you can change "foo" into "foo()" to make it be interpreted as a method call even though there is a local variable "foo", but there's no way to do that as a bareword anymore.

-Rob

···

On 2016-Mar-4, at 07:09 , Matthew Kerwin <matthew@kerwin.net.au> wrote:

On 04/03/2016 9:53 PM, "Matthew Kerwin" <matthew@kerwin.net.au <mailto:matthew@kerwin.net.au>> wrote:
>
> The question then becomes: why would you want to undefine a variable? All that would give you is a parse error (at parse time, not at execution time.)
>

Er, I mean, it would go back to interpreting the bareword as a function call.

Hello
We can undefine methods and even consts, so I thought it would be possible
for vars too...

Berg