I’m having a hard time learning ruby.

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.

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=
 
R

Rob Sanheim

On 8/3/06 said:
I share your frustration. I have yet to find any really nice,
interactive ruby documentation. I think part of the problem is that
the core of the language isn't even fully documented. But ruby-
doc.org is doing a lot of work to fix this problem.

Contrary to a lot of people on this list, I don't recommend Pickaxe
for introduction. It is awesome reference, but I found it really dry
as an introductory text. If you have a good sense of humor and some
patience, I'd recommend The Poignant Guide http://poignantguide.net/
ruby/.

Then to get some early practice with the ideas, I'd recommend
rubyquiz.com. There's a lot of nice little exercises there with lots
of source code and analysis for each problem. (Note: download all
the solutions, as some of the older individual solutions reference
mailing list archives that are no longer available).

Finally, when you're stuck, ask a question here. It's one of the
nicest programming lists I've ever been on.
-Mat

Have any of you tried why's "Try Ruby" ?

http://tryruby.hobix.com/

- rob
 
C

Chad Perrin

However, if this is your only problem then you're probably a more
competent programmer than you let on. If that's the case, Paul's suggestion
of this reference will be valuable to you:

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.
 
D

Dave Howell

On Windows, I've found running irb as:

irb --noreadline

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

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

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

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

I now have a new secret file in my home folder, one named .inputrc that=20=

contains this:

set disable-completion on

Now irb and get along much better. :)=
 
L

Leslie Viljoen

"Nate Imaqaguy" <[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
 
C

Chad Perrin

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.
 
T

Timothy Hunter

Dave said:
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 think the 2nd Edition of Hal Fulton'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.
 
R

Robert Klemme

Nate said:
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
 
G

gwtmp01

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 Wright
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,247
Latest member
crypto tax software1

Latest Threads

Top