Is anyone using -w?

Run your tests with -w. Do something when they don't run clean. Start with simple pull requests.

···

On Jun 25, 2014, at 0:01, Adam Wenham <adamwenham64@gmail.com> wrote:

Thanks Ryan, that actually sounds pretty interesting to me. I don't really know where to start though, so how could I get involved?

Why would you? bar= _is_ defined.

Btw. 1.9.3 also did not choke with this.

Kind regards

robert

···

On Thu, Jun 26, 2014 at 10:04 AM, Steve Tooke <steve.tooke@gmail.com> wrote:

On Thu, Jun 26, 2014 at 7:14 AM, Eric Christopherson > <echristopherson@gmail.com> wrote:

On Wed, Jun 25, 2014, Ryan Davis wrote:
>
> You can't call a private writer (without send) because private means you can't use a receiver.

Hmm; that doesn't seem to be the case at least in 2.1.1:

    class Foo
      private
      attr_accessor :bar

      public
      def set_bar(b)
        self.bar = b
      end

      def get_bar
        bar
      end
    end

    f = Foo.new
    f.set_bar(42) # no warning except under -w
    puts f.get_bar # outputs 42

Perhaps I misunderstand you, and you meant that using self as a receiver
for a private method merely triggers a warning under -w.

Oh that's interesting, I would expect you to get a NoMethodError for
`self.bar = b`

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

Why would you? bar= _is_ defined.

That's how ruby responds when you try to call a private method using
the explicit self receiver:

class Foo
  def use_bar_explicit_self
    self.bar
  end

  private
  def bar
    'bar'
  end
end

foo = Foo.new
foo.use_bar_explicit_self

# ~> -:3:in `use_bar_explicit_self': private method `bar' called for
#<Foo:0x007fe9c982cd68> (NoMethodError)
# ~> from -:13:in `<main>'

This works in exactly the same way when you pass a variable to a method:

class Foo
  def use_bar_explicit_self(var)
    self.bar(var)
  end

  private
  def bar(var)
    'bar'
  end
end

foo = Foo.new
foo.use_bar_explicit_self(:a_var)

# ~> -:3:in `use_bar_explicit_self': private method `bar' called for
#<Foo:0x007fdfea828a30> (NoMethodError)
# ~> from -:13:in `<main>'

But the same rules don't seem to follow when using the `=` operator

class Foo
  def value
    @bar
  end

  def use_bar_explicit_self=(var)
    self.bar=(var)
  end

  private
  def bar=(var)
    @bar = var
  end
end

foo = Foo.new
foo.use_bar_explicit_self = :a_var
foo.value # => :a_var

I'm guessing that the `=` operator essentially results in doing a send
to `"#{method_name}=` and therefore is bypassing ruby's usual method
visibility rules.

Btw. 1.9.3 also did not choke with this.

Yep - also 1.8.7 and rubinius 2.2.2 - so I guess this is expected
behaviour. It just surprised _me_ :slight_smile:

···

On Thu, Jun 26, 2014 at 10:58 AM, Robert Klemme <shortcutter@googlemail.com> wrote:

--
/tooky

Why would you? bar= _is_ defined.

That's how ruby responds when you try to call a private method using
the explicit self receiver:

Oh right. I thought it was another error. I just looked at the
message. I'm sorry.

But the same rules don't seem to follow when using the `=` operator

Right. Apparently that is handled specially. That makes sense if you
consider that there is no other way to invoke assignment methods other
than prefixing with "self.".

I'm guessing that the `=` operator essentially results in doing a send
to `"#{method_name}=` and therefore is bypassing ruby's usual method
visibility rules.

I don't think it necessarily needs to go through #send to allow this
to happen. There just needs to be a different call chain for things
like "self.{identifier}={expression}". But we can test:

$ ruby xx.rb
42
SEND
43
$ cat xx.rb
class Foo
  def send(*x) puts "SEND"; super end

  private
  attr_accessor :bar

  public
  def set_bar(b)
    self.bar = b
  end

  def set_bar_2(b)
    send("bar=", b)
  end

  def get_bar
    bar
  end
end

f = Foo.new
f.set_bar(42) # no warning except under -w
puts f.get_bar # outputs 42
f.set_bar_2(43)
puts f.get_bar # outputs 43

Voilá!

Btw. 1.9.3 also did not choke with this.

Yep - also 1.8.7 and rubinius 2.2.2 - so I guess this is expected
behaviour. It just surprised _me_ :slight_smile:

I also was not aware. Learn something new every day. :slight_smile:

Kind regards

robert

···

On Thu, Jun 26, 2014 at 1:02 PM, Steve Tooke <steve.tooke@gmail.com> wrote:

On Thu, Jun 26, 2014 at 10:58 AM, Robert Klemme > <shortcutter@googlemail.com> wrote:

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