From: Logan Capaldo Date: 2006-08-18T14:34:28+09:00 Subject: Re: Perl Junkie: Bad form? On Aug 17, 2006, at 6:05 PM, /usr/ceo wrote: > Eero Saynatkari wrote: >> /usr/ceo wrote: >>> Ok where do I start? :-) A little background... >>> >>> I've been declaring up and down for 6 years now I'm going to stop >>> using >>> Perl and start using Ruby. One thing that has always held me >>> back is >>> not having libraries I've put together in Perl available to me in >>> Ruby. >>> Instead, I've ported little Ruby tricks over into Perl to the >>> extent >>> possible. Now I've decided the only way I'm going to use Ruby is >>> start >>> porting some of the stuff I use in Perl to Ruby. >>> >>> Here's a little bit of a delimma I have going that I hope to >>> illustrate >>> using something simple. >>> >>> I wrote this routine in Perl called "puts" that mostly simulates >>> Ruby's >>> "puts" statement. (I intend to illustrate something bigger than >>> "puts" >>> emulation here, so don't let that drag you in. I'm more >>> interested in >>> the module interaction and EXPORTS [in Perl] and include [in Ruby] >>> interaction...). Here's the puts() routine in Perl: >>> >>> >>> >>> This is what I would rather be doing, generally, for imported >>> utility >>> type routines -- making them look "native." I understand (pretty >>> much) >>> about the separate namespace issue, and namespaces are certainly >>> nice >>> (both in Perl and in Ruby). But sometimes I want to pretend I have >>> something "natively" available to me, and this seemed to work. >>> >>> Ugly? Right? Hackish? A better way? Looking for the Ruby >>> perspective here because this is probably one of my more unsolved >>> delimmas -- how to view, approach and implement cases like this. >> >> The problem is you might be tromping all over the library >> user's namespace, possibly overriding methods etc. It is >> better to give the 'namespace' and then allow the client >> programmer #include the module if they desire shorter access. >> >> Those who remember, I actually advocate allowing the client >> programmer create a namespace for libraries instead of the >> library writer, too, but that is another discussion. > > So are you advocating this (by your last statement especially)? > > my/utils.rb: > > module Utils > def mputs( *args ) > args.each { |n| puts n } > end # def > end # module > > myscript.rb: > > #!/usr/bin/env ruby > > require 'my/utils' > include Utils > > mputs( > "Line 1", > "Line 2", > "Line 3" > ) > > BTW, I am familiar with the fact the one can use "here documents" in > Ruby as well. Again, the simple mputs() routine was intended to > illustrate the require/include interaction -- not to illustrate a way > to write multiple lines (in case someone else comes along and reads it > that way.) > > /usr/ceo > > Yes, this is SOP. The only other thing I would suggest is for modules like Util (where it's just a collection of "functions" and not methods) is you do module Util def mputs(*args) args.each { |item| puts item } end module_function :mputs end This lets you say Util.mputs or include Util mputs similar to the Math module.