strict pragma & scope

Discussion in 'Ruby' started by Tom Allison, Jan 20, 2006.

  1. Tom Allison

    Tom Allison Guest

    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".

    Is there anything like perl strict pragma in ruby?

    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?
    Tom Allison, Jan 20, 2006
    #1
    1. Advertising

  2. Tom Allison 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".
    >
    > Is there anything like perl strict pragma in ruby?


    Not exactly. Ruby has command line option -w that will help with
    warnings.

    > 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?


    Both: it depends on how you do it.

    >> def foo() 5.times {|i| p i}; puts "after", i end

    => nil
    >> foo

    0
    1
    2
    3
    4
    NameError: undefined local variable or method `i' for main:Object
    from (irb):4:in `foo'
    from (irb):5
    from :0

    In this case you have to declare i before the loop block to see it
    afterwards.

    >> def foo() for i in 0..5 do p i end; puts "after", i end

    => nil
    >> foo

    0
    1
    2
    3
    4
    5
    after
    5
    => nil

    Kind regards

    robert
    Robert Klemme, Jan 20, 2006
    #2
    1. Advertising

  3. On 20/01/06, Tom Allison <> 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 progra=

    mming
    > 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 sc=

    ope of
    > the iterator. So my favorite temporary variables of i, x, foo, and bar a=

    re 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| }
    =3D> 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
    =3D> 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 =3D 0
    =3D> 0
    irb(main):002:0> (1..10).each { |i| }
    =3D> 1..10
    irb(main):003:0> i
    =3D> 10

    I believe that there will be changes to this moving forward, but I
    can't remember the exact scope changes.

    -austin
    --
    Austin Ziegler *
    * Alternate:
    Austin Ziegler, Jan 20, 2006
    #3
  4. Tom Allison

    Gene Tani Guest

    Tom Allison 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".
    >
    > Is there anything like perl strict pragma in ruby?
    >
    > 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?


    this was a good writeup of for and #each
    http://www.sitepoint.com/forums/showthread.php?t=329378
    Gene Tani, Jan 20, 2006
    #4
  5. Tom Allison

    Guest

    On Jan 20, 2006, at 5:41 AM, Tom Allison wrote:
    > 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?


    The non-block looping constructs: while, unless, for/in do *not*
    create a new scope.

    The block looping constructs: each, loop do create a new scope.
    The scoping behavior exists because of the block, not because the
    methods each or loop have some special properties.

    So basically your question is about blocks, not about loops.

    Matz has acknowledge that the behavior of block arguments and block
    local variables
    is confusing and is exploring some alternatives.

    See Austin's post for an illustration of the current behavior of
    block arguments and
    local variables within a block.



    Gary Wright
    , Jan 20, 2006
    #5
  6. Tom Allison

    Tom Allison Guest


    >> 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.
    >



    Thanks for the examples. As soon as I get back to my ruby installed
    machine (not here) I'll play.
    Meanwhile...
    I don't think ruby -w quite does what perls 'use strict' pragma will
    allow.

    Under strict every variable must be identified, if not vartyped.
    my $foo;

    This prevents me from doing something like:

    my $super_ids;
    ...
    print $super_id,"\n";
    (note the typo)
    This is nice when it comes to trying to sort out bugs. Otherwise I'm
    trying to find out why my variable $super_ids keeps coming up empty.=20
    And after a few hours I'll probably realize that somewhere in my code I
    messed up.
    Tom Allison, Jan 20, 2006
    #6
  7. On Jan 20, 2006, at 2:05 PM, Tom Allison wrote:

    > I don't think ruby -w quite does what perls 'use strict' pragma will
    > allow.


    That's correct, -w is similar to Perl's warnings.

    > Under strict every variable must be identified, if not vartyped.
    > my $foo;
    >
    > This prevents me from doing something like:
    >
    > my $super_ids;
    > ...
    > print $super_id,"\n";
    > (note the typo)


    Ruby requires we declare a local variable before we use it, so you
    are all set. ;)

    James Edward Gray II
    James Edward Gray II, Jan 20, 2006
    #7
  8. --Apple-Mail-3-568461337
    Content-Transfer-Encoding: 7bit
    Content-Type: text/plain;
    charset=US-ASCII;
    format=flowed


    On Jan 20, 2006, at 3:05 PM, Tom Allison wrote:

    > my $super_ids;
    > ...
    > print $super_id,"\n";


    In ruby...

    super_ids = nil # assign to declare
    ...
    print super_id, "\n"

    If you run this you'll get
    NameError: undefined local variable or method `super_id' for main:Object


    --Apple-Mail-3-568461337--
    Logan Capaldo, Jan 20, 2006
    #8
  9. On 20/01/06, James Edward Gray II <> wrote:
    > > Under strict every variable must be identified, if not vartyped.
    > > my $foo;
    > >
    > > This prevents me from doing something like:
    > >
    > > my $super_ids;
    > > ...
    > > print $super_id,"\n";
    > > (note the typo)

    >
    > Ruby requires we declare a local variable before we use it, so you
    > are all set. ;)


    s/declare/set/

    There are no variable declarations in Ruby.

    The more likely problem that Tom is to find is with instance
    variables. It would be nice to have some way of detecting missing
    assignments to instance variables, but only have it show up when
    requested.

    -austin
    --
    Austin Ziegler *
    * Alternate:
    Austin Ziegler, Jan 20, 2006
    #9
  10. Logan Capaldo wrote:
    > On Jan 20, 2006, at 3:05 PM, Tom Allison wrote:
    >
    >> my $super_ids;
    >> ...
    >> print $super_id,"\n";

    >
    > In ruby...
    >
    > super_ids = nil # assign to declare
    > ...
    > print super_id, "\n"
    >
    > If you run this you'll get
    > NameError: undefined local variable or method `super_id' for main:Object



    Which basically means that in this "language" there is NO WAY to declare
    a variable. No way to say, "hey dude, I want a new (censored) x in
    here". All you get is a RUNTIME error if you print (or compute with
    or...) a variable sooner than you assign to it. Which is pretty useless.
    And which means that if I do a

    coll.each { |i| blah blah blah}

    there is no telling whether it will use a new local i or some i
    "declared" outside. And there is no way to make sure it does one or the
    other.

    And whenever I need to loop through something I have to make sure i use
    a different variable than any that could ever be "declared" in the same
    scope. Don't you think it's a bit stupid? You probably don't, you
    program in Ruby.

    --
    Posted via http://www.ruby-forum.com/.
    Jenda Krynicky, Feb 28, 2007
    #10
  11. On Feb 28, 2007, at 8:10 AM, Jenda Krynicky wrote:

    > Logan Capaldo wrote:
    >> On Jan 20, 2006, at 3:05 PM, Tom Allison wrote:
    >>
    >>> my $super_ids;
    >>> ...
    >>> print $super_id,"\n";

    >>
    >> In ruby...
    >>
    >> super_ids = nil # assign to declare

    super_ids (plural)
    >> ...
    >> print super_id, "\n"

    super_id (singular)
    >>
    >> If you run this you'll get
    >> NameError: undefined local variable or method `super_id' for
    >> main:Object

    >
    >
    > Which basically means that in this "language" there is NO WAY to
    > declare
    > a variable. No way to say, "hey dude, I want a new (censored) x in
    > here". All you get is a RUNTIME error if you print (or compute with
    > or...) a variable sooner than you assign to it. Which is pretty
    > useless.
    > And which means that if I do a
    >
    > coll.each { |i| blah blah blah}
    >
    > there is no telling whether it will use a new local i or some i
    > "declared" outside. And there is no way to make sure it does one or
    > the
    > other.
    >
    > And whenever I need to loop through something I have to make sure i
    > use
    > a different variable than any that could ever be "declared" in the
    > same
    > scope. Don't you think it's a bit stupid? You probably don't, you
    > program in Ruby.


    No, you just need to know if YOU created such a variable within the
    current lexical scope. Your 'i' in the block will always exist
    INSIDE the block. In Ruby 1.8, that reuses the 'i' that already
    exists in the current scope, but 1.9/2.0 changes this (I believe;
    haven't personally checked this out).

    Of course, if you can't consistently spell your variables the same
    way, the "language" isn't likely to know what you meant.

    -Rob

    Rob Biedenharn http://agileconsultingllc.com
    Rob Biedenharn, Feb 28, 2007
    #11
  12. Rob Biedenharn wrote:
    > On Feb 28, 2007, at 8:10 AM, Jenda Krynicky wrote:
    >
    >> Logan Capaldo wrote:
    >>> On Jan 20, 2006, at 3:05 PM, Tom Allison wrote:
    >>>
    >>>> my $super_ids;
    >>>> ...
    >>>> print $super_id,"\n";
    >>>
    >>> In ruby...
    >>>
    >>> super_ids = nil # assign to declare

    > super_ids (plural)
    >>> ...
    >>> print super_id, "\n"

    > super_id (singular)
    >> or...) a variable sooner than you assign to it. Which is pretty
    >> And whenever I need to loop through something I have to make sure i
    >> use
    >> a different variable than any that could ever be "declared" in the
    >> same
    >> scope. Don't you think it's a bit stupid? You probably don't, you
    >> program in Ruby.

    >


    I touched on the issue of lack of variable declarations in my short
    crtique here:
    http://blog.surfulater.com/2007/02/21/write-ruby-code-faster-with-ed-for-windows/

    Neville Franks, http://www.getsoft.com http://www.surfulater.com

    --
    Posted via http://www.ruby-forum.com/.
    Neville Franks, Feb 28, 2007
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Opal
    Replies:
    12
    Views:
    926
    Paul Opal
    Oct 11, 2004
  2. Talha Oktay
    Replies:
    8
    Views:
    206
  3. Rocky

    variable scope and use strict

    Rocky, Apr 27, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    147
    Tore Aursand
    Apr 27, 2004
  4. Dave Bakhash
    Replies:
    14
    Views:
    189
    Brian McCauley
    Jul 23, 2004
  5. bernd
    Replies:
    2
    Views:
    630
    bernd
    Jan 26, 2012
Loading...

Share This Page