From: "David A. Black" Date: 2005-11-07T00:54:33+09:00 Subject: Re: what does --can't define singleton method-- mean? --8323328-795131054-1131292468=:21554 Content-Type: MULTIPART/MIXED; BOUNDARY="8323328-795131054-1131292468=:21554" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --8323328-795131054-1131292468=:21554 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Hi -- On Mon, 7 Nov 2005, Daniel Sch=FCle wrote: > Sean O'Halpin wrote: >> On 11/6/05, Daniel Sch=FCle wrote: >>=20 >>> irb(main):014:0* class << Complex >>=20 >>=20 >> You're using the wrong syntax here. To re-open a class, just use >> normal class definition syntax: >>=20 >> class Complex >> def angle >> arg/Math::PI*180.0 >> end >> end > > I found out that I also can use > > Complex.class_eval do > =09def angle > =09=09arg / Math::PI * 180 > =09end > end > > this would "update" the method for all instances of Complex class Or: def Complex.angle ... > class << Complex > def angle; "angle"; end > end > > would create singleton_method for Complex class itself > Complex.angle =3D> "angle" > > what is the advantage or typical use of having singleton method for a cla= ss? Singleton methods of classes are generally referred to as "class methods", and they're actually the most frequent use-case for singleton methods. You'll find a lot of them in the core. Here are some examples: Regexp.escape(string) File.open(filename) Math.sqrt(num) # Math is actually a Module, not a Class, but same # idea Class.new etc. David --=20 David A. Black dblack@wobblini.net --8323328-795131054-1131292468=:21554-- --8323328-795131054-1131292468=:21554--