Forum: Ruby ruby course confusion

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
622fa8560c82dfaa59c91ec75efb0c19?d=identicon&s=25 Alex Combas (Guest)
on 2006-02-12 06:23
(Received via mailing list)
Working my way through the "ruby course"
http://ruby-doc.org/docs/Immersive%20Ruby%20progra...

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.
31af45939fec7e3c4ed8a798c0bd9b1a?d=identicon&s=25 Matthew Smillie (Guest)
on 2006-02-12 07:01
(Received via mailing list)
On Feb 12, 2006, at 5:23, Alex Combas 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 Smillie            <M.B.Smillie@sms.ed.ac.uk>
Institute for Communicating and Collaborative Systems
University of Edinburgh
622fa8560c82dfaa59c91ec75efb0c19?d=identicon&s=25 Alex Combas (Guest)
on 2006-02-12 21:16
(Received via mailing list)
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]
430ea1cba106cc65b7687d66e9df4f06?d=identicon&s=25 David Vallner (Guest)
on 2006-02-12 21:32
(Received via mailing list)
DÅ?a Nedeľa 12 Február 2006 21:16 Alex Combas 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 Vallner
31af45939fec7e3c4ed8a798c0bd9b1a?d=identicon&s=25 Matthew Smillie (Guest)
on 2006-02-13 01:52
(Received via mailing list)
On Feb 12, 2006, at 20:16, Alex Combas wrote:

> On 2/11/06, Matthew Smillie <M.B.Smillie@sms.ed.ac.uk> 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 Smillie            <M.B.Smillie@sms.ed.ac.uk>
Institute for Communicating and Collaborative Systems
University of Edinburgh
This topic is locked and can not be replied to.