# Ruby course confusion

**URL:** <https://www.ruby-forum.com/t/ruby-course-confusion/66035>\
**Category:** Ruby\
**Created:** [February 12, 2006, 6:23am UTC](https://www.ruby-forum.com/t/ruby-course-confusion/66035 "2006-02-12T06:23:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![combas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/aca169/32.png) [@combas](https://www.ruby-forum.com/u/combas)\
**Post date:** [February 12, 2006, 6:23am UTC](https://www.ruby-forum.com/t/ruby-course-confusion/66035/1 "2006-02-12T06:23:32Z")

</div>

Working my way through the “ruby course”

> **[RubyCourse\_1.0-1.pdf](https://ruby-doc.org/docs/Immersive%20Ruby%20programming%20course/RubyCourse_1.0-1.pdf)**
>
> 2.19 MB

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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![combas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/aca169/32.png) [@combas](https://www.ruby-forum.com/u/combas)\
**Post date:** [February 12, 2006, 7:01am UTC](https://www.ruby-forum.com/t/ruby-course-confusion/66035/2 "2006-02-12T07:01:52Z")

</div>

On Feb 12, 2006, at 5:23, Alex C. wrote:

> 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:

> 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 S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Institute for Communicating and Collaborative Systems  
University of Edinburgh

---

<div class="post-metadata">

**Author:** ![combas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/aca169/32.png) [@combas](https://www.ruby-forum.com/u/combas)\
**Post date:** [February 12, 2006, 9:16pm UTC](https://www.ruby-forum.com/t/ruby-course-confusion/66035/3 "2006-02-12T21:16:54Z")

</div>

On 2/11/06, Matthew S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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]

---

<div class="post-metadata">

**Author:** ![combas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/aca169/32.png) [@combas](https://www.ruby-forum.com/u/combas)\
**Post date:** [February 12, 2006, 9:32pm UTC](https://www.ruby-forum.com/t/ruby-course-confusion/66035/4 "2006-02-12T21:32:12Z")

</div>

DÅ?a NedeÄ¾a 12 FebruÃ¡r 2006 21:16 Alex C. napÃ­sal:

> I’ve searched Array and String and dont see anything that fits.  
> and pass variables to methods.  
> D’oh.

What I think are strong hints follows. Don’t read this mail if you don’t  
want  
any.

I think the block should be a predicate that defines if something was  
“found”

- that’s what the boolean it returns should tell. Or rather evaluate if  
an  
element is more “it” than the previous one found.

As for the two parameters, I think one should be an element from the  
array  
that’s being iterated on, and another the last element for which the  
block  
returned true.

Let Enumerable#inject be an inspiration, I believe #find\_it is supposed  
to  
have similar swissknifeitude. (Groan)

Alright, but I’m getting dangerously close to giving away complete  
spoilers,  
so I’ll leave you some challenge to the problem.

David V.

---

<div class="post-metadata">

**Author:** ![combas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/aca169/32.png) [@combas](https://www.ruby-forum.com/u/combas)\
**Post date:** [February 13, 2006, 1:52am UTC](https://www.ruby-forum.com/t/ruby-course-confusion/66035/5 "2006-02-13T01:52:52Z")

</div>

On Feb 12, 2006, at 20:16, Alex C. wrote:

> On 2/11/06, Matthew S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > 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.

Well, it’s meant to illustrate a very handy property of blocks, but  
it doesn’t assume anything about knowledge of a particular Ruby  
method. It does seem to assume some previous knowledge of how  
searching and sorting works, though.

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

Booleans don’t mean anything by themselves, the semantics is imposed  
by how they’re used. Let’s say I give you two methods that operate  
on two strings: is\_shorter? and is\_longer? The boolean value ‘true’  
has two precisely opposite meanings.

So in this case, the semantics of the boolean value is dependent only  
on what happens in the block. The method calling the block (i.e.  
find\_it) will treat ‘true’ in the same way no matter what its  
particular semantics are.

For example, “true” could mean “the first argument is shorter than  
the second” or vice-versa. It could mean that the first argument has  
more vowels than the second. It could mean that the first argument  
starts with a letter closer to ‘a’ than the second argument. Passing  
any of these blocks to find\_it will change exactly what’s being  
found. (This is a big hint.)

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

Well, from the above examples, I hope it’s clear that they don’t have  
any particular method in mind that returns a boolean. Just that  
there’s a block which takes two things, and returns a boolean value.  
find\_it will deal with a ‘true’ result from one block in exactly the  
same way as it deals with a ‘true’ result from any other block, and  
what that means only depends on the block.

David gave you a few big hints, so I’ll go one baby step further to  
start you off: reduce the problem to a simpler one. Assume that  
you’re trying to implement longest\_string by doing only pair-wise  
comparisons. The comparison is done by a separate method which takes  
two strings and returns true/false. You’ll need to decide the  
following questions, among others:

- What does a ‘true’ result from the comparison method mean?
- HOw should you order the comparisons to get the desired overall  
result?

I would expect that when you get that far, you’ll be able to make the  
connection with how the block version is supposed to work, and why  
it’s able to do any number of different jobs depending on what block  
you give it. If not, I’ll be happy to answer any other questions you  
have (though off-list, probably, since this seems to be getting less  
and less general-interest).

> 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]

You’re exactly right. The point isn’t to teach you how to find the  
shortest or longest string in an array. It’s more to teach you how  
Ruby can use blocks, how the results of general methods can be  
altered depending on the block they’re passed, and how you might go  
about writing such a general method.

Programming examples generally tend towards trivial, easily solved  
things with baroque restrictions on how to do them. Trivial examples  
are picked precisely _because_ they’re trivial - the student knows  
what needs to be done, and can easily see when you have the right  
answer and when you don’t. The restrictions are used to illustrate  
particular techniques and ways of thinking about problems in general.

best of luck,

matthew smillie.

* * *

Matthew S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Institute for Communicating and Collaborative Systems  
University of Edinburgh
