Well - the network situation this year is marginally better. I
can actually get to a hotspot, but I have to pay $13 (CDN) for a
day pass. That gives me network access as I sit here in the "Ruby
for Smalltalkers" tutorial with Chad Fowler.
So - to get started on that, Chad's pointing out that Ruby is a
lot like Smalltalk, albeit without the image or tools. So for
instance - in the interactive interpreter, you can list the methods
for a Ruby class, similarly to what you could do in an inspector.
Interesting side point here - Ruby is serving as a "gateway drug"
for a lot of companies - so it may help get other niche languages -
Smalltalk, Erlang, Lisp, etc.
On to Ruby: Variable types
- $variable - begins with a $, so it's global
- local variables (scoped to the area they are defined in -
method, class, etc)
- Pop an @ sign at the front - you get an instance variable
- Class variables exist as well
- Constants exist as well - assignments that begin with a capital
letter are Constant (and global)
Heh: "Java is written for Stupid People". Does not mean that all
Java developers are stupid; rather, it means that Java attempts to
protect you from yourself, such that intelligent developers get
frustrated with the shackles.
One of the interesting shortcuts possible in Ruby is that "self"
can be an implicit receiver of methods. So instead of:
"Smalltalk"
self print.
you can do
print
Which would be the same as:
self.print
Another interesting thing: I can, within a method, invoke super
(without the method name or arguments), and have all of the
arguments sent to the current method implicitly sent up. That also
means that you can't have super invoke a method other than the one
of the same name as the one you're in (which, while it would be bad
practice most of the time in Smalltalk, is possible).
So here's a class definition with some methods:
class SomeClass
def make_one
superclass.new
end
def a_method(a_parameter, another = 123)
@local_here = "whatever"
end
end
So what about naming conventions for methods and/or parameters?
There really aren't widespread conventions there. If you're looking
at a class and set of methods, how do you tell what arguments the
methods take? Documentation, source (etc).
The most interesting part of this session is how the audience is
interacting with Chad - it's like twins that have never met, have
some differences, but get along famously.
Back to Ruby - conditionals in Ruby are syntax. They have
if-then-else and elseif. Some examples:
if a > b
puts "yes"
elsif b > c
puts "asdf"
else
puts "no"
unless 2 > 1
puts "abcd"
end
puts "it is" if 2 > 1
puts "hi" unless false
while a > b
puts "yes"
end
for i in (0..2) do
puts i
end
This is all different than Smalltalk, but then again, likely
much more comfortable for people coming in from Java (etc). Another
example:
1.upto(10) do | num |
puts num*2
end
Is like
1 to: 10 do: [:num |
Transcript show: num*2].
to define a block (closure):
the_block = lambda do | num |
puts num * 2
end
1.upto(10, &the_block)
or invoke the block:
the_block.call(123)
Blocks cannot be serialized. At this point, there was a rather long trip down a rathole as to why Ruby blocks can be used the way they are, and I have to admit I zoned out on that - Why they are structured the way they are is less relevant than the simple fact that they are set up that way. So we're taking a break here, and we'll get on to Rails afterwards. I'll push that as a separate post.
Technorati Tags:
smalltalk