All Geek to Me

July 29, 2009

Ruby Dangerous Method

Filed under: Geek Stuff — britinla @ 11:10 am

In Ruby method names ending with an exclamation mark are considered “dangerous” as they change the object on which the method is invoked.

I ran into a problem using one of these dangerous methods last night. I wanted to ensure that strings that I was handling were uppercase. There are two methods in Ruby that do this: upcase and upcase! I attempted to use the latter option. It worked on my first tests, but it failed when I passed a string that was already in uppercase and the string was changed to nil (empty string). This behaviour is explicitly documented, but I had missed that detail. Using the regular method, s=s.upcase works as I required, so it was a simple fix. However, this is an annoying difference in behaviour between the two method and seems to be counter- intuitive.

July 26, 2009

Grooveshark

Filed under: Geek Stuff — britinla @ 6:00 am

I have used LAST.FM and the US only Pandora, both of which create playlists of streamed music based on artists and/or songs that you select. Today, via a website called similarsites.com I found Grooveshark. This provides a more flexible solution, allowing you to play specific songs, assemble a playlist of selected songs, as well as having software selected music. It combines the software and manual selection, by providing a list of similar songs. You can then add any of these songs to your list.

My first list was as follows:

Thula Hin Ginjet King Crimson
Mystic Rhythms Rush
Fat Angel Jefferson Airplane
Welcome to the Machine Pink Floyd
Comfortably Numb Dave Gilmour and Bob Geldof
Smokin Boston
Tequilla Sunrise Eagles
Wolfpack Syd Barrett

Note that Comfortably Numb in the above list is a link, which allows you to listen to it.

July 25, 2009

A Bottle of Tequila and a Loaded Gun

Filed under: Geek Stuff — britinla @ 8:29 am

I am learning to code in Ruby. Ruby is an interpreted object orientated programming language with some powerful features.

For instance, variables are dynamically typed as illustrated by the following snippet of code:

myVar = 4
print myVar.class
myVar = “Four”
print myVar.class

This code will print Fixnum and then String, indicating that the variable changed class depending on the assigned value.

Classes are never closed, you can add methods to them at any point.

class MyClass
    def sum(a,b)
        return(a+b)
    end
end

myObject=MyClass.new
print myObject.sum(1,4)

class MyClass
    def product(a,b)
        return (a*b)
    end
end

print myObject.product(2,4)

This code will print 5 and then 8

myObject is created from MyClass at a point at which the only method in the class is sum. A new method is added to the class and the existing object is able to to use that method even though it did not exist at the time of its creation.

Even system classes can have methods added or overridden. Changing the behaviour of existing classes, known as Monkey Patching, is an interesting, but very dangerous technique; like the bottle of tequila and loaded gun of this post’s title. It can be useful if it adds functionality to existing classes, providing a string comparison that returns a number expressing a percentage confidence that two strings were intended to be equal. This allows the user to key Califonia and for the program to accept that this was probably intended to be California. It can also be used to change existing methods in classes; having the equality comparison return true for 2+2=5 is only useful in the world of 1984, but being able to fix a problem in a supplied Ruby library is clearly invaluable.

With Great Power comes Great Responsibility

July 17, 2009

Ignorance is Strength

Filed under: Geek Stuff — britinla @ 10:19 pm

Books should consist of pages made from paper. Even though I am a geek of the highest order, I have not bought nor considered the purchase of Kindle,  Amazon’s electronic book reader. Since I enjoy reading books in the bath, an expensive piece of electronics that would not do well vanishing into the bubbles does not seem a good idea. Today comes an even more persuasive reason for avoiding this device. Amazon, at the publishers behest, removed purchased copies of two books from Kindles across the US. They did credit the victims of this theft, but I would not be happy if Barnes and Nobles took the book I was reading from my bedside table and left a few crumpled notes in exchange. The problem is that that you do not purchase Kindle books, you merely lease them at the whim of the publisher. The final irony in this tale is that the books removed as if they had never existed were Orwell’s Animal Farm and 1984. These books can be purchased from all good bookstores and from Amazon.

[New York Times]

Blog at WordPress.com.