From: Dave Howell Date: 2006-08-04T06:28:13+09:00 Subject: Re: I’m having a hard time learning ruby. On Aug 3, 2006, at 12:55, Paul van Delst wrote: > Nate Imaqaguy wrote: >> For starters, just do you know where I�m coming from: I have little >> to no programming experience, except for some C and VB classes I took >> 6 years ago, which I did fine in. >> My biggest hurdle getting started with ruby is finding which >> methods/classes/files to call upon for whatever the heck I�m trying >> to accomplish. Well, I had years of experience in lots of languages when I started into Ruby, but my experience definitely did NOT include Java or C, which is taken for granted in a lot of documentation. "This works a lot like a C zingwanger, except you can modify it while it's running," and the like. Sigh. Playing the guessing game of "if I were a command to do X, what would I call myself?" is something I still get to do a lot of. "Collect" and "index," (I'd never have guessed 'index' was a search function!) for arrays, and figuring out which of "chomp/chop/slice/split/strip" methods for strings I might want, were all non-obvious solutions to various problems I had when programming. >> I�m use to being able to perform a search, like in MSDN Library, >> using key words of something I�m trying to perform. There are two factors that get in the way of being able to ask "How do I do ____?" One, Ruby's documentation, like Ruby, is still really young compared to the languages you're used to (even if you subtract six years!). Two, the basic commands in Ruby (I think) are often dramatically more powerful than the core commands of other languages. For example, every 4-6 weeks, somebody pops up asking where on earth they can find a shuffle command. The answer is the deliciously clever, but obvious only in retrospect myarray.sort_by{rand}. Ruby has armloads of cases where the way you do _____ is by combining some of the basic, powerful operators; which is why they tend not to cause people to easily say "here's what this command is for;" it's good for too many things to easily list. I think the new "Ruby Cookbook" is intended to help with that problem. :) The other thing I'd suggest is carefully studying some of the really key methods. There's usually seven ways to do anything in Ruby. Here are the methods that I personally have found myself using over and over 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 join # especially the .join('') form... sort Some of the other commands are just synonyms for these (use whichever you prefer), and others I just haven't used as much. Also, some of those commands are actually part of Enumerable. Parts of Enumerable 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 + =~ # I find myself using regular expressions a lot; I wish they 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, 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 fiddling with files, I'd suggest skipping the annoying File, Dir, and IO stuff and going straight to Pathname. Thankfully, the number-related 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 > line recall+traverse (up/down,left/right arrows) yet in irb. Drives me > nuts. But you can play around within a regular source file rather than > use irb directly. Sometimes I just play around at the end of a source > file, e.g. That requires the "readline" library for some reason. (The regular command line does this, but irb apparently can't tap that resource or something. ) It took me three tries and a couple hours to get readline located, downloaded, installed, and enabled. (First I had to download and install some new installer program. Fink, DarwinPorts, I don't remember. Blah.) But I seem to have abysmal luck with anybody's 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. > => true > > to see if the stuff after the "actual" code (e.g. classes, modules, > whatnot) does what I think it does. If not, I modify, save, and > re-run. Hmm. I usually use 'load' instead of 'require' when I'm doing that sort of experiment. "Require" will try to not load the same file more than once, and I tend to leave irb up and running, so 'load' will replace what's in irb's memory with the new whatever that's in my file. But I've also definitely replaced bits of things. "Hmm, this one function 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 data I'd already gotten loaded in, still intact. It's kind of freaky.