U
/usr/ceo
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:
sub puts { for (@_) { print "$_\n" } }
That routine is in a Perl module called Utils.pm under a My directory
with puts() EXPORTed. So in a Perl script, I can:
#!/usr/bin/perl
$|++;
use My::Utils;
puts(
"This is line 1",
"This is line 2",
"This is line 3",
);
Now, in Ruby, I find if I construct something similar in a module, I
might create a utils.rb file, place it in the "my" directory under the
Ruby site_ruby directory and have:
module Utils
def Utils.mputs( *args )
args.each { |n| puts n }
end # def
end # module
Then in a Ruby script:
#!/usr/bin/env ruby
require "my/utils"
Utils.mputs(
"This is line 1",
"This is line 2",
"This is line 3"
)
But I don't like having to write "Utils.mputs". In Perl, I exported
the "puts" routine, so in Perl it's in the main namespace and I can
just say: puts( "A", "B", "C" ), etc. I want to be able to do the same
thing in Ruby. Instead of Utils.mputs( blah ), I want to just write
mputs( blah ). So in Ruby, I did this to the Utils module:
module Utils
def mputs( *args )
args.each { |n| puts n }
end # def
end # module
include Utils
Now in my Ruby script, I can do:
#!/usr/bin/env ruby
require "my/utils"
mputs(
"This is line 1",
"This is line 2",
"This is line 3"
)
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.
Thanks!
/usr/ceo
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:
sub puts { for (@_) { print "$_\n" } }
That routine is in a Perl module called Utils.pm under a My directory
with puts() EXPORTed. So in a Perl script, I can:
#!/usr/bin/perl
$|++;
use My::Utils;
puts(
"This is line 1",
"This is line 2",
"This is line 3",
);
Now, in Ruby, I find if I construct something similar in a module, I
might create a utils.rb file, place it in the "my" directory under the
Ruby site_ruby directory and have:
module Utils
def Utils.mputs( *args )
args.each { |n| puts n }
end # def
end # module
Then in a Ruby script:
#!/usr/bin/env ruby
require "my/utils"
Utils.mputs(
"This is line 1",
"This is line 2",
"This is line 3"
)
But I don't like having to write "Utils.mputs". In Perl, I exported
the "puts" routine, so in Perl it's in the main namespace and I can
just say: puts( "A", "B", "C" ), etc. I want to be able to do the same
thing in Ruby. Instead of Utils.mputs( blah ), I want to just write
mputs( blah ). So in Ruby, I did this to the Utils module:
module Utils
def mputs( *args )
args.each { |n| puts n }
end # def
end # module
include Utils
Now in my Ruby script, I can do:
#!/usr/bin/env ruby
require "my/utils"
mputs(
"This is line 1",
"This is line 2",
"This is line 3"
)
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.
Thanks!
/usr/ceo