Re: eruby

Mauricio Fernández <[email protected]> Mon, 7 Apr 2003 00:10:29 +0200
Newsgroups gmane.comp.lang.ruby.german
Message-ID <[email protected]>
On Sat, Apr 05, 2003 at 11:59:44PM +0200, Tobias Pape wrote:
> 
> und was istz dieses class << foo, ich dachte vererbung sei mit
> class < foo???? 

Ist ja so :)

class << obj kann benutzt werden, um singleton Methoden zu einem Objekte
hinzuzufügen.  Die sind Methoden, die nur einem einzigen Objekte gehören.

batsman@kodos:/tmp$ expand -t2 c.rb
class A; end
a = A.new
class << a
  def foo
    puts "singleton method von a"
  end
end

a.foo

# Jetzt definieren wir eine Klassenmethode

class << A
  def foo
    puts "A.foo"
  end
  # wie
  # def A.foo; puts "A.foo" end
end

A.foo

batsman@kodos:/tmp$ ruby c.rb
singleton method von a
A.foo


-- 
 _           _                             
| |__   __ _| |_ ___ _ __ ___   __ _ _ __  
| '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ 
| |_) | (_| | |_\__ \ | | | | | (_| | | | |
|_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
	Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

"Who is General Failure and why is he reading my hard disk?"
Microsoft spel chekar vor sail, worgs grate !!
	-- Felix von Leitner, [email protected]