More about methods, it’s inspired/spurred by a comment on methods, public_methods, and private_methods by Pit Captain. It also corrects some misconceptions I had (and may have (wrongly) given others).
I’ve added a new category, “eating crow” for this and any other postings where I step back and re-evaluate my posts. This is to keep me honest, and, if y’all would, please feel free to tell me when it’s time to “eat crow”.
I’d gotten a little confused. Seeing #initialize
as a private method of Object
(the class) I made the (wrong) assumption that an instance was inheriting it. Here’s what led to my downfall:
1 2 3 4 5 |
irb(main):038:0> Object.private_methods.include? "initialize" => true irb(main):039:0> Object.new.private_methods.include? "initialize" => true irb(main):040:0> |
However, these methods are not the same:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
irb(main):010:0> class Foo irb(main):011:1> def Foo.bar irb(main):012:2> end irb(main):013:1> end => nil irb(main):014:0> class Fud < Foo irb(main):015:1> end => nil irb(main):016:0> f=Fud.new => #<Fud:0x2a95616a88> irb(main):017:0> f.methods => ["inspect", "my_methods", "tap", "clone", "public_methods", "__send__", "object_id", "instance_variable_defined?", "equal?", "freeze", "extend", "send", "methods", "where_is_this_defined", "hash", "dup", "to_enum", "instance_variables", "eql?", "instance_eval", "id", "singleton_methods", "taint", "enum_for", "frozen?", "instance_variable_get", "instance_of?", "display", "to_a", "method", "type", "instance_exec", "protected_methods", "==", "===", "instance_variable_set", "kind_of?", "respond_to?", "to_s", "class", "__id__", "tainted?", "=~", "private_methods", "untaint", "nil?", "is_a?"] irb(main):018:0> f.methods.include? "bar" => false irb(main):019:0> Fud.methods.include? "bar" => true irb(main):020:0> g=Foo.new => #<Foo:0x2a955fede8> irb(main):021:0> g.methods.include? "bar" => false |
We can see that the class method Foo#bar
is not accessible within the instance — it would need to be explicitly called.
Also, below you can see that both the class and the instance can have, due to the magick of scope, the same method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
irb(main):022:0> class Groo irb(main):023:1> def Groo.bar irb(main):024:2> puts "I've got class" irb(main):025:2> end irb(main):026:1> def bar irb(main):027:2> puts "I don't have class" irb(main):028:2> end irb(main):029:1> end => nil irb(main):030:0> Groo.bar I've got class => nil irb(main):031:0> g=Groo.new => #<Groo:0x2a9568bef0> irb(main):032:0> g.bar I don't have class => nil irb(main):033:0> class Gruff < Groo irb(main):034:1> end => nil irb(main):035:0> Gruff.bar I've got class => nil irb(main):036:0> h=Gruff.new => #<Gruff:0x2a95669760> irb(main):037:0> h.bar I don't have class => nil |
As an aside, #initialize
is by definition a private method:
initialize
is a special method in Ruby programs. When you call Song.new to create a new Song object, Ruby creates an uninitialized object and then calls that object’s initialize method, passing in any parameters that were passed to new. This gives you a chance to write code that sets up your object’s state. — http://www.rubycentral.com/book/tut_classes.html
1 comment
1 ping
Eric
September 13, 2008 at 12:12 am (UTC -5) Link to this comment
You say: We can see that the class method Foo#bar is not accessible within the instance — it would need to be explicitly called.
In Ruby notation, Class#method is typically used to indicate an instance method and Class::method to indicate a class method.
Also, classes and instances *do* actually meet in Ruby. A class is actually an instance of a class named Class.
irb(main):001:0> class Foo
irb(main):002:1> end
=> nil
irb(main):003:0> f = Foo.new
=> #
irb(main):004:0> f.class
=> Foo
irb(main):005:0> Foo.class
=> Class
Ramblings » Blog Archive » methods, public_methods, and private_methods
September 12, 2008 at 3:11 pm (UTC -5) Link to this comment
[…] it’s not a public method of the instance, but rather a private method of the class(Note: see Class is Class and Instance, Instance, and never the twain shall meet for why the edit is here): irb(main):013:0> MethodsTest.private_methods.include? […]