Understanding Ruby class vs instance methods

Posted by randombits on Stack Overflow See other posts from Stack Overflow or by randombits
Published on 2010-04-20T02:07:45Z Indexed on 2010/04/20 2:13 UTC
Read the original article Hit count: 306

Filed under:

I have the following code:

#!/usr/bin/ruby

class Person
  def self.speak
    p = self.new
    puts "Hello"
    p.chatter
  end

private

  def chatter
    puts "Chattering"
  end
end

p = Person.new
Person.speak

I'd like to make chatter private, accessible only within p.. but I want p to be able to access it within the class method. Is there a better way to design this so chatter isn't available to the public, but a "factory" method like self.speak can call chatter?

© Stack Overflow or respective owner

Related posts about ruby