<html><head><meta http-equiv="Content-Type" content="text/html charset=windows-1252"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Hello,<div><br><div apple-content-edited="true">
<div style="color: rgb(0, 0, 0); font-family: Helvetica; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; border-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; border-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><br></div></div></span></div></span></div></div><div><div>On 18-Jul-2013, at 1:21 PM, Rob Miller <<a href="mailto:rob@bigfish.co.uk">rob@bigfish.co.uk</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><meta http-equiv="content-type" content="text/html; charset=utf-8"><div dir="auto"><div>Thanks, that clears some things up — but I'm still left with the question of why you'd do that rather than just defining a global method? Seems an unnecessary hoop to jump through. </div></div></blockquote><br></div><div>In Ruby no matter where you are, you always have a `self` object. When you inside a instance method of a class, self is the instance of that class. When you are defining a class method, `self` is the class. </div><div><br></div><div>Ruby also has a concept of "default definee". When you define a method without an explicit receiver, the method is defined as instance method in the "default definee" class. </div><div><br></div><div>When you are in global scope, the self is an object called 'main'.  Object 'main' is a very special object in Ruby. It is an instance of class `Object` itself and very curiously has dual nature, which means it can behave as an object and sometimes as a class.</div><div><br></div><div>Banister has written some good material on nature of this "main" object:</div><div><br></div><div><a href="http://banisterfiend.wordpress.com/2010/11/23/what-is-the-ruby-top-level/">http://banisterfiend.wordpress.com/2010/11/23/what-is-the-ruby-top-level/</a></div><div><br></div><div>So what does this leaves us with? global methods defined in "main" object are mixed into Object class as private instance methods and hence are available everywhere but as private method (other than object main itself).</div><div><br></div><div>In a nutshell:</div><div><br></div><div>def foo; end</div><div><br></div><div>and </div><div><br></div><div>module Kernel</div><div>  def foo; end</div><div>end</div><div><br></div><div>Are not same at all. :-)</div><div><br></div><br></div><div><br></div><div><div>Cheers</div><div>Hemant</div><div><a href="http://twitter.com/gnufied">http://twitter.com/gnufied</a></div><div><a href="http://www.codemancers.com">http://www.codemancers.com</a></div></div><div><br></div></body></html>