Tuesday, March 10, 2009

Duck Typing

I learnt how to program in Ruby, and as we all know it's a very flexible language. Recently I've been doing a lot of stuff in C# on windows, and a lot more stuff in Objective-C on the iPhone. Something finally clicked for me, when I was using these languages, and that was Duck Typing.

Ruby, unlike the C style languages, couldn't care less about what type of object it is dealing with. Now before you start, I will repeat, it couldn't care less about what type of object it is dealing with.

What Ruby cares about is if object responds to the method you're sending it. In Ruby there is even a respond_to? method to assert this. i.e.

1.respond_to?(:to_s) #=> true
1.respond_to?(:foo_bar) #=> false

So you can have two objects, of totally differing types, and instead of asserting type like you might in say C#, you can instead check that the object responds to a method. The great thing about this, is that if it doesn't and you want it to, you can use Ruby's meta programming to dynamically inject that method into that object. For instance

unless 1.respond_to?(:foo_bar)<br />   1.class.class_eval do<br />      def foo_bar<br />         self * 100<br />      end<br />   end<br />end

1.foo_bar #=> 100
2.foo_bar #=> 200
30.foo_bar #=> 30000

#=> NoMethodError: undefined method `foo_bar' for 12.2:Float

So we've used class_eval to dynamically inject the method foo_bar in the class of 1(Fixnum), and so now all instances of Fixnum will respond to that method. We've never even checked it's type, we don't care, all we know is that we want it to respond to the method foo_bar and now it does. Obviously our Float class doesn't have this method hence the reason that 12.2 has raised the undefined method error. Any guesses on how to make this work :D, yeah that's right, class_eval and respond_to? will do it, just like we did with Fixnum.

So in summary what is the point of Duck Typing? Why not just check the class, and respond with what we as coders know it can and cannot do(or think we know it can do)?

Well, seriously why do you care what class it is? When what you really wanted to know is, whether it's going to respond to a method? Sometimes class and type is important, and obviously those features are there and should be used when class is important, but sometimes you just need to stop and ask yourself.

Do I can about type or do I just want to know if I can execute a method on this object?

Tuesday, June 17, 2008

Functional Ruby methods

Ruby is a great language and normally I'd roll it in a object oriented fashion, however I've just started looking at Haskell, and thought that lambda could give me something similar to Haskell functions in Ruby. albeit without a ton of Haskell's features.

So here are two examples, the first is normal OOP Ruby

b = Time.now
class Integer
  def main
    self * self
1000000.times{ |i| i.main }
puts "Time: #{Time.now - b}"

On my machine this returns "Time: 1.115471"

Here is a functional style version.

b = Time.now
main = lambda { |x| x * x }
1000000.times &main
puts "Time: #{Time.now - b}"

First thing to note is that it's only 4 lines of code, not 8. Second thing is that it returns the following to the terminal:
"Time: 0.960359"

That's 0.155112 faster, just over 15% faster in computing a simple algorithm like this. Of course this is where functional programming is meant to shine, this is simply an experiment to see if I could do this is Ruby, it is hardly a well tested benchmark, and the results are not all together unexpected, but it's cool in any case.

Thursday, June 5, 2008

Targetting IE7 with CSS

I've needed to target IE6 & IE7 within my css for 1 margin rule, so rather than some convoluted process I used the following simple hack

margin: 10px 10px 5px 10px;
*margin: 10px 10px 10px 10px; /* Targets IE6 & IE7 */
_margin: 5px 5px 5px 5px; /* Targets IE6 */

Yes it is invalid CSS, but I don't really care, it's 1 - 2 lines in a 200 line stylesheet.

Thursday, May 1, 2008

Ignoring files with SVN

This is one of those things that I've just never really got my head around. I'm not sure why, I suppose I just never took the time to read the manual. Stupid me!

After filling up my drives on my svn host, I found that it was pretty much log files that were killing me, so now I needed to work out how to do it. You'd think it would be simple, something like svn ignore log/development.log, but noooo.

I won't rant to much here about what svn propset and svn propedit do, you can read the manual page here

Short of it all is follow these instructions.

1. Create a file that will list all the files you want to ignore, I've called mine .svn_ignore and it is in the root of the trunk of my repository

2. Edit that file and add a list of the files you want to ignore, mine looks like this(it's for a rails project)

The file is just a newline separated list of the files you want to ignore, as you can see you can use wild cards.

3. Now for the fun bit, first make sure that the files you want to ignore aren't checked in yet.
$> svn st
? log/production.log
? log/test.log
? log/development.log
? db/schema.rb
? db/development.sqlite3
? db/test.sqlite3

4. Execute the following command to ignore all of the listed(in your .svn_ignore) files.
$> svn propset svn:ignore -F .svn_ignore ./log/
property 'svn:ignore' set on 'log'

$> svn st
? db/schema.rb
? db/development.sqlite3
? db/test.sqlite3

5. Obviously you can now apply this same files to your db directory.
$> svn propset svn:ignore -F .svn_ignore ./db/

$> svn st

Happy times.

Thursday, April 24, 2008

Rending your XML as HTML with XSLT in Rails

I had a need to convert XML services that my rails app was providing into customizable html widgets tht could then be in-jested by some sort of server side includes(like NGINX's SSI). So I needed to use XSLT.

When the W3C wrote the XML specification they also developed the Extensible Stylesheet Language for Transformations. XSLT provides a powerful, flexible language for transforming XML documents into something else. In this case, we're going to use it to create HTML documents.

So to quickly look at how we're rendering xml currently.

def show
@event = Event.find(params[:id])
render :xml => @event

OK so we are simply rendering only xml back from this request. Now of course we can react to any registered mime-type we want, obviously HTML is one of those. So we do something like....

def show
@event = Event.find(params[:id])
responds_to do |format|
format.xml { render :xml => @event }

So, I'm going to assume that you've seen this responds to block before and you know that it's just going to render the show.html.erb template in our views, when html is requested.

Installing Ruby's XSLT Library

I'm on Mac OS-X 10.5.2 and haven't tested this install on anything else, but it's pretty simple really, unfortunately it's not a gem, but hey, it's not that hard

First you'll need to download the library, you can get it from here

Unzip that guy and in your terminal navigate to that directory, then execute the following commands(You'll need Make installed so ensure you've XCode installed on OS-X, or some sort of GCC/Make):
ruby extconf.rb
make test
make doc
sudo make install

Fire up irb and ensure you return true from require 'xml/xslt' if you get a false, the library hasn't installed properly, you can probably find help at http://greg.rubyfr.net/pub/packages/ruby-xslt/files/README.html

Getting Rails to render:

Firstly, include your new friend in environment.rb
require 'xml/xslt'

In my application controller I've added a private method

def xslt(_xml, _xslt)
xlt = XML::XSLT.new
xlt.xml = render_to_string :xml => _xml
xlt.xsl = _xslt

Now in your controller method(I'll use the example from above)

def show
@event = Event.find(params[:id])
respond_to do |format|
format.html { render :inline => xslt(@event, "#{RAILS_ROOT}/xslt_template/show.xslt") }
format.xml { render :xml => @event }

Create an XSLT Style

You're of course going to need XSLT styles to apply to pages.
I'll start with the XML I'm using. It's just rendered by the show method from above:

<created-at type="datetime">2008-04-23T18:36:28+10:00</created-at>
<date type="date">2008-04-23</date>
<id type="integer">1</id>
<location>Camperdown Park</location>
<title>Cardboard Tube Fighting</title>
<updated-at type="datetime">2008-04-23T18:36:28+10:00</updated-at>

And this is the XSLT template I'll use to style this.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" />

<xsl:template match="/">
<xsl:apply-templates select="event/location" />
<xsl:apply-templates select="event/title" />

<xsl:template match="title">
<xsl:value-of select="." />
</xsl:template><br />
<xsl:template match="location">
<xsl:value-of select="." />

Smile like a bandit!

terminal: script/server
Navigate to the path of your method and voila you've just applied an XSLT style to your XML stream dynamically.

Next is for me to roll this into a Rail templating language... But that's another post.

Thursday, April 17, 2008

Programmers aren't creative

I used to be a photographer, and recently I've been selling some of my gear to new photographic hopefuls. Of course the inevitable questions arises about what I do now?
I'm a programmer I respond.
The usual response is:
Oh, that's not a very creative field to go to. I mean you used to be a photographer.

Well I'm going to put this question out there to the world. Is programming not creative? Really stop and think about it.
As someone who has come from a creative field to a "non-creative" field, I can tell you that in my opinion programming is very creative. Programmers are constantly asked to produce a completely new thing, from nothing at all. To create a program. Photographers are generally asked to capture something there in front of them. Now I'm not here to start the whole photography isn't creative debate I feel there is a degree of creativity in photography, but why does programming seem to have this non-creative air about it.
Is it Bill Gates fault? Or all the other a-typical nerd looking boys made infamous in the '80s for the development of Windows etc. How long will it take for people to put together the iPod/iMac/iPhone with programmers? All those cool funky user interfaces, those problems are solved by programmers... Are they not creative solutions?

So we come to the question, what is creativity?

the ability to transcend traditional ideas, rules, patterns, relationships, or the like, and to create meaningful new ideas, forms, methods, interpretations, etc.; originality, progressiveness, or imagination

I don't know about you, but that pretty well sums up about 8/10ths of my job as a programmer.

Anyone else feel that their industry is unnecessarily brandished with the non-creative iron?

Wednesday, April 16, 2008

Mocking and Stubbing in rSpec

I program in Ruby. A lot. I've recently found myself writing applications to provide simple web services to flash applications. We've pretty well standardized on using JSON as our preferred format at Snepo, so I've found it useless attempting to build and delivery anything without using some sort of TDD.

Welcome rSpec to my world, and it adds in a level of communication between myself and the involved parties about what a service should do, that I'm not sure any other framework does. For instance, we can begin specifying behaviors that the controller should return.
I might have a service that finds me all users who've been active in the last 24 hours. So I'd specify, like follows.

describe "Users Controller", "find_recent action" do
it "should find all users who've been active in the last 24 hours" do
c, r = request("/users/find_recent/24")
c.body.should eql(["some json array of elements"])

But! Here is our first problem with outside in development, we don't have a any methods in a User model yet! Now we could go off and build some, but this behavior might change as we move forward and in any case, we should test those methods with their own tests, this is testing the controller class after all. So why not try to get as much of this working how we want now. Well we can.

Hello Mocks and Stubs!

Mocks and Stubs confused me for a long time. The official definition in rSpec says: Mock objects are imitation objects that give you declarative control over their behaviour in the course of the execution of an example

I found this really confusing so I'll try to explain it, in my own words.
A mock allows you to send a message to an object(including a class, a class is an object after all) that does not yet exist( that is the message/method does not yet exist ). The rSpec framework looks out for the method that your test to call this object, and then returns what you specified in your test. If it is never called it considers that the test has failed. That is, a mock is a test in itself, a stub is not. A stub simply substitutes itself for the real object. So to flesh out the above example with a mock.

describe "Users Controller", "find_recent action" do
it "should find all users who've been active in the last 24 hours" do
@users = mock("A list of users")
c, r = request("/users/find_recent/24")
c.body.should eql(@users.to_json)

What we are doing here is creating a new mock object(@users) so we have something to return from our mock of the static method find_all. That happens in the line below, we tell rspec that it should_receive a message (:find_all) for a class called User, and when it does receive that message to return the mock object that we created above for the controller to keep on using it.
If we just wanted to stub that, i.e. if we didn't care if it got called, but if it does we'd better return something, we'd use: User.stub!(:find_all).and_return(@users).

There is one small misnomer in that whole mocking stubbing framework in rSpec, and that is @users = mock("A list of users"). This is not a mock, if it's not used rSpec doesn't care, there is no expectation placed on this object, to me this should be something like stub_object, in any case, that's a little gottcha albeit a linguistic one.
Now you see, you can get to it from the outside in this time and you can get all the model method's that you'll need sorted before your write a scrap of code in your models.
Oh and just so you know, your controller method might look something like this( in Merb ).

def find_recent
users = User.find_recent(params[:duration].to_i)
render_text users.to_json