Iâ?m having a hard time learning ruby

On Aug 3, 2006, at 12:55, Paul van Delst wrote:

Nate I. 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.
:slight_smile:

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.

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.

On 8/3/06, Mat S. [email protected] wrote:
[ snip ]

Have any of you tried why’s “Try Ruby” ?

http://tryruby.hobix.com/

  • rob

On Fri, Aug 04, 2006 at 06:10:06AM +0900, Just Another Victim of the
Ambient M. wrote:

http://www.rubycentral.com/book/builtins.html

It's not exactly searchable

It is if you wget it and can use grep fairly well. If you’re on
Windows, you may need Cygwin or Windows-ports of those tools to get the
same behavior.

I don't know if this is considered rude or not but, since the Pickaxe 

book is available online at:

http://www.rubycentral.com/book/

...you could simply suck down the entire site and perform your search on 

the HTML, all for free. Again, I don’t know how the community feels about
you downloading the entire online “book,” so my apologies if this is
considered rude…

Considering the Debian APT archives provide it in a .deb package for
easy “isntallation” on the local system (package name “rubybook”), I
doubt there’s any cultural prohibition there. The online version is
licensed under the Open Publication License: so long as you don’t
violate its terms, do what thou wilt.

On Aug 3, 2006, at 13:31, Bill K. wrote:

irb --noreadline

can provide more hassle-free support for line editing / copy / paste
and
command history in irb.

Oh, man, I got so tired of having to convert tabs to spaces in my
editor before pasting them into irb!

On the other hand, with --noreadline, then I couldn’t recall previous
lines with the up-arrow. Also a horrible nuisance.

By purest serendipity, I discovered how to make irb not dump a
directory listing (???) in the middle of a paste when it hit a tab
character. It was buried near the end of the all-but-interminable
manual page for the command line interface (bash).

I now have a new secret file in my home folder, one named .inputrc that
contains this:

set disable-completion on

Now irb and get along much better. :slight_smile:

On Aug 3, 2006, at 4:44 PM, Matt T. wrote:

But, really, with all of this frustration… it’s better than the
frustrations just about anything else will give you… :slight_smile:

+1

On 8/3/06, Just Another Victim of the Ambient M.
[email protected] wrote:

“Nate I.” [email protected] wrote in message

The "methods" method works anywhere in Ruby but it is most useful in an

IRB session.
I’m unsure why you want to type in your entire program into IRB. You
probably just want to type in relevant situations to test Ruby’s behaviour,
such as operator precedence or regex behaviour.
If you want to look at all the methods of a class, you simply need to
create an object of said class and call the “methods” method, perahps
followed by the “sort” method which can help you find what you’re lookig
for…

I have often typed a program in a text editor and then just pasted it
into an IRB window to play with dynamically. On Linux this is easy, on
windows you right-click on the IRB window’s title-bar and go to
edit->paste.

If you installed on Windows and used the one-click installer, check
out Fxri - it’s a “graphical” irb and ri all in one.

If you find yourself pasting your programs into IRB a lot, you’ll also
want to look at the ruby-breakpoint library, which allows your program
to run and then “break out” into an IRB session right in the middle so
you can look at the variables and methods right there.

To try it, go to the prompt and:

gem-install ruby-breakpoint

Then write your program:

require ‘breakpoint’

…some code…
breakpoint
…some more code…

More info here: http://ruby-breakpoint.rubyforge.org/doc/

Les

Dave H. wrote:

methods for strings I might want, were all non-obvious solutions to
various problems I had when programming.
I think the 2nd Edition of Hal F.'s The Ruby Way is going to help
with this problem. It’s got more of a “here’s a problem, how do you
solve it with Ruby” orientation.

Nate I. wrote:

Someone told me about ?.methods?, but that only works if you?re in an
irb session, which I find painful to use because I have to type all my
code in, line by line. I can?t even figure out how to paste each line
of the code I already have. Inevitably I fat finger something and get
mad.

Here’s what I do frequently: enter the text in the editor and then paste
it to the IRB session (on Windows you can do that with right mouse click

  • maybe you have to change settings of the terminal to “quick edit mode”
    and “insert mode”).

Kind regards

robert

On Fri, Aug 04, 2006 at 06:32:27AM +0900, Rob S. wrote:

Have any of you tried why’s “Try Ruby” ?

Yes. For my purposes, irb is a lot more convenient – but it sure is a
clever bit of hackery, and I’m thoroughly impressed with it even if I
don’t use it much.

Wow; lots of good stuff here. Thanks again everyone–much appreciated.

-nate

On Aug 3, 2006, at 5:28 PM, Dave H. wrote:

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.

As with any language (computer or natural), you won’t become
comfortable or fluent in the language until you have learned the
vocabulary. Knowing how to conjugate a regular verb or the syntax of
a method definition is not enough.

I know this sounds decidedly low-tech but I’ve found that the best
way to become familiar with the vocabulary of a class library is to
read through the documentation several times and then several more
times.

Gary W.