[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

ruby course confusion

Alex Combas

2/12/2006 5:23:00 AM

Working my way through the "ruby course"
http://ruby-doc.org/docs/Immersive%20Ruby%20programming%20course/RubyCourse...

I was wondering if anyone else finds this tutorial a little confusing.
Its really good in parts, but other parts are really unclear but maybe
its just me.

For example:
~~~
find_it
Write a function find_it that takes an array of strings and a block.
The block should take two parameters and return a boolean value.
The function should allow to implement longest_string, shortest_string,
and other functions by changing the block.
~~~

Sorry, this just seems really muddled.
Is find_it a class, method, or what?
What is the point of the boolean that the block needs to return?

I asume that find_it is a class, and longest_string and shortest_string
are object methods, and the array and the block are passed when
the class is initialized, but I feel like I'm missing something.

--
Alex Combas
http://noodlejunkie.blo...


2 Answers

Matthew Smillie

2/12/2006 6:02:00 AM

0

On Feb 12, 2006, at 5:23, Alex Combas wrote:

> For example:
> ~~~
> find_it
> Write a function find_it that takes an array of strings and a block.
> The block should take two parameters and return a boolean value.
> The function should allow to implement longest_string,
> shortest_string,
> and other functions by changing the block.
> ~~~
>
> Sorry, this just seems really muddled.
> Is find_it a class, method, or what?
> What is the point of the boolean that the block needs to return?
>
> I asume that find_it is a class, and longest_string and
> shortest_string
> are object methods, and the array and the block are passed when
> the class is initialized, but I feel like I'm missing something.

A function in this case would be a method. The two terms are often
used interchangeably, despite theoretical quibbles that they're not
quite the same thing; a lambda expression is also a function, for
instance.

The point of the block is that you're attempting to write a method
that can be used as a general implementation of several other
methods, depending on the contents of the block. The boolean return
value of the block is actually a very small hint, following from the
fact that finding the shortest/longest element of an array is very
closely related to sorting the array.

Anyway, here's the initial problem restated:

> Write a method find_it that takes as parameters:
> - an array of strings
> - a block
> The block should take two parameters and return a boolean value.
>
> You should be able to implement longest_string and shortest_string
> simply by changing the block provided to find_it. In other words,
> the implementation of longest_string and shortest_string will
> consist only of a call to find_it with a particular block:
>
> def longest_string (arr)
> find_it(arr) { ... }
> end
>
> def shortest_string (arr)
> find_it(arr) { ... }
> end


As for the document being muddled, I read a fair bit of it, and it
strikes me that the amount of apparent muddle would depend on your
previous exposure to programming in general.

It's a bit of a catch with teaching/learning programming; it uses
very jargony language without the same formalism you get in maths. A
method in one language is a function in another is a procedure in a
third and doesn't even exist as a concept in a fourth language. This
vocabulary issue really does put a hitch in learning programming,
since to know what a term really means, you need to know the theory
behind it, for which you need to know what the terms mean.... and I
think you see the problem. The best way to deal with this obstacle
it is to jump right in, and ask questions when you're stumped. Which
you seem to be doing, so good on you.


Hope that helps.

matthew smillie.



----
Matthew Smillie <M.B.Smillie@sms.ed.ac.uk>
Institute for Communicating and Collaborative Systems
University of Edinburgh




Alex Combas

2/12/2006 8:16:00 PM

0

On 2/11/06, Matthew Smillie <M.B.Smillie@sms.ed.ac.uk> wrote:

Thanks Matthew, I appreciate you taking the time
to reword this for me, but there is one part that is
still bothering me.

> > The block should take two parameters and return a boolean value.

Its the thing with the boolean that I dont get. This seems to be asuming
some prior knowledge of a particular ruby method.
I've searched Array and String and dont see anything that fits.

What is this boolean supposed to show? Booleans can me on/off
true/false, found/not_found, or many other things.

If the guide just stated what the boolean is for, or even better what
method they want used that returns a boolean I'm sure this would
be a rather simple problem.

Finding the longest or shortest string in an array is so trivial.
This really seems more like an exercise on howto structure blocks
and pass variables to methods.

result = arr.sort_by {|w| w.length};
shortest,longest = result[0],result[-1]
--
Alex Combas
http://noodlejunkie.blo...