OO Paradigm and Ruby Exercises for a Beginner

I am at a loss for where to begin here, so I’ll jump in.

I have worked with several languages, and Ruby is the first one I've

really felt good about. I picked up the Pickaxe book and read through
it.
While I get confused at times when “shortcuts” are used (there seem to
be a
half-dozen valid ways to do almost anything, with various amounts of
verbosity and code needed - I am used to using explicit “return”
statements,
for example) I haven’t had much trouble understanding the syntax.
However, I find that I am running into roadblocks when I attempt to
program anything remotely complex. I’ve looked at many of the exercises
in
Chris P.'s Learn to Program and I don’t find these too difficult,
especially since they seem to be focused around syntax. I suspect my
frustration is with the object oriented paradigm. I have a superficial
understanding of Classes and Objects and such, and I understand why OO
is
useful. However, I am befuddled by much of the code that I read. While
it is
possible that I am simply not proficient at algorithm design and am
unable
to follow algorithms that are designed by others, I suspect that it is
more
deeply rooted in my lack of “getting” OO.

I’ve got no idea what resources might be used to overcome this problem,
and
I am not even certain of my diagnosis. However, I would appreciate any
suggestions, resources, or help that you could offer me, because I am
excited about Ruby and eager to get past this blockage.

Thanks,
Picklegnome

P.S. I apologize for not using my real name; I understand this to be bad
etiquette. As I am a minor, my parents feel I am “at risk” and have
asked me
to keep all such information private. I hope not to offend by following
their instruction.

gnome,
Do you have any example code for a program you’re working on or a
place we can look at code that confuses you?

Don’t worry, one of the challenges of learning OO is ‘getting it.’ I
always thought it was harder for us old timers who learned non-OO
programming first. Some people say that OO is a mindset, but once you
get it, it is a pretty natural way to think. Afterall, the world is
full of ‘objects.’

http://java.sun.com/docs/books/tutorial/java/concepts/object.html has
a good tutorial on understanding objects at Sun.

http://www.aonaware.com/OOP1.htm has an interesting tutorial on object
oriented programming, that does a pretty good job of describing what
objects and classes are.

Picklegnome wrote:

[snip]

useful. However, I am befuddled by much of the code that I read. While it is
possible that I am simply not proficient at algorithm design and am unable
to follow algorithms that are designed by others, I suspect that it is more
deeply rooted in my lack of “getting” OO.

I’ve got no idea what resources might be used to overcome this problem, and
I am not even certain of my diagnosis. However, I would appreciate any
suggestions, resources, or help that you could offer me, because I am
excited about Ruby and eager to get past this blockage.

Hmm, I could refer you to chapter 1 of The Ruby Way (soon to be in
a 2nd edition). But read it in the library first, don’t spend your money
on something you might not use.

Chapter 1 of Damian Conway’s Object Oriented Perl is also good.

I also enjoyed the discussion in Bertrand Meyer’s book Object Oriented
Software Construction
. That’s where I learned OO myself in 1990.

P.S. I apologize for not using my real name; I understand this to be bad
etiquette. As I am a minor, my parents feel I am “at risk” and have asked me
to keep all such information private. I hope not to offend by following
their instruction.

What?! Picklegnome isn’t your real name??

Seriously, anyone who complains about a minor not giving his real
full name is an idiot (or worse). No one here could possibly be
offended.

Your parents are probably being over-cautious. Shows like 60 Minutes
and Dateline love to talk about Internet predators, but the frequency
is probably greatly overestimated. However IMO you’re obligated to obey
your parents. I’m old-fashioned that way.

OT: In an age where millions of college graduates can’t write a simple
English paragraph, thanks for writing an intelligent and articulate
email.

Cheers,
Hal

“Times are bad. Children no longer obey their parents, and everyone is
writing a book.” – Marcus Tullius Cicero

One of the Ruby Q.zes that prompted my post was the “Space Merchant”
quiz
at http://rubyquiz.com/quiz71.html . I understood very little of the
code in
the setup.
On the other hand, Ruby Q. #12, “Scrabble Stems”, at
http://rubyquiz.com/quiz12.html was one that I understood most of. Line
14
(starting with “uniques.each do…” was the first sequence of code that
confused me in any way. I understand all of the basic manipulations
(with
only one or two consecutive operations) but when it gets to something
like
Line 14 or 21 (result = STEMS.delete…) I get lost.

Thanks,
Picklegnome

On 7/26/06 1:49 PM, in article
[email protected], “Kevin
Clark”

On Thu, Jul 27, 2006 at 05:28:01AM +0900, Hal F. wrote:

Hmm, I could refer you to chapter 1 of The Ruby Way (soon to be in
a 2nd edition). But read it in the library first, don’t spend your money
on something you might not use.

I’ve seen references to this book a number of times, and heard good
things about it. What, exactly, is its approach and “theme”? For
instance, the Pickaxe is in general a progression from simple
programming concepts with Ruby to more complex constructions, Why’s
(Poignant) Guide is a very entertaining approach to very beginner-level
programming, some books teach beginning programming and “just happen” to
use a given language as its example language, Damian Conway’s Object
Oriented Perl is specifically about OOP with Perl, there are books
specifically teaching use of Perl for CGI programming, Agile Web
Development with Rails is, of course, about use of Rails (with Ruby
instruction as a side-effect of that), and so on. The Ruby Way is a
great-sounding title, but it doesn’t tell me as explicitly what it’s
teaching as (for instance) Learning Perl Objects, References, and
Modules described what the book contained (before they changed the name
to Intermediate Perl).

full name is an idiot (or worse). No one here could possibly be
offended.

I fully understand a desire for privacy, whether parentally mandated or
arising from personal preference. Just don’t engage in sock-puppet
activities (assuming a second identity for deceptive purposes) and the
like, and I’m sure you’ll get by just fine. In other words, Hal’s
right. Don’t sweat it.

OT: In an age where millions of college graduates can’t write a simple
English paragraph, thanks for writing an intelligent and articulate
email.

Ditto! In addition, thanks for asking a question that should probably
be asked more often. I’ve seen two or three nontrivial efforts at a
Ruby instructional text of some sort that start out with statements like
“I was planning to write a book about object-oriented Ruby that started
with objects and just explained everything form the perspective of using
OOP in a way that would be comprehensible and quickly learned by
programming newbies, but it was too hard, so I started with variables in
the normal imperative/procedural style instead.” That drives me up the
wall. I’m no Ruby expert, but I have some familiarity, and what I know
so far has quickly indicated to me that probably the best way to get
someone up to speed quickly on OOP with Ruby from a standing start would
be to teach OOP in a largely transparent manner, so that you’re doing
OOP without realizing it for a while. OOP has become such a ubiquitous
and important part of the programming landscape that it makes sense to
teach OOP first, when the alternative is strictly procedural code, for
most purposes. Why hasn’t this been done (as far as I’m aware) with
Ruby as the example language?

Yes, I’d do it myself, if I knew more Ruby than I do thus far. Some
day, I suppose, I will – if it hasn’t been done by the time I’m ready.

On Thu, Jul 27, 2006 at 08:48:19AM +0900, Hal F. wrote:

Chad P. wrote:

I’ve seen references to this book a number of times, and heard good
things about it. What, exactly, is its approach and “theme”?

Well, the first chapter is different: It’s an overview of OOP in general
and Ruby in particular.

Excellent!

[ snip a bunch of exposition ]

Does that help?

Wonderful! It’s going on my list of must-haves, forthwith – though I
may wait for 2nd Ed to come out before buying it. Is there a
free/online version available to get me started?

[ sip a bunch of dithering by yours truly ]

I’ve thought about this, but I think it can only be taken so far.

In explaining OOP, you sort of have to understand what P is before you
grasp what OO is. Maybe we should use the French abbreviation, POO. :slight_smile:

I’m not sure I agree with that. In fact, I’m pretty sure I don’t. If
programming is taught in a strictly object-oriented manner from line
one, the student can progress as a programmer via the OOP instruction,
rather than needing to be a competent programmer first. I think that,
at most, perhaps a handful of pages (four to six, maybe) need to be
spent on “What is a variable?” before getting into object oriented
programming in earnest, and even at that point OOP techniques can be
used where appropriate.

I’m just tired of seeing people say they were going to do an
OOP-complete beginning programmer’s tutorial, but gave up, and did a
procedural tutorial with cursory mentions of OOP concepts just for
color. Dammit.

Chad P. wrote:

I’ve seen references to this book a number of times, and heard good
things about it. What, exactly, is its approach and “theme”?

Well, the first chapter is different: It’s an overview of OOP in general
and Ruby in particular.

The rest of the chapters, for the most part, attempt to answer “how-to”
questions. These range from the trivial (“How do I find the legnth of
a string?”) to the monumental (“How do I write a web app?”).

It also serves as a kind of “inverted reference” by being
topic-oriented. Things are arranged by topic, not alphabetically.
Thus you’ll see the String methods upcase, downcase, casecmp, and
capitalize all discussed together (with maybe a little regex stuff
thrown in).

Some of the design principles were:

  • nothing is too small to be included (people’s
    opinions differ about what is “obvious” or
    “trivial”)
  • cover almost all the core and standard libs
  • things that are “missing” from the core and
    standard libs are often coded
  • it strives for good Ruby style, sometimes
    with flaws
  • examples are given whenever possible/practical
  • it has a CS flavor rather than a corporate one
  • it emphasizes principles rather than drop-in
    usable code (though that is often there too)
  • it gives pointers to outside info when possible
  • it gives little tidbits outside its main topic,
    for flavor

The first edition had nine chapters and 350+ code
fragments. Many of the chapters in the 2nd ed are
split up and rearranged, and the appendices are
gone. The 2nd ed will have 22 chapters – how
many code fragments, I’m not sure. The new one is
significantly larger, more up to date, and generally
more accurate.

It’s supposed to be available in early November.

Does that help?

someone up to speed quickly on OOP with Ruby from a standing start would
be to teach OOP in a largely transparent manner, so that you’re doing
OOP without realizing it for a while. OOP has become such a ubiquitous
and important part of the programming landscape that it makes sense to
teach OOP first, when the alternative is strictly procedural code, for
most purposes. Why hasn’t this been done (as far as I’m aware) with
Ruby as the example language?

Yes, I’d do it myself, if I knew more Ruby than I do thus far. Some
day, I suppose, I will – if it hasn’t been done by the time I’m ready.

I’ve thought about this, but I think it can only be taken so far.

In explaining OOP, you sort of have to understand what P is before you
grasp what OO is. Maybe we should use the French abbreviation, POO. :slight_smile:

Hal

Hal F. wrote:

deeply rooted in my lack of “getting” OO.
on something you might not use.

Chapter 1 of Damian Conway’s Object Oriented Perl is also good.

I also enjoyed the discussion in Bertrand Meyer’s book Object Oriented
Software Construction
. That’s where I learned OO myself in 1990.

I liked Rebecca Wirfs-Brock’s Designing Object-Oriented Software. It
does a good job of explaining how to think about objects in terms of
roles, responsibility, and collaboration.

http://www.wirfs-brock.com/DesignBooks.html

It tends to be kinda pricey, so add it to your library list. (But buy
Hal’s book when the new edition comes out.)


James B.

“In Ruby, no one cares who your parents were, all they care
about is if you know what you are talking about.”

  • Logan C.

Picklegnome,

I am happy to hear of your enthusiasm for Ruby and programming.

Others have mentioned good books, so I will spare from adding to the
list.
The bit of advice I can given is simple: just program a lot. You can
read web pages and books all day long, but some things you just won’t
get until you come across them on your own.

When I first started to get serious about programming I would read a
lot of code. A lot the time I would get confused and wonder why
someone did this or that. To the point were it was distracting me from
really learning. I finally just started to code a lot and after a
while I would come up with similiar solutions. Then I would think
back, “Huh, that’s why they did that.”

Don’t get me wrong studying and reading are definitely great for
learning new ideas and the way others think about things. My point is
that you have to be trying to program and solve problems on your own
for it to be most effective.

Best of luck!

–Meador

Hal F. wrote:

The first edition had nine chapters and 350+ code
fragments. Many of the chapters in the 2nd ed are
split up and rearranged, and the appendices are
gone. The 2nd ed will have 22 chapters – how
many code fragments, I’m not sure. The new one is
significantly larger, more up to date, and generally
more accurate.

It’s supposed to be available in early November.

Rough cut today (i think)

http://safari.oreilly.com/0768667208

On 2006-07-26, Hal F. [email protected] wrote:

I am not even certain of my diagnosis. However, I would appreciate any
Software Construction_. That’s where I learned OO myself in 1990.
I was going to recommend Meyer’s book, OOSC, but Hal beat me to it.
It contains a very thorough and well-thought-out explanation of the
concepts of OO programming and design. Its emphasis is on static typing
(using the language Eiffel as the vehicle - Java, C++, and C# are other
examples of languages that use static typing), which is different from
the dynamic typing of Ruby, Python, Perl, Smalltalk, etc.; but to learn
OO thoroughly, you’ll need to learn about both; and a lot of the
concepts
in OOSC can be applied to dynamically-typed languages. OOSC is rather
large, but if you have the patience (and if you’re serious about
learning,
you will have the patience), reading the sections of it that cover
OOD/OOP
explicitly should be very beneficial. (You might find the first edition
more concise, if you can find it.)

I have Conway’s book, but I believe I skipped the first chapter when I
read
the book a year or two ago to save time; but the rest of the book is
good -
shows a very good grasp of OO -, so the OO intro. chapter is probably a
good way to learn some basics of OOP with a dynamically-typed language.
(I
see that OOSC is one of the books he recommends at the end of the
chapter,
p. 19.) (I’m not familiar with “The Ruby Way”, so can’t comment on it.)

One aspect of OO that is important (and seems to be sometimes neglected
by experienced developers) is that of creating clean, well-defined
(and documented) interfaces - where interface in this context means
the semantics and services that a well-designed class provides
to users/clients of the class. These services are not tied to the
implementation of the class (or of the class’s descendants/subclasses).
Not needing to care about how a class one is using is implemented allows
one to write code that is cleaner and less expensive to maintain than
otherwise.

Gene T. wrote:

Rough cut today (i think)

http://safari.oreilly.com/0768667208

So it would seem. Take what you see there
as only 90% accurate, however.

Hal

Meador I. wrote:

Picklegnome,

I am happy to hear of your enthusiasm for Ruby and programming.

Others have mentioned good books, so I will spare from adding to the list.
The bit of advice I can given is simple: just program a lot. You can
read web pages and books all day long, but some things you just won’t
get until you come across them on your own.

Quite true. Here’s a related suggestion: Modify working code to make
it do different things, or do the same things in different ways. Take a
single-file and turn it into a set of small libraries.

It can be very frustration to set out to code something that employs a
specific technique or demonstrates some concept, and never get the thing
running (or running quite right). However, if start from a known good
point and try changing things, you can roll back to a known good state
and start looking at where things went wrong.

Try taking some of the example from Ruby Q. and see if you can add
little things, small features. See if the code still works. If not,
find out what you changed. Take it, break it, fix it, mix it.

Happy hacking.


James B.

http://www.ruby-doc.org - Ruby Help & Documentation
http://www.artima.com/rubycs/ - The Journal By & For Rubyists
http://www.rubystuff.com - The Ruby Store for Ruby Stuff
http://www.30secondrule.com - Building Better Tools

Thanks to everyone who has responded; I’m very happy to know that so
many
people are as excited about me learning as I am!

I’ll be sure to check out The Ruby Way; from what I’ve read it covers
many
concepts that I may be able to use. (Hal - you aren’t going to sell any
books if you tell people not to buy it!)

I’ll be sure to check out the other books that were suggested, too.

Thanks also to Meador, vasudevram, and James B., all of whom
suggested
that I play around with code (my own or someone else’s).

I’ll go get to work on my racecars…

Thanks again,
Picklegnome

On 7/27/06 10:35 AM, in article
[email protected], “vasudevram”

Meador I. wrote:

learning new ideas and the way others think about things. My point is
that you have to be trying to program and solve problems on your own
for it to be most effective.

Best of luck!

–Meador

Good suggestions by Meador. I’d like to add these:

Since you’re new to OOP, you might want to try writing small (OOP)
programs that model real-world objects and their behaviour and
interactions. E.g. Racing cars on a racing track. Classes like Car,
with instances like Car1, Car2 (or SchumiCar (Michael Schumacher), etc.
:), methods like Car#start, Car#stop, Car#changeGear, and so on.
After writing some such basic classes and methods, write programs that
use them, say to do simulations of a race-car session. No need to do a
GUI initially - will complicate things, though could do later. Could
just use print statements to the console. Simulations and game
programming are a good way to learn/improve your programming skills,
and hey - also fun :slight_smile: Remember Matz’s comments about why he created
Ruby … “I believe the purpose of life, is at least in part, to be
happy” …
I’m pretty sure that point is one that has drawn many people towards
learning and using Ruby.

Once you’ve learnt some programming by doing this and some of the other
suggestions given, then you can progress to modeling
(non-physical-world) software concepts as classes and objects (e.g.
Queue, DatabaseConnection, Thread, etc. - just some examples, may not
be the best ones to illustrate what I mean).

Also, as part of Meador’s suggestion of programming a lot, another good
thing to do is to download, read, understand and then *modify" programs
written by others. Nothing shows whether you’ve understood some(one
elses) code as much as modifying it and then seeing whether it works
as per your intent or not.

Cheers
Vasudev

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs