D
Dave Howell
Well, I had years of experience in lots of languages when I started=20
into Ruby, but my experience definitely did NOT include Java or C,=20
which is taken for granted in a lot of documentation. "This works a lot=20=
like a C zingwanger, except you can modify it while it's running," and=20=
the like. Sigh.
Playing the guessing game of "if I were a command to do X, what would I=20=
call myself?" is something I still get to do a lot of. "Collect" and=20
"index," (I'd never have guessed 'index' was a search function!) for=20
arrays, and figuring out which of "chomp/chop/slice/split/strip"=20
methods for strings I might want, were all non-obvious solutions to=20
various problems I had when programming.
There are two factors that get in the way of being able to ask "How do=20=
I do ____?"
One, Ruby's documentation, like Ruby, is still really young compared to=20=
the languages you're used to (even if you subtract six years!).
Two, the basic commands in Ruby (I think) are often dramatically more=20
powerful than the core commands of other languages. For example, every=20=
4-6 weeks, somebody pops up asking where on earth they can find a=20
shuffle command. The answer is the deliciously clever, but obvious only=20=
in retrospect myarray.sort_by{rand}. Ruby has armloads of cases where=20
the way you do _____ is by combining some of the basic, powerful=20
operators; which is why they tend not to cause people to easily say=20
"here's what this command is for;" it's good for too many things to=20
easily list.
I think the new "Ruby Cookbook" is intended to help with that problem.=20=
The other thing I'd suggest is carefully studying some of the really=20
key methods. There's usually seven ways to do anything in Ruby. Here=20
are the methods that I personally have found myself using over and over=20=
again:
Arrays
+
<<
[ ] # you'd think you'd know this, but there's ranges: =
myArray[4..7]
negative numbers: myArray[1..-1]
and what happens when you type =
anArray[2].
assoc # an array-of-arrays search system
collect
collect!
each
flatten
index =09
join # especially the .join('') form...
sort
Some of the other commands are just synonyms for these (use whichever=20
you prefer), and others I just haven't used as much. Also, some of=20
those commands are actually part of Enumerable. Parts of Enumerable=20
worth getting to know include
Enumerable
collect #this one's crazy powerful voodoo
each_with_index
find (aka detect)
find_all
grep
inject #this one's more crazy powerful voodoo
sort_by
String
+
=3D~ # I find myself using regular expressions a lot; =
I wish they=20
weren't so confusing.
chomp
each # especially each('')
gsub # and sub, sub!, and gsub!
match
scan # and trying to figure out when I want .match and when =
scan,=20
often with {}
split
succ # this one's really strange
That's it. That's the stuff I seem to keep using over and over. For=20
fiddling with files, I'd suggest skipping the annoying File, Dir, and=20
IO stuff and going straight to Pathname. Thankfully, the number-related=20=
methods are mostly pretty normal, and usually named as you'd expect.
That requires the "readline" library for some reason. (The regular=20
command line does this, but irb apparently can't tap that resource or=20
something. <shrug>) It took me three tries and a couple hours to get=20
readline located, downloaded, installed, and enabled. (First I had to=20
download and install some new installer program. Fink, DarwinPorts, I=20
don't remember. Blah.) But I seem to have abysmal luck with anybody's=20
installer besides the native OSX one.
Hmm. I usually use 'load' instead of 'require' when I'm doing that sort=20=
of experiment. "Require" will try to not load the same file more than=20
once, and I tend to leave irb up and running, so 'load' will replace=20
what's in irb's memory with the new whatever that's in my file. But=20
I've also definitely replaced bits of things. "Hmm, this one function=20
doesn't work." {rewrite rewrite}
irb> class SomeClass
irb>
copy and paste one function out of the middle of the =
whole class
irb> end
Presto. One method from the middle of the class changed, with whatever=20=
data I'd already gotten loaded in, still intact. It's kind of freaky.=20=
into Ruby, but my experience definitely did NOT include Java or C,=20
which is taken for granted in a lot of documentation. "This works a lot=20=
like a C zingwanger, except you can modify it while it's running," and=20=
the like. Sigh.
Playing the guessing game of "if I were a command to do X, what would I=20=
call myself?" is something I still get to do a lot of. "Collect" and=20
"index," (I'd never have guessed 'index' was a search function!) for=20
arrays, and figuring out which of "chomp/chop/slice/split/strip"=20
methods for strings I might want, were all non-obvious solutions to=20
various problems I had when programming.
There are two factors that get in the way of being able to ask "How do=20=
I do ____?"
One, Ruby's documentation, like Ruby, is still really young compared to=20=
the languages you're used to (even if you subtract six years!).
Two, the basic commands in Ruby (I think) are often dramatically more=20
powerful than the core commands of other languages. For example, every=20=
4-6 weeks, somebody pops up asking where on earth they can find a=20
shuffle command. The answer is the deliciously clever, but obvious only=20=
in retrospect myarray.sort_by{rand}. Ruby has armloads of cases where=20
the way you do _____ is by combining some of the basic, powerful=20
operators; which is why they tend not to cause people to easily say=20
"here's what this command is for;" it's good for too many things to=20
easily list.
I think the new "Ruby Cookbook" is intended to help with that problem.=20=
The other thing I'd suggest is carefully studying some of the really=20
key methods. There's usually seven ways to do anything in Ruby. Here=20
are the methods that I personally have found myself using over and over=20=
again:
Arrays
+
<<
[ ] # you'd think you'd know this, but there's ranges: =
myArray[4..7]
negative numbers: myArray[1..-1]
and what happens when you type =
anArray[2].
assoc # an array-of-arrays search system
collect
collect!
each
flatten
index =09
join # especially the .join('') form...
sort
Some of the other commands are just synonyms for these (use whichever=20
you prefer), and others I just haven't used as much. Also, some of=20
those commands are actually part of Enumerable. Parts of Enumerable=20
worth getting to know include
Enumerable
collect #this one's crazy powerful voodoo
each_with_index
find (aka detect)
find_all
grep
inject #this one's more crazy powerful voodoo
sort_by
String
+
=3D~ # I find myself using regular expressions a lot; =
I wish they=20
weren't so confusing.
chomp
each # especially each('')
gsub # and sub, sub!, and gsub!
match
scan # and trying to figure out when I want .match and when =
scan,=20
often with {}
split
succ # this one's really strange
That's it. That's the stuff I seem to keep using over and over. For=20
fiddling with files, I'd suggest skipping the annoying File, Dir, and=20
IO stuff and going straight to Pathname. Thankfully, the number-related=20=
methods are mostly pretty normal, and usually named as you'd expect.
I hear ya. On my Mac laptop, I haven't figured out how to get command=20=
line recall+traverse (up/down,left/right arrows) yet in irb. Drives me=20=
nuts. But you can play around within a regular source file rather than=20=
use irb directly. Sometimes I just play around at the end of a source=20=
file, e.g.
That requires the "readline" library for some reason. (The regular=20
command line does this, but irb apparently can't tap that resource or=20
something. <shrug>) It took me three tries and a couple hours to get=20
readline located, downloaded, installed, and enabled. (First I had to=20
download and install some new installer program. Fink, DarwinPorts, I=20
don't remember. Blah.) But I seem to have abysmal luck with anybody's=20
installer besides the native OSX one.
And then simply do
$ ruby -w fruit.rb
on the command line or, in irb, use "require"
irb(main):001:0> require 'fruit'
Picking some fruit....
Eating some fruit....
I have 9 piece(s) of fruit left.
=3D> true
to see if the stuff after the "actual" code (e.g. classes, modules,=20
whatnot) does what I think it does. If not, I modify, save, and=20
re-run.
Hmm. I usually use 'load' instead of 'require' when I'm doing that sort=20=
of experiment. "Require" will try to not load the same file more than=20
once, and I tend to leave irb up and running, so 'load' will replace=20
what's in irb's memory with the new whatever that's in my file. But=20
I've also definitely replaced bits of things. "Hmm, this one function=20
doesn't work." {rewrite rewrite}
irb> class SomeClass
irb>
copy and paste one function out of the middle of the =
whole class
irb> end
Presto. One method from the middle of the class changed, with whatever=20=
data I'd already gotten loaded in, still intact. It's kind of freaky.=20=