Odd behaviour concerning procs.

Discussion in 'Ruby' started by Christophe Poucet, Oct 14, 2005.

  1. Hello,

    I've noticed the following behaviour and I'm not certain if this is
    expected behaviour, however from the point of view of any other
    language, it certainly seems odd.

    Assume there is no constant with the name 'X' defined anywhere.

    X => NameError: uninitialized constant X
    proc{|X| true}.call(15) => true
    X => 15
    proc{|X| true}.call(15) # true, warning: already initialized constant
    X

    I would presume that the X would be localized to the proc. I know that
    for normal variables, the variable in the calling scope is only changed
    if it already exists, but in this case, the constant X does not even
    exist. (And when it does exist, is it really preferred behaviour that a
    proc will change a constant?)

    With regards,
    Christophe
    Christophe Poucet, Oct 14, 2005
    #1
    1. Advertising

  2. Christophe Poucet wrote:
    > Hello,
    >
    > I've noticed the following behaviour and I'm not certain if this is
    > expected behaviour, however from the point of view of any other
    > language, it certainly seems odd.
    >
    > Assume there is no constant with the name 'X' defined anywhere.
    >
    > X => NameError: uninitialized constant X
    > proc{|X| true}.call(15) => true
    > X => 15
    > proc{|X| true}.call(15) # true, warning: already initialized constant
    > X
    >
    > I would presume that the X would be localized to the proc. I know that
    > for normal variables, the variable in the calling scope is only changed
    > if it already exists, but in this case, the constant X does not even
    > exist. (And when it does exist, is it really preferred behaviour that a
    > proc will change a constant?)


    This doesn't seem odd to me. I would read that as the equivalent of

    proc{|a| self.class.const_set:)X, a); true}.call(15)

    and that certainly defines the constant. Constants are not like local
    variables - they don't have scopes. Instead, they exist on objects,
    like class and instance variables. So there's no way a constant could
    only exist inside the proc and remain undefined outside it.
    Kevin Ballard, Oct 14, 2005
    #2
    1. Advertising

  3. Christophe Poucet wrote:
    > Hello,
    >
    > I've noticed the following behaviour and I'm not certain if this is
    > expected behaviour, however from the point of view of any other
    > language, it certainly seems odd.
    >
    > Assume there is no constant with the name 'X' defined anywhere.
    >
    > X => NameError: uninitialized constant X
    > proc{|X| true}.call(15) => true
    > X => 15
    > proc{|X| true}.call(15) # true, warning: already initialized
    > constant X
    >
    > I would presume that the X would be localized to the proc. I know
    > that for normal variables, the variable in the calling scope is only
    > changed if it already exists, but in this case, the constant X does
    > not even exist. (And when it does exist, is it really preferred
    > behaviour that a proc will change a constant?)


    Basically the part between || leads to a normal assignment. So this
    behavior is pretty much in line with other behavior (i.e. for local vars).
    I guess the difference comes from the fact that lookup for constants is
    done a bit differently. This allows to do what you cannot do with a local
    var. It's odd, yes.

    Kind regards

    robert
    Robert Klemme, Oct 14, 2005
    #3
    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. Griff
    Replies:
    3
    Views:
    703
    S.M. Altaf [MVP]
    Dec 3, 2005
  2. Michael Speer

    Odd behavior with odd code

    Michael Speer, Feb 16, 2007, in forum: C Programming
    Replies:
    33
    Views:
    1,048
    Richard Heathfield
    Feb 18, 2007
  3. Griff

    Newbie - question concerning page behaviour

    Griff, Nov 23, 2005, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    133
    Devone
    Nov 25, 2005
  4. Griff

    Newbie - question concerning page behaviour

    Griff, Nov 23, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    108
    Devone
    Nov 25, 2005
  5. matt
    Replies:
    1
    Views:
    233
    George Ogata
    Aug 6, 2004
Loading...

Share This Page