Ruby Book for People Who Aren't (Yet) Programmers

I like the idea of loops that sound like English. I want to make them as
simple as possible. I am currently thinking of this:

  1. First introduce :
    3.times do

    end

This is the simplest loops possible, and the most like English.

  1. Next do conditionals:
    if cond

    end

  2. Now, with the background of conditionals, move on to ‘while’:
    while cond

    end

I will not use the word ‘block’, or ‘iterator’. I will not show this
example:
3.times do |var|

end

I will just tell them that this is a loop that blindly does the stuff
inside 3 times. No mention of iterators or blocks or anything else. I
think it’s a great introduction to loops.

What do you think?

Daniel.

···

Yes, if it’s ‘while true’. However:

while (number < 400)
puts ‘Still too small…’
number = number * 2
end

…seems almost like english. “While the number is less than 400, write out
‘Still too small…’, and then…”

When would you ever use the phrase ‘loop do’ in a sentence? Maybe ‘loop
doing’, or ‘In the loop, do…’

Even so, I really want to avoid the ‘do’ until after blocks have been
well-explained. That bit me time and time again. Am I the only one this
happened to?

If so, then I will think about it some more… no promises, though. :slight_smile:

Chris

I don’t think there’s anything wrong with calling them floats and integers
right away (though I would avoid Fixnum and Bignum until you really
introduce classes in depth). Same with strings. You have to call them
something, so go ahead and call them by their real names. If you call
stuff ‘text’ in one chapter, then switch to ‘string’ in the next, I think
you are more likely to confuse the nuby.

What I was trying (and failing) to say was that talking about the String
CLASS or String OBJECTS was a bad idea (so early). Just call them strings.

So you suggest I quickly start using the terms ‘String’, ‘Float’ and
‘Integer’. Is that right?

What do you suggest I do when the student finds out that ‘3.class’ returns
‘Fixnum’ instead of ‘Integer’?

I don’t see why you have to introduce ‘Bignum’ as soon as you introduce
‘Fixnum’. One could just use the terms ‘Fixnum’, ‘Float’ and ‘String’ and
never mention ‘Bignum’.

I realize that ‘Fixnum’ is an ugly name, but it’s the one that Ruby uses.

Daniel.

Daniel Carrera wrote:

I hate to say this but the Windows irb (at least the one packaged with
the PragProg installer) is awesome for multi-liners. If you go back in
the history and select a line, then the next line’s history will begin
at the selected line.

Guys, why not simply use the ruby interpreter.

run ruby.
type your short script in
press ctrl-d (and enter and windows)
and your script executes.

Even better, if you make a mistake early on, ruby drops out with the
parse error. (okay a hassle if you are typing something big in, but then
you should be using files or irb)

Rob

My personal feelings about variable substitution was that I put off learning
it until I really had to, and I still almost never use it, in favor of
string concatenation. I just don’t see the point. […]

My 2 cents, anyway.

I must say, I find this opinion absolutely amazing. I am being forced
to code Java at work at the moment, and of course there are many things
I hate about it. Somewhere near the top is the lack of variable
substitution. I suppose if Vim couldn’t handle the highlighting, it
would be off-putting.

I don’t see what the big deal about highlighting is. Regardless of syntax
highlighting, I think that this:

puts “Result: #{num1} + #{num2} = #{num1+num2}”

Is more readable than

puts 'Result: ’ + num1.to_s + ’ + ’ + num2.to_s + ’ = ’ + (num1+num2).to_s

But that’s just my opinion.

Regardless, I concede Chris’ point. In a tutorial it’s best to not show
too many ways to do it. I think I’ll go the single-quote route.

Daniel.

dblack@candle.superlink.net wrote:

What is BDUF?

Big Design Up Front.

I really love you guys…

Ya’ll are making me look good at work.

Thanks for the help :slight_smile:

Jason

···

— “Gennady F. Bystritsky” gfb@tonesoft.com wrote:

----- Original Message -----
From: “Jason Persampieri” jason@persampieri.net
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Thursday, December 05, 2002 2:42 PM
Subject: Hash ‘issues’

OK, I’m messing around with hashes for the first
time,
and I’ve hit a (very) small bump.

Let’s say I have a an array where each line
contains
key=value. Also, assume that there can be
duplicate
keys and if so, the final ‘value’ should be an
array.

In theory, I’d like to ‘each’ through the array
and
fill the hash like this…

foo = Hash.new
data.each {|line|
line =~ /^(.)=(.)$/
key, value = $1.split, $2.split # so far, so
good

foo[key].push(value)
}

Now, there are two problems with this.

(1) << (or .push) doesn’t change the array… I
have

Array#push and Array#<< DO change the receiver.

a = [1, 2, 3]
a.size ==> 3
a.push(4) ==> [1, 2, 3, 4]
a.size ==> 4
a.push([5,6] ==> [1, 2, 3, 4, [5, 6]]
a.size ==> 5

to do foo[key] = foo[key].push(value). This is
ok,
but wouldn’t it make sense to have <<= or .push!
methods? But this is completeness stuff and
obviously
not a priority for Matz.

(2) << (or .push) doesn’t know what to do if
foo[key]
is nil (since it doesn’t know it’s of type Array).
No
problem, I think to myself… I’ll just declare
foo as
foo=Hash.new(). Of course, this doesn’t work
since

(foo[key] ||= ).push(value)

now, ALL keys point to the SAME array… if I
modify
one, I modify all of them…doh! I know I COULD
do a
check to see if the value is nil or not, but that
gets
a little messy. I guess what would be nice is if
I
could specify that this is a Hash of Arrays (or
whatever) during instantiation without the
defining of
a default value… something like
foo=Hash.new(Array).

Anyone have a better solution?

Jason

And the construct that got me started on Ruby. After 20 years of
programming in a dozen languages, when I read that “3.times do…” is
a loop in Ruby I said “I gotta get me some of that!”

···

On Sat, 7 Dec 2002 01:35:28 +0900, Daniel Carrera dcarrera@math.umd.edu wrote:

I like the idea of loops that sound like English. I want to make them as
simple as possible. I am currently thinking of this:

  1. First introduce :
    3.times do

    end

This is the simplest loops possible, and the most like English.

Now don’t think that I’m trying to entice anyone to the dark side, but
with Emacs :wink: I’m using a nice little snippet in my ~/.emacs that
would be perfect for nubies:

(defun ruby-eval-buffer () (interactive)
“Evaluate the buffer with ruby.”
(shell-command-on-region (point-min) (point-max) “ruby”))

(defun ruby-custom-setup ()
(define-key ruby-mode-map “\C-c\C-c” 'ruby-eval-buffer))

(add-hook 'ruby-mode-hook 'ruby-custom-setup)

Whatever buffer you are in, as long as it is in Ruby mode (and it will
be if you started with emacs foo.rb'), just press c’ twice while
holding down the control key and the buffer’s content will be piped
into the interpreter. No need to save, no need to switch to the
command line, very IDE-like. The result appears in a new window.

Unfortunately it won’t work at all with interactive programs. Some
day I’ll look into the misteries of comint-modes…

Massimiliano

···

On Thu, Dec 05, 2002 at 05:57:46PM +0900, Harry Ohlsen wrote:

I seem to recall someone wote some macros to execute Ruby code in
vim …

So you suggest I quickly start using the terms ‘String’, ‘Float’ and
’Integer’. Is that right?

What do you suggest I do when the student finds out that ‘3.class’ returns
’Fixnum’ instead of ‘Integer’?

···

----- Original Message -----

Why would anyone use 3.class? As far as I’m concerned, they shouldn’t even
know about the ‘class’ method until they are well beyond my tutorial. Why?
Because it is almost always used for bad things! If anything (and I still
don’t think this is really a good idea), you could use the 'kind_of?'
method, which would work fine with Integer. I find both of these methods
rarely useful in ‘getting things done’, and that’s what a tutorial should
focus on, IMHO.

For example, I have personally found ‘method_missing’ to be far more useful
(and academically interesting) than ‘class’. But that is probably outside
of the scope of my tutorial, too.

I don’t know about you, but I don’t want to rewrite the pickaxe. I just
want to prepare someone for being able to use it confidently. I’m thinking
of my tutorial as sort of an ‘unofficial prequel’.


I don’t see why you have to introduce ‘Bignum’ as soon as you introduce
’Fixnum’. One could just use the terms ‘Fixnum’, ‘Float’ and ‘String’ and
never mention ‘Bignum’.

Perhaps…

I don’t like Fixnum much, but Float and String aren’t really obvious names
to a non-programmer, either.

I guess the reason I favor Integer over Fixnum is that sometimes you won’t
have a Fixnum:

(1234567890*1234567890).class --> Bignum

It just feels wrong to say that non-Float numbers are Fixnums, because they
aren’t… at least not always. If you divide the Numeric type into two
parts and one of them is Float, the other is Integer.

And how do you explain that to_f returns a Float, and to_i returns a Fixnum?

‘999999999999999999999’.to_i.class --> Bignum

Well, to_i doesn’t return a Fixnum; it returns an Integer.

It just feels to me like the ‘right’ way to do it.

Chris

puts “Result: #{num1} + #{num2} = #{num1+num2}”

Is more readable than

puts 'Result: ’ + num1.to_s + ’ + ’ + num2.to_s + ’ = ’ + (num1+num2).to_s

···

----- Original Message -----

Well, I won’t disagree with you there. But, as I said, I have found that
people play with strings more often than numbers, and I do think that

puts 'Hello ’ + name

is more readable than

puts ‘Hello #{name}’

(at least to a nuby) but maybe that’s just me.

Chris

Cool! Thanks for the snippet. I’ve added that and tweaked your lisp
a little to make a buffer syntax check function too.

(defun ruby-check-buffer () (interactive)
“Evaluate the buffer with ruby syntax warn and check.”
(shell-command-on-region (point-min) (point-max) “ruby -wc”))

···

On Wed, Dec 18, 2002 at 07:53:57AM +0900, Massimiliano Mirra wrote:

On Thu, Dec 05, 2002 at 05:57:46PM +0900, Harry Ohlsen wrote:

I seem to recall someone wote some macros to execute Ruby code in
vim …

Now don’t think that I’m trying to entice anyone to the dark side, but
with Emacs :wink: I’m using a nice little snippet in my ~/.emacs that
would be perfect for nubies:

(defun ruby-eval-buffer () (interactive)
“Evaluate the buffer with ruby.”
(shell-command-on-region (point-min) (point-max) “ruby”))


Alan Chen
Digikata Computing
http://digikata.com

Quick note:

Just to clarify, the reason I keep saying ‘my’ tutorial is because I am
referring to the tutorial I am writing, as distinct from the one Daniel
is writing (which is more appropriately referred to as ‘his’ ).

I haven’t posted a link to it because it is still too far inferior to
Daniel’s to even mention. I will post it in a few days when I have… well,
at least the outline done!

I hope it will be beneficial to have both tutorials around. Certainly,
different people learn in different ways, and there are many ways of
ordering the presentation of various ideas. I think that having two
complimentary tutorials would bring up more issues for us to think about.
It seems to have been working out pretty well in this thread, anyway!

Chris

No, that’s entirely correct. One of the great aspects of your tutorial
approach is that you take something familiar - addition - and show how it can
be done with lots of different things.

Substitution has no place in such a tutorial.

Gavin

···

----- Original Message -----
From: “Chris Pine” nemo@hellotree.com

Well, I won’t disagree with you there. But, as I said, I have found that
people play with strings more often than numbers, and I do think that

puts 'Hello ’ + name

is more readable than

puts ‘Hello #{name}’

(at least to a nuby) but maybe that’s just me.

Chris

In article 0cdf01c29c71$c9d6a4c0$6401a8c0@MELONBALLER, Chris Pine wrote:

Well, I won’t disagree with you there. But, as I said, I have found that
people play with strings more often than numbers, and I do think that

puts 'Hello ’ + name

is more readable than

puts ‘Hello #{name}’

They don’t do the same thing. Be aware of the difference between single
and double quotes:

[mike@ratdog Mail]$ irb
irb(main):001:0> name = ‘Mike’
=> “Mike”
irb(main):002:0> 'Hello ’ + name
=> “Hello Mike”
irb(main):003:0> ‘Hello #{name}’
=> “Hello #{name}”
irb(main):004:0> “Hello #{name}”
=> “Hello Mike”

Mike

···


mike@stok.co.uk | The “`Stok’ disclaimers” apply.
http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA
mike@exegenix.com | Fingerprint 0570 71CD 6790 7C28 3D60
http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA