From: Rick DeNatale Date: 2006-10-18T08:07:20+09:00 Subject: Re: include inside and outside classes On 10/16/06, Pit Capitain wrote: > John Ky schrieb: > > Why is it that when I can do this: > > > >> include Math > >> puts sqrt(169) > > > > I can't do this?: > > > >> class X > >> include Math > >> puts sqrt(169) > >> end > > Rick has already written about the differences between class scope and > toplevel scope. (Are you reading this, Tom?) > > With #include you can call the Math methods in instance methods: > > class X > include Math > def m > puts sqrt(169) > end > end > > X.new.m # => 13.0 And the reason that this works is that these functions are module functions which act like a combination of class methods for the module, and private instance methods. The first lets you say Math.sqrt(4) and the second lets you say class A include Math def meth sqrt(4) end end And the way you make a module function in one of your own modules is either like this: module MyModule def foo end module_function :foo end or module MyModule module_function def foo end end The module_function method works like private and protected. With arguments it makes specific methods module_functions, without arguments it makes subsequently defined methods module_functions. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/ IPMS/USA Region 12 Coordinator http://ipmsr12.denhaven2.com/ Visit the Project Mercury Wiki Site http://www.mercuryspacecraft.com/