I just finished my “Learn Ruby” talk for the programmers here at work.
(Game programmers, strong in C/C++).
I flew through a massive amount of stuff… leaving continuations and
metaprogramming on the table for another day. I managed to get it done
just over my goal of 90 minutes (maybe 100).
I found that the programmers who’d worked in dynamic languages
previously, through a University course or otherwise, followed along
pretty well. Those without that experience dazed out once we got into
things like lambdas.
Overall though, I’m satisfied.
I did my presentation in HTML generated from ERB. I just set my browser
to full-screen – it worked pretty well. I’ve posted both the ERB and
HTML files at: http://www.waits.net/~swaits/LearnRubyTalk/
I’d very much like to improve the document, so any feedback is
appreciated. It also might be useful to others in my situation
(teaching Ruby to programmers).
I’d very much like to improve the document, so any feedback is
appreciated. It also might be useful to others in my situation
(teaching Ruby to programmers).
–Steve
Looks great, Steve. Thanks for publicizing it.
I’m wondering, how much confusion was there over symbols? Were the
dynamic-language folks more comfortable with symbols than the others?
This is one thing I sometimes find is hard to explain to someone who is
new to Ruby.
I’m wondering, how much confusion was there over symbols? Were the
dynamic-language folks more comfortable with symbols than the others?
This is one thing I sometimes find is hard to explain to someone who is
new to Ruby.
I explained it to them that as a programmer we’d basically use symbols
very similarly to how we use enums in C/C++. I showed how they’re
commonly used as keys in hashes.
I explained that each symbol has a unique ID, and that Ruby uses symbols
to look stuff up within its own system. I believe I had given them
several “.send(:symbol)” examples at that point.
I showed them :symbol.to_i, and :symbol.class too.
I think they understood it. I didn’t notice any difference between
the dynamic experienced and the rest.
I explained that each symbol has a unique ID, and that Ruby uses symbols
to look stuff up within its own system. I believe I had given them
several “.send(:symbol)” examples at that point.
And it’s much better than the “it’s an immutable string” explanation,
which I’ve found to be just a rats nest of problems.
Enums are another good analogy. But in C++/C# enums are typed so you
know the client gave you a valid value. The only way I can see to
enforce this in Ruby is to create a constant array of the valid symbols
and then make sure what they passed me is in the array:
class HockeyGear
BuyableGear = [ :stick, :helmet, :skates ]
def self.buy_something(gear)
if (!BuyableGear.include?(gear))
return nil
end
Somehow I feel this is pretty un-rubylike, to have to have that if
statement in front of every method. Any idea if there’s a more
ruby-like way to have enum-like behavior?
know the client gave you a valid value. The only way I can see to
enforce this in Ruby is to create a constant array of the valid
symbols
and then make sure what they passed me is in the array:
Enums are typed in C++, to a certain extent (better than C anyway),
but if the lhs is an int well…
Somehow I feel this is pretty un-rubylike, to have to have that if
statement in front of every method. Any idea if there’s a more
ruby-like way to have enum-like behavior?
The idiom I usually use is:
class HockeyGear
Stick = Object.new
Helmet = Object.new
…etc…
end
Of course other than the aesthtics of looking like an enum
(HockeyGear::Stick) in the source code there’s no advantage over
symbols. On the other hand, if someone is passing you an enum, it
probably means any one of the enums is valid, so you should be
checking all of them anyway, and throw an exception in the else
clause. Checking whether an object is of the right type isn’t very
ruby-like anyway.
I just finished my “Learn Ruby” talk for the programmers here at work.
(Game programmers, strong in C/C++).
…
I’d very much like to improve the document, so any feedback is
appreciated. It also might be useful to others in my situation (teaching
Ruby to programmers).
Your section on control structures is a little broken. You have “loop do
…
end while …”. The while condition will only be evaluated once (so it’s
equivalent to “if”), and if true “loop” will repeat the block forever.
What
did you intend this example to do?
pretty well. Those without that experience dazed out once we got into
(teaching Ruby to programmers).
I like the way you mixed in a little mixin humor.
“Little Miss Mixins, you are a skanky friend-with-benefits.”
Your section on control structures is a little broken. You have
“loop do …
end while …”. The while condition will only be evaluated once (so
it’s
equivalent to “if”), and if true “loop” will repeat the block
forever. What
did you intend this example to do?
Oh yikes! It’s just a poor example and it’s what I get for working
on this in the wee hours of the morning.
I just finished my “Learn Ruby” talk for the programmers here at work.
(Game programmers, strong in C/C++).
…
I’d very much like to improve the document, so any feedback is
appreciated. It also might be useful to others in my situation (teaching
Ruby to programmers).
While we’re on errata, the @instance_var in the section on Variables may
be
misleading to newcomers. In that scope, it’s “one per instance” of the
class
Dismissed, and there will only be one of these. It’s only in an instance
method (e.g. initialize) that you will have a variable for each instance
of
the class. Again in the Idiomatic Ruby section.
class Foo @instance_var = 0 # belongs to Foo itself
def bar @instance_var = 0 # one per instance
end
end
Mixins:
LessThanOrEqualComparable#>(other) should be “not self < other and not
self
== other”
= should probably be not self < other
Also, I’m surprised you didn’t cover Range in the core classes. All you
have
to do is
r = 1…5 #=> 1…5
r.class #=> Range
r.each {|i| puts i }
Under Everything is an Object, you didn’t use is_a? kind_of? or
superclass,
and you didn’t actually show that anything was an object. You did show
everything has a class, and that Class is a Class. In particular, this
is
not implied by Object.class #=> Class. You can demonstrate it like this:
“foo”.kind_of? Object #=> true
Class.kind_of? Object #=> true
Person.superclass #=> Object
Class.superclass #=> Module
Class.superclass.superclass #=> Object
probably means any one of the enums is valid, so you should be checking
all of them anyway, and throw an exception in the else clause. Checking
whether an object is of the right type isn’t very ruby-like anyway.
Hm, I wrote the attached Enum class a while ago. What do you think?
I’d very much like to improve the document, so any feedback is
appreciated. It also might be useful to others in my situation
(teaching Ruby to programmers).
I only read the first 20% or so (for lack of time at the moment), but I
have to say that it looks great, Steve. I’ve already spurled it,
and will be recommending it to people when they ask about Ruby. It is
extremely heavy on examples and snippets, which I think is a wonderful
thing. Any documentation about programming which has few or no examples
is an exercise in anguish and frustration (can you say perldoc?).
Again, good job. Look forward to future revisions.
symbols. On the other hand, if someone is passing you an enum, it
probably means any one of the enums is valid, so you should be
checking all of them anyway, and throw an exception in the else
clause. Checking whether an object is of the right type isn’t
very ruby-like anyway.
Hm, I wrote the attached Enum class a while ago. What do you think?