Yeah, it's whether it reads from a variable or calls a method.

e.g.:

def blah
  x  # equivalent to x() if x is in scope, otherwise "no method defined"
end

def blob
  x = 1
  x  # reads local variable
end

and thanks to the source of these emails

def bozo
  x = x  # returns nil, never calls x()
end

Looking into it a bit more, I was wrong; it's done at parse time, but
per-statement.  So x = x is fine, because it's defined and used in the same
statement.  Any read of x in a statement before it's assigned will fail (or
be a method call).  So the following does not work:

def bling
  return x  # undefined local variable or method `x' (if x isn't defined
elsewhere)
  x = 1
end


On Thu, Jan 14, 2016 at 4:59 PM, Ryan Bigg <[email protected]> wrote:

> I think what you're referring to is if it checks for a local variable or
> method, which only happens when you call it.
>
>
> > On 14 Jan 2016, at 15:39, Clifford Heath <[email protected]>
> wrote:
> >
> > Ugh, yes, sorry, brain fart, I’m thinking of something else.
> > What’s the thing where the parser has to check local usage to decide
> syntax?
> > There’s definitely a reason Ruby needs to know about local variables,
> even
> > if they’re only mentioned in conditional code that’s not hit.
> >
> > Clifford Heath.
> >
> >> On 14 Jan 2016, at 12:00 PM, Ryan Bigg <[email protected]> wrote:
> >>
> >> It won't ever do self.send(:x=, 3). x = 3 is always a local variable
> assignment.
> >>
> >>>> On 14 Jan 2016, at 11:52, Clifford Heath <[email protected]>
> wrote:
> >>>>
> >>>> On 14 Jan 2016, at 9:19 AM, Simon Russell <[email protected]>
> wrote:
> >>>> I'd say this "feature" probably has no useful purpose; I can't
> imagine a language designer deliberately putting it in.  It's more likely
> to cause bugs than joy.
> >>>> I'm not certain, but I'm fairly your "unintended parser
> functionality" is heading in the right direction.
> >>>
> >>> No. I think it’s a necessary feature of the Ruby syntax. When Ruby
> sees “x = 3”, it needs to decide whether you want an assignment to a local
> variable, or “self.send(:x=, 3)”.
> >>>
> >>> So it looks around the local scope for clues about what you meant, and
> when it decides that a word is the name of a local variable, it treats it
> that way throughout the function.
> >>>
> >>> Clifford Heath.
> >>>
> >>>> From memory working on a Ruby compiler, Ruby allocates local
> variables before execution, but doesn't mark where they become "real".  So
> they're essentially real from the start, and get assigned a default value
> of nil.  It makes the whole thing easier to build; it's kind of a shortcut.
> >>>>
> >>>> So, moral of this story is...  Don't assign things twice?  Makes for
> a better design anyway! :)
> >>>>
> >>>> On Thu, Jan 14, 2016 at 5:35 AM, Gareth Townsend <
> [email protected]> wrote:
> >>>> Rubocop has a linter for shadowing variables;
> >>>>
> >>>>
> http://www.rubydoc.info/github/bbatsov/rubocop/Rubocop/Cop/Lint/ShadowingOuterLocalVariable
> >>>>
> >>>> Gareth Townsend
> >>>> http://www.garethtownsend.info
> >>>>
> >>>>> On 13 Jan 2016, at 22:00, David Parry <[email protected]>
> wrote:
> >>>>>
> >>>>> Shadowing variables, which is effectively what you’ve done with
> “misspelt_argument”, is pretty-much always a bad idea in most languages.
> >>>>>
> >>>>> In functional languages, it’s mostly impossible to do without the
> compiler complaining, and for very good reasons, one of which you’ve
> pointed out.
> >>>>>
> >>>>> I suspect that tools like RuboCop would probably pick this sort of
> abomination up. Anyone know if that’s the case?
> >>>>>
> >>>>>
> >>>>>
> >>>>>> On 13 Jan 2016, at 8:41 PM, Tom Hale <[email protected]> wrote:
> >>>>>>
> >>>>>> It took some debugging to come across this one:
> >>>>>>
> >>>>>> irb(main):043:0> w
> >>>>>> NameError: undefined local variable or method `w' for main:Object
> >>>>>> from (irb):43
> >>>>>> from /home/ravi/.rbenv/versions/2.2.3/bin/irb:11:in `<main>'
> >>>>>> irb(main):044:0> w = w
> >>>>>> => nil
> >>>>>> irb(main):045:0>
> >>>>>>
> >>>>>> Yeah, right, so what?
> >>>>>>
> >>>>>> It leads to "find the nil" fun-hunts in cases such as:
> >>>>>>
> >>>>>> irb(main):057:0> mispelt_argument =
> Array(mispelt_argument).flatten.uniq.map(&:name)
> >>>>>> => []
> >>>>>>
> >>>>>> Are there any cases in which this "feature" is actually useful, or
> is it an unintended parser functionality?
> >>>>>>
> >>>>>> Tom
> >>>>>>
> >>>>>> --
> >>>>>> You received this message because you are subscribed to the Google
> Groups "Ruby or Rails Oceania" group.
> >>>>>> To unsubscribe from this group and stop receiving emails from it,
> send an email to [email protected].
> >>>>>> To post to this group, send email to [email protected]
> .
> >>>>>> Visit this group at https://groups.google.com/group/rails-oceania.
> >>>>>> For more options, visit https://groups.google.com/d/optout.
> >>>>>
> >>>>> --
> >>>>> You received this message because you are subscribed to the Google
> Groups "Ruby or Rails Oceania" group.
> >>>>> To unsubscribe from this group and stop receiving emails from it,
> send an email to [email protected].
> >>>>> To post to this group, send email to [email protected].
> >>>>> Visit this group at https://groups.google.com/group/rails-oceania.
> >>>>> For more options, visit https://groups.google.com/d/optout.
> >>>>
> >>>> --
> >>>> You received this message because you are subscribed to the Google
> Groups "Ruby or Rails Oceania" group.
> >>>> To unsubscribe from this group and stop receiving emails from it,
> send an email to [email protected].
> >>>> To post to this group, send email to [email protected].
> >>>> Visit this group at https://groups.google.com/group/rails-oceania.
> >>>> For more options, visit https://groups.google.com/d/optout.
> >>>>
> >>>>
> >>>> --
> >>>> You received this message because you are subscribed to the Google
> Groups "Ruby or Rails Oceania" group.
> >>>> To unsubscribe from this group and stop receiving emails from it,
> send an email to [email protected].
> >>>> To post to this group, send email to [email protected].
> >>>> Visit this group at https://groups.google.com/group/rails-oceania.
> >>>> For more options, visit https://groups.google.com/d/optout.
> >>>
> >>> --
> >>> You received this message because you are subscribed to the Google
> Groups "Ruby or Rails Oceania" group.
> >>> To unsubscribe from this group and stop receiving emails from it, send
> an email to [email protected].
> >>> To post to this group, send email to [email protected].
> >>> Visit this group at https://groups.google.com/group/rails-oceania.
> >>> For more options, visit https://groups.google.com/d/optout.
> >>
> >> --
> >> You received this message because you are subscribed to the Google
> Groups "Ruby or Rails Oceania" group.
> >> To unsubscribe from this group and stop receiving emails from it, send
> an email to [email protected].
> >> To post to this group, send email to [email protected].
> >> Visit this group at https://groups.google.com/group/rails-oceania.
> >> For more options, visit https://groups.google.com/d/optout.
> >
> > --
> > You received this message because you are subscribed to the Google
> Groups "Ruby or Rails Oceania" group.
> > To unsubscribe from this group and stop receiving emails from it, send
> an email to [email protected].
> > To post to this group, send email to [email protected].
> > Visit this group at https://groups.google.com/group/rails-oceania.
> > For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby or Rails Oceania" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/rails-oceania.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
or Rails Oceania" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/rails-oceania.
For more options, visit https://groups.google.com/d/optout.

Reply via email to