[LRUG] Global methods vs. Kernel mixins

Rob Miller rob at bigfish.co.uk
Wed Jul 17 12:09:25 PDT 2013


Hi guys and gals,

I'm sure this is an elementary problem, but my recent discovery and 
subsequent love of Ara Howard's "main"[1] got me digging through its 
source code to help advance my basic Ruby knowledge.

I wondered how main's DSL was created. For example, the main wrapper is 
called as:

	Main {
		# code here
	}

It seemed obvious to me that it was a method called Main that accepted a 
block, but I wondered about how exactly it was defined. It turns out the 
definition is:

	module Kernel
	private
	  def Main(*args, &block)
     	# code here
	  end
	end

Now I understand what the Kernel module is (it's mixed into every 
object, so its methods are global? Correct me if I'm wrong!), and I 
understand the implications of adding a private method to Kernel (it can 
only be called as `Kernel.foo` or totally unprefixed, it doesn't show up 
as a method on all objects?).

But what's the difference between doing this versus just doing:

	def Main(*args, &block)
		# code here
	end

Surely both ways end up with a global method that's callable from 
everywhere? Why jump through the hoop of mixing-in to Kernel? Are there 
any advantages?

Is this some scope-related problem, linked to the fact that he defines a 
module called Main also (so he couldn't defined a method with the same 
name)?

Best,
Rob

[1]: https://github.com/ahoward/main
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20130717/01baf205/attachment.html>


More information about the Chat mailing list