Doug Bryant

Tech thoughts and notes

OSCON 2005 - Day 1 (Ruby W/ Dave Thomas)

Finally…, it’s here. And the line up for today is Intro to ruby w/ Dave Thomas and Rails with DHH.

Dave Thomas is one of my most favorite technical authors – this should be good…

Introduction to Ruby

Is programming still fun? in and of itself it is fun – it’s the language and the tools. Rise of scripting language make it more fun b/c less time to run program.

Ruby born in japan 1994 – Mats

Ruby passes the 5 minute test.

Wait – this is funny – Dave is presenting in socks.

Ruby removes alot of the inherited cruft of other languages – no need to put () around method and class definitions – don’t need to put ; at the end of a line. These things are unnecessary -the compiler/interpreter can figure it out.

new is not a keyword. It is built into the object. Makes overriding the functionality of creating the object more flexible (perhaps by using class level variables?)

attributes begin w/ @

Attributes and methods are one and the same. Makes for universal access
precode
@my_var
attr_reader :my_var
/code/pre

is the same as

precode
def my_var=(x)
@a = x
end
def myvar
return @a
end
/code/pre

Blocks and iterators are pervasive in Ruby
The santa clause theory: precode3.times { puts “Ho!” }/code/pre

variable

is called a block parameter

yield inside a method definition looks at code and stores in your “back pocket” which it executes later. (Thats a pretty crappy writeup…)

Ruby convention for iterators. Block should test for params. If block is not given , code should store in an array and return at the end of the method/iterator call.

Blocks as callbacks – use callback syntax as argument to initialize. If block not given, nil is stored. Can use later like
precode
callback.execute if callback
/code/pre

Resource management w/ blocks. Use block with resources to ensure resources are closed afterwards.

precode
File.open(‘/etc/passwd’) do
#… do something
end
/code/pre

File will be closed automagically at the end of the block.

Did not know this. Per-statement exception handling
precode
def x(name)
f = File.open(name)
yield f ensure f.close
end
/code/pre
where yield f ensure f.close is the per statement exception handling

Transparent block passing
precode
def x(name, callback)
File.open(name, callback)
end
/code/pre

Duck typing. Ruby has strongly-typed objects but untyped variables and methods. Type is determined by object protocol (by what object can do) Just have to make sure the types you are passing in support the methods that the object needs. If it walks like a duck and talks like a duck, it must be a duck.

Ruby community differentiates the type and class of an object

  • type: what it can do
  • class: what generated it (who created this object)

Metaprogramming mini DSL’s within you classes
How do we get there:

  • Classes are open and can be extended – ex: can add stuff to string. ex – could add an encryption method to string so you could say cat.encrypt. yes it is dangerous to redefine core class methods like overriding the + method on Fixednum, but just b/c some idiot might do it doesn’t mean it should not be there. I you do something stupid, take ourself out to the parking lot and beat self w/ rubber hose.
  • Definitions are active Add to classes with things such as adding tomorrow and yesterday methods to the Time class. One thing that was really driven home for me is that you can execute arbitrary code in you classes (functional). For ex. Caching. in one of your classes, you could read a file and store it in a string as part of the definition. Then methods on the call would have ready acccess to the string or array. Put another way, you can write code that executes during class definitions.
  • All methods calls have a receiver “ruby”.length “ruby” is the receiver.

This is very powerful for metaprogramming
precode
class Doug
end
class Bryant
end

variable = day.even? Doug: Bryant
variable.do_something
/code/pre

Learned you can use the “inherited(subclass)” method on base classes and will automagically be called when you extend the base class.

Other stuff
Object space can tell you about objects at runtime. ex. give me all the strings in my program. Useful for such things as debugging – why is that person object still hanging around

Dave Thomas is an excellent speaker. I was very impressed. If you ever get a chance to hear him speak, do it. He has a very clear and concise way of explaining things. Not to mention a good sense of humor. He keeps things technical and assumes the audience are not all dumbasses – he knows we have all done some sort of software development before and does not stick on the syntax of the language.