a selfish ruby question

D

Dave Rose

why is it that:
class Akill
def astop(seq)
self.select{|astop| astop.first==seq}.collect
end
end => nil
grr.astop(b[0])
i get this error....
NoMethodError: private method `select' called for #<Akill:0x2dd01f8>
from (irb):86:in `astop'
from (irb):89
from ?:0...and when i do this:

class Akill
def astop(seq)
@arte.select{|astop| astop.first==seq}.collect
end
end

i don't....?

...and what is the difference in it's use if both worked...
i mean...what is the difference between making a method
that uses 'self' and one that uses a instance variable....

when would one to want to design a method with self and/or with a
instance variable?

....and even though i add to the Akill class:

attr_reader :arte, :stops, :rtes

...i still get these errors....
class Akill
def astop(seq)
self.select{|astop| astop.first==seq}.collect
end
end => nil
grr.arte.astop(b[0])
NoMethodError: undefined method `astop' for #<Array:0x2dd01b0>
from (irb):143
from :0
NameError: undefined local variable or method `b0' for main:Object
from (irb):144
from :0
NoMethodError: private method `select' called for #<Akill:0x2dd01f8>
from (irb):140:in `astop'
from (irb):145
from :0
 
P

Phrogz

You can't have an explicit receiver for private methods. See below.

class Foo
def try1
something_private()
end

def try2
self.something_private()
end

private
def something_private()
puts "I'm doing private things!"
end
end

f = Foo.new
f.try1
#=> I'm doing private things!

f.try2
#=> tmp.rb:7:in `try2': private method `something_private' called for
#<Foo:0x282dd70> (NoMethodError)
#=> from tmp.rb:19

f.something_private
#=> tmp.rb:21: private method `something_private' called for
#<Foo:0x282ddd4> (NoMethodError)
 
D

Dave Rose

Phrogz said:
You can't have an explicit receiver for private methods. See below.
in trying to understand this....
why is it then the Array.select instance method is private? here?....
 
P

Phrogz

Dave said:
in trying to understand this....
why is it then the Array.select instance method is private? here?....

You need to provide more details. You have explicitly stated that you
have another class, not an Array. Does your class inherit from Array?

Create the smallest program you can that showcases your problem, and
perhaps then we can help you.
 
R

Rick DeNatale

in trying to understand this....
why is it then the Array.select instance method is private? here?....

It's not.

Quoting from your original post
grr.arte.astop(b[0])
NoMethodError: undefined method `astop' for #<Array:0x2dd01b0>
from (irb):143
from :0

Here grr.arte is returning an Array, and Array doesn't have an astop method.
grr.astop([b0])
NameError: undefined local variable or method `b0' for main:Object
from (irb):144
from :0

Did you mean to say b[0] instead of b0?
grr.astop(b[0])
NoMethodError: private method `select' called for #<Akill:0x2dd01f8>
from (irb):140:in `astop'
from (irb):145
from :0

I guess that you've defined select as a private method in Akill or one
of it's superclasses. It's not complaining about select being a
private method of Array, but of Akill.

As has been pointed out, it's illegal to use an explicit receiver on
private methods.
This is the way that the Ruby runtime enforces privacy. So

self.publicMethod
is the same as just
publicMethod

but
privateMethod
works and
self.privateMethod raises an exception.
 
L

Logan Capaldo

I guess that you've defined select as a private method in Akill or one
of it's superclasses. It's not complaining about select being a
private method of Array, but of Akill.

This select is most likely Kernel#select (as in IO).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,211
Messages
2,571,092
Members
47,693
Latest member
david4523

Latest Threads

Top