[#3006] mismatched quotation — "stevan apter" <apter@...>

ruby documentation uses a punctuation convention i've never seen

13 messages 2000/05/27

[ruby-talk:02775] Re: More code browsing questions

From: Dave Thomas <Dave@...>
Date: 2000-05-12 05:57:07 UTC
List: ruby-talk #2775
Albert Wagner <alwagner@tcac.net> writes:

> Thanks again, Dave. I understand your example, but not the following
> code from ext/tk/lib/tkafter.rb:
> :
> require 'tk'
> 
> class TkAfter
>   include TkCore
>   extend TkCore

Ah - now you need the chapter I just wrote on classes and objects.

In Ruby, class definitions are executable code, not just lame old
declarations. So, while it is trundling through

  class TkAfter
    :
  end

Ruby is executing code. In this case, 'self' is actually set to
reference TkAfter, which is an object of type class.

  class TkAfter
    puts self.type
    puts self.name
  end

  => Class
  => TkAfter

The other thing to know is that a method called with no receiver has
an implicit receiver of 'self', so matz's Tk code is equivalent to

  class TkAfter
    [self.]include TkCore
    self.extend  TkCore

  end

(the reason for the [self.] is that 'include' is a private method, and 
can't in reality be called with a receiver).

So, what does this incantation do? Let's try it.

  module TkCore
    def tclMe
      puts "Hee Hee!"
    end
  end

  class TkAfter
    include TkCore
    extend  TkCore
  end

  tka = TkAfter.new
  tka.tclMe           # => Hee Hee!

  TkAfter::tclMe      # => Hee Hee!


So, by including the module, you make its methods available as
instance methods. By extending yourself with it, you make its methods
available as class methods.



Dave

In This Thread