I've started learning Ruby. This being my first post here I guess I should tell you all that I'm basically a C# developer, with a bit of Java. I had the good fortune to learn some Smalltalk from a co-worker and half-built an application in it (the project was canceled). I haven't done much web development, but I'm planning on letting the Ruby learning take me there eventually. Last week I started reading Ruby tutorials, blogs, and documentation, playing with
irb whenever I wanted to.
Today I started building my first program, and, coming from a Visual Studio background, was finding it really annoying to have to require each and every new class I made (I'm following the .NET one class per file convention - don't know if that's Ruby standard or not). So I Alt-Tabbed to Google (who really closes it?) and started learning about require. Apparently, it's just a statement like any other that can be executed from anywhere. Then I used the "ri" command to learn about the Dir.entries method, and well... came up with this file I'm finding quite convenient:
Behold 'requireDir.rb'!
#dropping this file in any directory and then requiring it
#(ie. typing "require 'DIR\requireDir')
#will require all files in the DIR directory
dir = File.expand_path(File.dirname(__FILE__))
Dir.entries(dir).select { |f| f =~ /.*\.rb/ }.each { |f| require File.join(dir, f) }
The code comments explain what it does. I don't know if there's a better way to do this, or reasons I shouldn't do this. Like I said, I'm brand new to Ruby, but I thought it was cool how easy it was to make something like this. I figure it shouldn't be hard to make recursive either, if I want to later on.
Oh - and if you're wondering how I posted the code like that in that nice little scrollable window... it took me 20 minutes Googling. If you don't have that much time, just click
here. :)