ruby course confusion

A

Alex Combas

Working my way through the "ruby course"
http://ruby-doc.org/docs/Immersive Ruby programming course/RubyCourse=
_1.0-1.pdf

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

Matthew Smillie

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

Alex Combas

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

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 =3D arr.sort_by {|w| w.length};
shortest,longest =3D result[0],result[-1]
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top