NubyGems: Simple Array Definition Shortcut

by Gregory Brown

So, I've been training a new employee who has some academic programming experience but no Ruby experience. I decided to revitalize my NubyGems series because now instead of trying to remember what I tripped over almost two years ago, I have active conversations with someone new to the language to work from.

I am going to try to post them unabridged, but try to keep them short and really simple. Whenever my friend asks an interesting question, you'll see my explanations along with some IRB examples.

People with less than 3 months of Ruby experience, please give me feedback on these as to whether they are helpful. I promise to clarify things in articles or expound a bit more if things are hard to understand.

But for now, the NubyGems experiment continues!


(01:58:52) Dinko: I keep seeing the %w what's that do
(01:59:18) Me: have you installed irb yet?
(01:59:31) Me: >> %w[a b c]
=> ["a", "b", "c"]
(01:59:36) Me: it's just a shortcut
(02:00:33) Me: says, what follows is an array of strings seperated by spaces
(02:01:17) Me: for clarity, note that any symbolic delimiter can be used
(02:01:20) Me: >> %w(a b c)
=> ["a", "b", "c"]
>> %w$a b c$
=> ["a", "b", "c"]
>> %w~a b c~
=> ["a", "b", "c"]
(02:01:35) Me: [] is most common because it's semantically meaningful
(02:01:39) Me: reminds you of arrays
(02:01:50) Me: but () is common because some other languages do it that way
(02:02:28) Me: note also the flexibility that affords
(02:02:30) Me: >> %w~a [ c~
=> ["a", "[", "c"]


Hopefully this little discussion helps shed light on what %w is useful for

Now, you see these all over the place, but one thing I find myself using them over and over for is dynamic requires. I get sick of typing stuff like:


require "foo"
require "bar"
require "baz"


So a lot of times, you'll see me write stuff like:

%w[ foo bar baz ].each { |lib| require lib }


Now that might make me just a tad bit lazy, but I think that looks pretty! :)


10 Comments

BDQuinn
2006-06-07 00:46:07
Great little nugget of information, but could benefit with a tiny bit more detail, maybe one example of a simple practical use.
Gregory Brown
2006-06-07 02:05:21
BDQuinn: Added an example. Hope this helps.


Thanks for the feedback


2006-06-07 06:20:16
Such tidbits are good for those with intermediate experience too.
Dan Rolander
2006-06-07 06:30:43
Yes, these tips are very helpful. Keep them coming!
BDQuinn
2006-06-07 07:16:37
Yeah, that example makes the tip more pratical.
Justin
2006-06-07 12:18:41
Nice tip, but I don't think I've ever seen %w[ ... ] until I saw it here. I've been coding in Ruby, for both Rails and non-Rails apps, and I've only seen %w( ... ) used.


Just my $0.02! A nice tip, otherwise.

Gregory Brown
2006-06-07 12:57:06
Justin:


I think I originally picked this up from James Edward Gray II.
I like it because as I mentioned, it's pretty symbolic of arrays.


I also use it to make sure I don't typo.


Just by habit I do string and array shortcuts using different delimeters



>> %( this is a string )
=> " this is a string "
>> %w[ this is an array ]
=> ["this", "is", "an", "array"]


mentally, I think of %w[...] as array notation and %(...) as string notation.


Of course, it's just a preference. :)

yokimbo
2006-06-19 21:25:31
Nice start, Gregory. The final example is especially useful as it seems more Rubyish. Keep them coming.
online casino
2006-10-15 00:28:25









JNettik
2008-05-10 09:05:00
Great explanation! I'm going through the book Beginning Ruby, and Peter has been doing a great job of explaining new concepts, until he dropped %w{a e i o u} on us, and all he said was it was a literal array, but no hint as to why he used %w, what it was or what it does. Disappointing since he went over very basic programming concepts ad nauseam.