tag:blogger.com,1999:blog-1945584933192119932024-03-08T06:01:20.657+00:00RubyloveRuby. I love it.Michael Baldryhttp://www.blogger.com/profile/11289893983974015091noreply@blogger.comBlogger11125tag:blogger.com,1999:blog-194558493319211993.post-38193583960145831512011-09-05T10:44:00.001+01:002011-09-05T10:44:57.673+01:00Rack::URLMap failing with Phusion PassengerI'll make this quick, I've found that if I try and use Rack::URLMap with Phusion Passenger, it always results in a text/plain page that says 'Not Found: <request path>'. This will not do.<br />
<br />
Something to do with the way Rack::URLMap matches against the map, it uses the env["SERVER_NAME"] which for some reason, Phusion passenger sets to "_", I've written a money patch that sets it to the same value as env["HTTP_HOST"] which solves the problem.<br />
<br />
<pre name="code" class="ruby">class Rack::URLMap
alias_method :old_call, :call
def call(env)
env["SERVER_NAME"] = env["HTTP_HOST"]
old_call(env)
end
end
</pre><br />
You can now use Rack::URLMap as usual and it'll work as expected.Michael Baldryhttp://www.blogger.com/profile/11289893983974015091noreply@blogger.com1tag:blogger.com,1999:blog-194558493319211993.post-2160941994393031832011-05-25T08:59:00.000+01:002011-05-25T08:59:50.816+01:00Useful .profile scripts!Well, I've only written one so far, but it's ultra helpful and saves you remembering all the options you need to pass to standard commands to do what you want. If anyone else feels like commenting with their time-saving commands, maybe I'll build up a library of them.<br />
<br />
Here goes :-<br />
<br />
<h3>files_with - Find files containing a string inside the current directory, recursively</h3><br />
<pre name="code" class="ruby">files_with() { grep -H -n "$*" ./* -R | cut -d: -f1 -f2; }
</pre><br />
<pre name="code" class="ruby">$ files_with hello
./blah.txt:4
./sub/blah.txt:6
</pre>Michael Baldryhttp://www.blogger.com/profile/11289893983974015091noreply@blogger.com0tag:blogger.com,1999:blog-194558493319211993.post-14754861733178743112011-05-14T20:27:00.000+01:002011-05-14T20:27:33.706+01:00Rails 3 partial viewsI got caught out today trying to use a Rails 3 partial view.. I was passing a local in to it but it just wasn't getting picked up.<br />
<br />
<pre name="code" class="ruby">=render "shared/cat_box", :locals => {:cat => @cat}
</pre><br />
<pre name="code" class="ruby">ActionView::Template::Error (undefined local variable or method `cat' for #<#<Class:0x1062afad8>:0x1062aa240>)
</pre><br />
Scratching my beard for a while until I realised that maybe render only looks for locals in the first parameter hash.. e.g. using the full way of rendering a partial:<br />
<br />
<pre name="code" class="ruby">=render :partial => "shared/cat_box", :locals => {:cat => @cat}
</pre><br />
All working fine now!Michael Baldryhttp://www.blogger.com/profile/11289893983974015091noreply@blogger.com0tag:blogger.com,1999:blog-194558493319211993.post-48185038331801991372011-05-06T15:10:00.000+01:002011-05-06T15:10:17.086+01:00Hack days rockI'd just like to say..<br />
<br />
We had a hack day recently and the coolest thing has come out of it. It won't make us any money so it would never have been developed if not for a hack day but it's one of the most useful tools for finding people in our ever expanding office.<br />
<br />
So, all the devs have macbooks (thanks Forward!). We roam around the office, moving rooms, desks, sitting in comfy chairs or on beanbags in multiple rooms around the office - It's great, but people have a hard time finding you.<br />
<br />
Some of the devs came up with an awesome idea - Tail the logs on our router, find all the logins and logouts for each access point throughout the office, take down all the locations of the access points. Now we have a nice page that tells you what area of the office each person was last seen in! You can also get email notification when someone who hasn't been seen in a while logs in to the network.<br />
<br />
Anyway.. Thought I'd share that, because it's awesome.Michael Baldryhttp://www.blogger.com/profile/11289893983974015091noreply@blogger.com0tag:blogger.com,1999:blog-194558493319211993.post-46857314835059691722011-05-03T22:06:00.000+01:002011-05-03T22:06:47.366+01:00A short story about OpenStruct and a performance issueOnce upon a time, in a office far far away, I sat all day, wondering why my report query was taking 0.54 seconds to get the data, then 49 minutes (yes, 49 minutes) to process and render it. Ok, drop the story telling now. <br />
<br />
It was only 13000 records! I ran ruby-prof (for the first time ever) and spend a while looking at the results wondering what they meant :) <br />
<br />
One thing I noticed was that there were quite a few bold lines in the result that were in OpenStruct (thanks for making this easy for me ruby-prof).<br />
<br />
I concluded that calling OpenStruct in a loop was defining a brand new class and defining all the attr_accessors for each of the keys in the hash... in my reporting case, each row is converting to a hash, and each row has exactl the same keys.<br />
<br />
I decided to run a small benchmark with OpenStruct against a class I built that lets you create a Class from a hash, which just adds all the attr_accessors then returns the type, which you can new up with the hash many times over.<br />
<br />
<pre class="ruby" name="code">class DynamicRow
def self.with_keys(keys)
result = Class.new
result.class_eval do
keys.each { |k| attr_accessor k }
def initialize(hash)
hash.each { |k, v| instance_variable_set :"@#{k}", v }
end
end
result
end
def self.benchmark
require 'benchmark'
sample_hash = {:a => "hello", :b => 3}
Benchmark.bm do|b|
b.report("OpenStruct ") do
50_000.times { OpenStruct.new(sample_hash) }
end
b.report("DynamicRow ") do
row_type = DynamicRow.with_keys(sample_hash.keys)
50_000.times { row_type.new(sample_hash) }
end
end
end
end
</pre><br />
Running the benchmark gave me some convincing results<br />
<br />
<pre name="code"> user system total real
OpenStruct 13.400000 0.080000 13.480000 ( 13.572973)
DynamicRow 0.570000 0.010000 0.580000 ( 0.584201)
</pre><br />
Well, thats a nice improvement. But the difference there isn't going to bring the processing down that much is it? The other factor I thought might be playing a role was the number of keys, so I increased this from 2 as shown above in sample_hash to 4.<br />
<br />
<pre name="code"> user system total real
OpenStruct 25.750000 0.160000 25.910000 ( 26.117587)
DynamicRow 0.790000 0.010000 0.800000 ( 0.809647)
</pre><br />
So, with OpenStruct, the time taken increases equal to the number of keys.. with DynamicRow it's not so bad. <br />
<br />
My report now runs in about 1 minute, instead of most part of an hour.<br />
<br />
Hope the experience I've gained today helps someone else :)Michael Baldryhttp://www.blogger.com/profile/11289893983974015091noreply@blogger.com1tag:blogger.com,1999:blog-194558493319211993.post-49154733209587720012011-04-26T11:09:00.002+01:002011-04-26T11:09:46.383+01:00Formatting dates and times in JavascriptSo I needed to format a Date object in javascript today and couldn't find a decent clean solution. What I really wanted was Ruby's strftime.. So I wrote it.<br />
<br />
<a href="http://github.com/michaelbaldry/formatDate-js">format dates in javascript with Date.format()</a>Michael Baldryhttp://www.blogger.com/profile/11289893983974015091noreply@blogger.com0tag:blogger.com,1999:blog-194558493319211993.post-66388170818238234092011-04-21T12:58:00.000+01:002011-04-21T12:58:41.002+01:00Single line rescuesI've just found that you can use <strong>rescue</strong> like you can with <strong>if</strong> and <strong>unless</strong><br />
<br />
<pre class="ruby" name="code">x = (do_something_that_might_raise rescue nil)
</pre><br />
x will be nil if do_something_that_might_raise raises an exception... Obviously catching and swallowing all exceptions from isn't great but it does have it's uses :)Michael Baldryhttp://www.blogger.com/profile/11289893983974015091noreply@blogger.com1tag:blogger.com,1999:blog-194558493319211993.post-268762486269016522011-04-21T12:39:00.000+01:002011-04-21T12:39:34.041+01:00Writing an math expression evaluator: Lexical Analysis<h2>Introduction</h2>I've always been interested by compilers and specifically the effort that goes in to processing a string of code in to something a computer can understand. The most complete but simple example is a math expression parser.<br />
<br />
At first thought, it doesn't sound all that complicated, but giving it a bit more thought, you can't just split an expression like "1 + 2 * (6 / 2)" on spaces and then work out what each part is, what if they enter "5+5"?<br />
<br />
Then you think of actually evaluating the expression... operator precedence, processing the deepest expression parts first, etc.. also, how do you even know it's valid? you may be able to enter "+ + 2 4 ( hello 1" but you would have trouble evaluating that!<br />
<br />
While writing these posts, I've learnt the basic methods for translating and evaluating mathematical expressions and implemented it as a ruby application. Please let me know if there are any improvements I can make, as I'm always looking to improve things :)<br />
<br />
<h2>Lexical Analysis</h2>To make this easier and cleaner, evaluation is split in to 2 phases, Lexical analysis is the first phase, taking the string and converting it in to a stream of symbols which are fed in to the second phase, the parser. The lexer should also raise errors indicating invalid characters and anything it can't find a symbol for.<br />
<br />
As an example "1 + 1 * 3.14159", it would create a stream of symbols like: IntegerLiteral(1) AdditionOperator IntegerLiteral(1) MultiplicationOperator FloatLiteral(3.14159)<br />
<br />
The method I chose was to hold a list of "matchers" that basically match a symbol at the current position in the input string.<br />
<br />
So it'd start out with "1 + 1 * 3.14159" and the first request for a token would return IntegerLiteral(1) using a simple regular expression like: /^[0-9]*/. return IntegerLiteral and advance the string the number of characters that were matched.<br />
<br />
now we are at " + 1 * 3.14159" a regular expression could match /\s+/ and just skip it because we aren't interested in that. it'll still advance the string the number of whitespace characters it matched.<br />
<br />
We get to the plus, a simple literal check will match this so if code[position] is "+" then add one to the position and and return a AdditionOperator.<br />
<br />
And so on<br />
<br />
I have implemented this in a generic a way as possible to allow for other symbols and matchers to be lexed without changing the code code.<br />
<br />
<pre class="ruby" name="code">class Lexer
def initialize(str, matchers)
@str = str
@position = 0
@matchers = matchers
end
def consume
return Symbol.new(nil, 0, :end) if @position >= @str.length
part = @str[@position..-1]
@matchers.each do |m|
result = m.match(part)
if result
@position += result.length
return self.send :next if m.ignore?
return result
end
end
raise "Unable to lex: #{part}"
end
end
</pre><br />
To create a Lexer class that would give us the correct symbols for a math expression, you just create a matcher for each type of symbol. This code isn't very pretty, but some nice DSL will be written later!<br />
<br />
<pre class="ruby" name="code">matchers = []
matchers << RegexMatcher.new(/(\s)+/, :whitespace, :ignore => true)
matchers << LiteralMatcher.new('+', :add)
matchers << LiteralMatcher.new('-', :subtract)
matchers << LiteralMatcher.new('*', :multiply)
matchers << LiteralMatcher.new('/', :divide)
matchers << LiteralMatcher.new('(', :left_paren)
matchers << LiteralMatcher.new(')', :right_paren)
matchers << RegexMatcher.new(/(\s)+/, :whitespace, :ignore => true, :)
lexer = Lexer.new("1 + 1 * 3.14159", matchers)
</pre><br />
<h2>Well - I forgot about this post!</h2><br />
Since I started writing this I've now written a gem for lexical analysis with the pretty DSL I said could be done later.. now it looks like this:<br />
<br />
<pre class="ruby" name="code">ExpressionLexer = Lexr.that {
ignores /\s+/ => :whitespace
matches /[-+]?[0-9]*\.?[0-9]+/ => :number, :convert_with => lambda { |v| Float(v) }
matches "+" => :addition
matches "-" => :subtraction
matches "*" => :multiplication
matches "/" => :division
matches "(" => :left_parenthesis
matches ")" => :right_parenthesis
}
</pre><br />
then can be used like this:<br />
<br />
<pre class="ruby" name="code">lexer = ExpressionLexer.new("1 * 12.5 / (55 + 2 - 56)")
until lexer.end?
puts lexer.next
end
</pre><br />
giving you an output of:<br />
<br />
<pre class="ruby" name="code">number(1.0)
multiplication(*)
number(12.5)
division(/)
left_parenthesis(()
number(55.0)
addition(+)
number(2.0)
subtraction(-)
number(56.0)
right_parenthesis())
end()
</pre><br />
Ok, so this has kind of turned in to a shameless plug for my own gems.. Here is the details :)<br />
<br />
<a href="http://github.com/michaelbaldry/lexr">Lexr - A simple but powerful lexical analyser for Ruby</a><br />
<br />
There is also another gem I wrote that uses this called <a href="http://github.com/michaelbaldry/math_engine">math_engine</a>. It's a recursive decent parser which parses math expressions and gives you the result. It was a great experience writing these and I learnt a lot. Hopefully you can learn from them too.Michael Baldryhttp://www.blogger.com/profile/11289893983974015091noreply@blogger.com0tag:blogger.com,1999:blog-194558493319211993.post-75233996506564650322010-09-02T11:09:00.001+01:002010-09-02T11:10:18.589+01:00Removing the 3D border on an iframe in IESomething I came across worth remembering in future.<br />
<br />
Internet explorer puts a 3D border on iframes... this is removed by adding a frameborder="0" on the element. This is not W3C compliant but is the only way to remove it in IE. Firefox and other browsers respect the border: none; in CSS.Michael Baldryhttp://www.blogger.com/profile/11289893983974015091noreply@blogger.com0tag:blogger.com,1999:blog-194558493319211993.post-43171016849153157812010-08-23T12:44:00.001+01:002010-08-23T12:45:02.660+01:00Using retry to run exception block againOne cool thing that I've not personally seen in another language is the retry keyword. If you use it in a rescue block, it'll rerun the block of code the exception occurred in.. Take a look at this code:<br />
<br />
<pre name='code' class='ruby'>retry_count = 0
begin
ShoddyWebService.new.call
rescue Timeout::Error => ex
retry_count += 1
raise ex if retry_count > 5
puts "Shoddy web service timed out..retry #{retry_count}/5"
retry
end
</pre><br />
Now, if your web service call fails 5 times, the code will fail. This makes your applications more resilient to external problems, as services timing out does happen once in a while.. I'd rather my application handled that nicely instead of just bombing out.Michael Baldryhttp://www.blogger.com/profile/11289893983974015091noreply@blogger.com1tag:blogger.com,1999:blog-194558493319211993.post-70902059864534781252010-08-18T15:20:00.001+01:002010-10-22T09:00:56.927+01:00Slick code with simple DSLs thanks to RubyOne of the things I love about Ruby is the massive advantages gained by the way it's built - things like blocks make it so easy for Ruby to have amazing looking, clean code. The great thing is, this also extends to writing your code in Ruby!<br />
<br />
Want the people consuming your code to have easy to understand, learn and read code? Write a nice DSL for it. (I'm going to take a second here - DSL stands for Domain Specific Language.. kind of like another programming language, that is structured in the best possible way to interact a specific domain)<br />
<br />
Take for example an email sending class..<br />
<br />
<pre class="ruby" name="code">Email.new("clocKwize@gmail.com", "spammaster@yahoo.com", "I love you", "Not really!").send
</pre><br />
Now, it's pretty straightforward what this does, but it'll be easy to forget which email it'll send to and which email it'll appear to come from.. Lets write a DSL to make this way more readable, and more extensible, should we need to add more parameters and options later<br />
<br />
<pre class="ruby" name="code">Email.send do
to "clocKwize@gmail.com"
from "spammaster@yahoo.com"
subject "I love you"
body "Not really!"
end
</pre><br />
Now, that code may be longer, but no developer will ever find it hard to see exactly what the code is doing, they'll never have to go and look at the definition of Email to find out.<br />
<br />
Now, there is no harm in providing both ways to use your code, you don't want to restrict people to use a DSL, it's usually a layer built on top of an API. Here is how I wrote the code initially<br />
<br />
<pre class="ruby" name="code">class Email
def initialize(to = nil, from = nil, subject = nil, body = nil)
@to, @from, @subject, @body = to, from, subject, body
end
def self.send(&block)
email = Email.new
email.instance_eval &block
email.send
end
def to(value)
@to = value
end
def from(value)
@from = value
end
def subject(value)
@subject = value
end
def body(value)
@body = value
end
def send
puts "Sending an email!"
end
end
</pre><br />
Now this works nicely by using a cool method called <b>instance_eval</b>. This basically takes a block and runs it but with a 'self' of whatever you are calling instance_eval on. In this case, when the block is run, and it comes across to "clocKwize@gmail.com" - this is calling self.to("clocKwize@gmail.com") on the Email object created in send. <br />
<br />
The only draw back to this is, once self is our email object, we can no longer access instance stuff in the class we are using the DSL in!<br />
<br />
For example<br />
<br />
<pre class="ruby" name="code">class TestClass
def initialize(magic_number)
@magic_number = magic_number
end
def go
Email.send do
...
body "Magic number is: #{@magic_number}"
end
end
end
</pre><br />
Now, this looks like it should work, but it wont because Ruby will be looking for @magic_number on the Email object, which isn't what the user of your DSL intended at all.<br />
<br />
The best way around this to pass the instance of the Email to the block as a parameter. Not quite as slick but the block keeps its original scope and works fine:<br />
<br />
<pre class="ruby" name="code">Email.send do |email|
...
email.body "Magic number is: #{@magic_number}"
end
</pre><br />
This is accomplished by changing the self.send method as follows<br />
<br />
<pre class="ruby" name="code">def self.send(&block)
email = Email.new
block[email]
email.send
end
</pre><br />
But you know, I kind of like the first way, and if the user doesn't need any instance stuff from the class they are in, we shouldn't stop them using the prettier DSL. A very simple way to do this is to ask the block how many parameters it accepts..If it takes one, pass the Email instance, if it doesn't use self as the Email instance.<br />
<br />
<pre class="ruby" name="code">def self.send(&block)
email = Email.new
block.arity == 1 ? block[email] : email.instance_eval(&block)
email.send
end
</pre><br />
There we go.. the best of both worlds! Consumers of your DSL can use whatever way they are comfortable with and your code will work either way. Hopefully this will help someone on their path of discovery.Michael Baldryhttp://www.blogger.com/profile/11289893983974015091noreply@blogger.com3