12.10.09
Posted in Uncategorized at 11:55 am by JohnB
Update: Ignore all the great ideas below. @defunkt pointed me to jaml, which does a big chunk of what I was interested in.
I’ve followed the work of @hcatlin, @defunkt and @justinbmeyer on templating languages (haml, mustache and javascriptMVC) and have looked for something that met my needs. Here is an idea that seems simpler than javascriptMVC but has more power than haml or mustache – hopefully the sweet spot I’ve been looking for.
The initial idea, inspired by haml but not needing a text parser, was to represent markup as JSON:
Which would generate:
However, this provides no support for interaction with the markup. When considering generating a set of dialogs for user interaction I really wanted field validation built in, like this:
Eventually I realized that wrapping all the functionality as a filter, in essence creating a tiny scrap of functionality, was what I was looking for:
I could see wrapping a form scrap in a larger dialog-handling scrap that knew enough to validate its internal scraps before allowing the OK button to save the data. It should work – I just wish it could be as pretty as the others – but I don’t see that as possible, given the semantics of JSON.
The true test will be when I actually implement the code to support such a construct. Look for a johnb/scrappy project on Github. Until then, I’ll just assume it works!
Permalink
11.30.09
Posted in Uncategorized at 11:05 am by JohnB
I was recently given the opportunity of attending Ruby Conf 2009. It was a lot of fun being with a few hundred of my Rubyist peers. You can now see for yourself via the recently-uploaded session videos or the RubyConf-in-22 minutes video. Or read other RubyConf wrap-ups. Due to the sheer amount of information, my personal wrap-up here is in the boring at-this-time-I-saw-this format. I’ll try to have links to each talk, but they aren’t all available at the time of writing this – I’ll try to update the list later.
Wednesday night I received my ticket via email from a nice guy in New York who couldn’t go at the last minute. Lucky for me!
Thursday 9am the conference started with a keynote from “Matz” (Yukihiro Matsumoto) the creator of Ruby. It was not the most interesting talk I’d heard, but everyone listened politely. Immediately after Matz’s talk I picked up my free Poken device to track the people I met. The Poken was kinda fun at the conference but not very useful unless you’re near a lot of new people who also have them. But at the conference it was kinda fun to touch someone’s Poken to capture a digital business card.
Thursday 10am I attended the BERT and ERNIE talk from one of the GitHub guys, which was fairly close to what I’d already read of the new GitHub server infrastructure. Its amazing what you can do with smart people and enough servers to effectively distribute your workload.
Thursday 11am to 4pm I listened to talks on Bits and Bytes, DSLs and Flying Robots – the quality of these talks was inconsistent and there was not much material I wasn’t already aware of, but I met some interesting people at each session.
At 4pm Thursday was a talk on MongoDB that cleared up some misconceptions I had. This dovetailed nicely with the Friday 9am “Not Only SQL” talk about other non-SQL database options. Unfortunately, I don’t see any obvious winner over my current use of PostgreSQL – there probably is a better one for our particular needs but the cost, in time, of trying the various options seems prohibitive.
Thursday 5pm was my last session of the day, with a presentation on Experiment Driven Development. The basic idea is that rather than relying on your best guess, or your Marketing guy’s best guess about a new feature, you just test both new ideas on actual users before committing to a definite plan of action. He hasn’t put up any slides for his talk (he has a 1 month old baby at home) so you’ll have to rely on my judgement that it was interesting. For me, it means that I’ll be looking for opportunities to capture metrics to measure user response to system changes. I expect the rest of the ConFreaks’ videos will be online in the next week or two.
Rather than stay for the 8pm Lightning Talks I went with my friend Dan and his friends to a yummy tapas restaurant in the Mission.
Friday I learned more than I wanted to know about ongoing improvements to the RubyGems system, partial evaluation of Ruby (pseudo JIT compilation) and an alpha-level persistence layer called Aqua.
At lunch on Friday (and again on Saturday), I watched a fun programming competition to see which team could refactor a chunk of code better and faster. It was all very subjective, but gave a real-world view into how pair programming actually works and other people’s ideas on what makes for a consistent test-driven process.
Friday 2pm was a talk on the latest in MacRuby, a very promising Ruby VM running on top of Objective-C using native objects and garbage collection. It looks promising but I won’t play with it until the 0.5 release is officially out (any day now).
Friday 3pm was Jim Weirich’s talk on SOLID Ruby that expanded on some ideas about Object Oriented Programming that were originally described by “Uncle Bob”. He is a good speaker who really knows his topic so my high expectations were happily met.
Friday 4pm was a fun talk by the always-interesting creator of GitHub, Chris Wanstrath, on what Rubyists can learn from Python.
Friday evening I attended the Startup Crawl, eating tasty food, drinking beer and meeting a few people. Scribd had the best food; Heroku had the best ambiance (and one of their guys solved a technical problem I had on one of my personal sites – nice bonus – instead of telling me to RTFM).
Saturday I woke a bit late but made it in time for the Clojure talk – it seems like a good language for concurrency, but not nearly as easy to write as Ruby. Tim Bray has a nice ongoing series of blog posts on various languages that focus on concurrency. He recently delved into Clojure and it looks somewhat promising (but there is no free lunch).
Saturday 11am I attended an interesting talk on scaling called Synchronous Reads, Asynchronous Writes. Paul Dix went on at length on the various strategies his site has used to improve performance by off-loading work to services that allow eventual data consistency. It looks like a fairly standard infrastructure once you get to a large number of servers but it was good to hear the nitty gritty details discussed.
My last session was Gregg Pollack’s discussion of a number of performance-related gems and plugins. It was essentially what he covers in his screencasts, but since I hadn’t seen them it was well worth hearing it in person. I’ve now subscribed to his Ruby5 podcast which I might finally listen to on the train.
All in all, it was a very good conference. I met, in person, a lot of interesting people that I’d been following in various ways (twitter, blogs, RSS feeds, etc.) and I’d love to go again next year – but I’ll try to get a ticket when they go on sale instead of the night before!
Permalink
09.20.09
Posted in Uncategorized at 10:04 pm by JohnB
I’ve now reconsidered my earlier plan to get rid of my avatar. Why? I’m so glad you asked. Three reasons:
I’m now identified with the avatar. I recently contacted a guy that I hadn’t corresponded with in a while and he said “Oh yeah – I remember you. The guy with the big rock man thing”. So if I get rid of the avatar, I might lose some of the people that would otherwise “recognize” me online.
The Games will blow over but the avatar is still, in my opinion, pretty darn cool. Ya don’t see huge multi-ton monoliths every day.
Time and inertia. I have very little “spare” time in my life and my plan for changing the avatar would likely have taken some time to get right (a series of ~10 images shifting from the rock man to my own smiling face). This is time that would take away from whatever projects I’d otherwise be working on.
So, I hope you like it – you’ll keep seeing it on twitter and other places.
Permalink
05.28.09
Posted in Uncategorized, musings at 1:24 am by JohnB
A few years back, we took a trip from the Bay Area all the way to British Colombia. Near the apex of our journey we saw the most incredible sights. Orca whales, swimming at and under our boat. Brown-colored Black bears (yes, the distinction matters) by the side of the road. And on a mountain near Whistler – the place where I really learned to ski many years ago (such as a boy from San Diego can actually learn to ski) – we saw a huge totem called an Anukshuk. Built of huge Stonehenge-style rocks, it was perched far up the mountain. I liked it so much that I started using it as my avatar on a number of sites.

Yes, I know that it is one of the mascots of the 2010 Olympic games. I thought it would be so cool to make use it, long before the rest of the world associated it with anything other than me. But now, as the date (February 2010) approaches, I wonder when – or if – I should replace it. I’d like it to be a gradual process – but that is not usually the way these things work. You replace one image with another. Whoosh. Done.
And so I ponder. But as I ponder, the Games come closer. And with the date getting closer, it impacts The Locals more and more. To house and feed and transport that abrupt influx of humanity that is The Games, there must be changes. A streamlining of transport. A building of hotel rooms. A “regooding” of the now-deemed-blighted sections of the city. A streamlining of the food establishments, to cater to the oh-hey-theres-a-Starbucks-right-there crowd. Consistency is chosen, time and again, over flavorful melange.
But these Locals speak our language – by gosh they’re almost ‘mericans! Some, like the quirky observer William Gibson, use a delicate turn of phrase to create new words for the process that is unfolding. Others, like Tim Bray of the Urban Geek persuasion (no, I don’t know what that phrase means either) point us toward articles detailing the decline of the locals under an onslaught of progress and, later, of the fights and Pyrrhic victories. How soon until we see articles about the replacement of family owned businesses with multinational conglomerates? And do I want to be associated with all that through my avatar?
Stay tuned!
Permalink
12.21.08
Posted in ruby at 12:25 pm by JohnB
Intending to direct a new Ruby-ist to rubyquiz.com, I hastily misread the Counting Toothpicks quiz as the reverse of what was intended. So here is my quick code to solve the wrong problem: converting a string of toothpicks to its underlying equation and giving the answer. I was quite happy with my short solution until realizing that I had solved a much simpler problem. In any case, here it is:
def toothpicks str
puts str
str.gsub!(' ','')
raise 'Wrong format' unless str =~ /^(x|\||\+|-)*$/
count = str.length
count += str.scan(/(x|\+)/).length
str.length.downto(1) do |n|
str.gsub!( '|'* n, n.to_s )
end
str.gsub!('x','*')
puts str
result = eval(str)
puts "#{result} from #{count} toothpicks"
end
if ARGV[0]
toothpicks ARGV[0].dup
else
toothpicks '||| x || + ||||| - |||||||'
end
Running with no arguments tests the example equation:
>>toothpicks.rb
||| x || + ||||| - |||||||
3*2+5-7
4 from 22 toothpicks
Note: syntax highlighting provided by this nice page.
Permalink
10.01.08
Posted in Uncategorized at 10:02 pm by JohnB
My aunt Barbara passed away yesterday. We were never close, but she was the last of her siblings to pass on, even though she was the oldest. I know that was hard on her. Now its finally her turn. May she find rest.
She played a pivotal role in my life, long before I was even conceived. After her mother, my grandmother, had passed away, Barb took it upon herself to put my mother through college. I doubt she ever understood this nuclear engineering thing my mother studied, but she supported it. And eventually my mother met another engineer and here I am today.
It wasn’t unexpected – she had been declining for a while – but I’m surprised at how choked up I am about her passing. Outspoken, disapproving, warm and alive – often in the same sentence.
I remember a message she once left on our answering machine. Her voice sounded so similar to my dead mother’s voice, with the same Pennsylvania accent, that it sent chills up my spine.
Thanks Barb, for all you’ve done and all you were – you’ll be missed – and in the end, what more is there, than to be missed?
Permalink
04.20.08
Posted in Uncategorized at 12:49 pm by JohnB
[aside: Why write an email that will be read by just one or two people when you can instead write a blog post that will be seen by... uh... one or two people?]
I was recently discussing the problem of tracing program execution through code that doesn’t exist. Sometimes, usually to understand and debug a program, you need to trace through it. If the code doesn’t exist, its hard to follow where it goes. Actually, the code exists, its just not in source control or otherwise easily searchable. Let me explain.
In Ruby, whose source code is a lot like its executable format, its trivial to write code that writes code – so you, the programmer, don’t have to. The Rails web framework uses this a lot, for creating whatever form of ‘find’ strikes your fancy: find_by_firstname, find_by_firstname_and_gender, find_by_pet_species_and_viciousness, etc. – as long as your database table has the appropriately named columns then it will find what you’re looking for with no work on your part! Actually, some work is required – you need to know that most any routine starting with ‘find’ is probably auto-generated by Rails and thus will be un-findable in the source tree.
A more complex example is the routing helpers – a shorthand way of saying “put a link here on this web page that will take the user to that web page over there. Until you realize that most any method ending in ‘path’ is a URL helper then you’ll be confused by code that references user_edit_path or formatted_pet_list_path or formatted_pet_species_list_path. The last one says that you want to get the path to a specially-formatted list of the various pet species represented by the system (e.g. “http://myPetSite.com/pet_species/list.csv”). Fairly clear once you know what it does, but fairly obtuse until you get to that point. And it is, trust me on this, shorter, more maintainable and more clear than the alternative (after, of course, you learn to read it).
So this difficulty in finding the source to “formatted_pet_species_list_path” (or something like that) started a discussion that eventually got around to the idea that Ruby and Rails uses code to write code.
“Its the 11th commandment”, said one, ” – thou shalt not write self-modifying code!”
Yes and no. Call me irrationally exuberant or say that I drank some sugary flavored colored water – but I don’t think its as cut-and-dried as it used to be. This commandment, when Moses brought it down from on high, was written for a compiled language, where the source was very very different from the executing code, and it actually did modify the code being executed. This is bad bad bad – its hard to understand, it leads to intractable bugs, it’ll make you go blind, melt glaciers and other bad stuff. No argument there.
But is this what Rails is doing with Ruby? I’d say its different. Instead of modifying existing code it is merely creating code that did not exist before. Its more akin to a pre-processor that could, just before compiling a program, generate all the permutations of finding database rows from columns X, Y and Z (and any number of others). Looked at this way, it appears to be similar to a C++ template – generic code that a programmer has written to simplify the writing of code that does similar things in similar ways. The main difference is that with Ruby there is no pre-processor – the routine gets created at the time it is first used.
The “template” in this case is a routine called method_missing that gets handed the name and arguments of any routine that doesn’t exist. It looks for a function name matching the form ‘find_by_X‘ where X makes sense for the database table in question (of course, if it doesn’t match the correct format then it just passes the name and arguments onward for some other routine to either make sense of or to burp up an error). Once the routine is created it is available to be called again, with none of the overhead incurred when it was first created. More importantly, it was created with no additional overhead on the part of the programmer – to write it, test it, debug it or modify it. This, in my opinion, is a huge advantage – but I’m a lazy developer who doesn’t want to write or debug any code that I don’t have to.
Kool-aid anyone?
Permalink
03.20.08
Posted in Uncategorized at 4:22 pm by JohnB
Pay By Touch To Shut Down All Biometric Services Immediately
Biometric authentication transactions to cease at 11:59:59PM March 19, 2008
SAN FRANCISCO – (March 19, 2008) – Solidus Networks, Inc., dba Pay By Touch, regretfully announced today that it will no longer process biometric transactions on behalf of its merchant customers and consumer membership base, as of 11:59:59PM March 19, 2008.
On December 14, 2007, Solidus Networks filed for U.S. bankruptcy protection under Chapter 11. As part of the company’s restructuring, it was determined that the enterprise could no longer support the biometric authentication and payment system as it currently exists, based on lack of funding and current market conditions.
Other non-biometric Solidus Networks business units will continue on their current business paths.
Solidus Networks extends its sincere gratitude to the shoppers, merchants, vendors, investors, partners, and employees who have been supporting the company’s vision since its first biometric payment transaction in 2002.
Permalink
02.27.08
Posted in Uncategorized, ruby at 12:54 am by JohnB
[Update 10/11/2009: I just found a better tool, bindata, to do what I'm describing in this post. It also lists, at the bottom of the page, many links to yet other implementations of binary data packing/unpacking. Worth checking out if this is what you need.]
[Update: presentation from the 4/15/2008 Ruby Meetup is now available here.]
I like reading code. Its like a novel and I want to read it cover-to-cover. Some, such as Why’s Camping framework, I struggle to comprehend. But most code that I read comes up slightly short. Like a novel with some mis-spellings, awkward phrasing or repeated analogies, I mentally mark it as “could be better”. And sometimes I really do sit down and write something better – maybe just for my own amusement but often for a useful purpose.
I recently had the experience of reading some code that parsed a variable-length binary data structure. This sort of thing comes up often when parsing a file format or communications protocol. Most of the code looks fairly similar because it does similar stuff: ignore one byte, read the next four as the length of the following junk, read two important bytes, ignore two more, read another four-byte length and skip past the following N bytes – ad nauseum.
I’ve written it in C, and it looks something like this (ignoring error conditions like getting to the end of the buffer):
ptr = &data; // start at the beginning of our data
ptr++; // skip junk we don't care about
UInt32 len = *(UInt32 *) ptr; // get the 4-byte length
len = ntohl(len); // convert from network byte ordering
ptr += sizeof(UInt32); // skip past the length we just read
ptr += len; // skip past the data we don't care about
UInt16 cost = *(UInt16 *)ptr; // read our important two bytes
cost = ntohs(cost); // convert to the correct byte ordering
In Ruby, this tends to be shorter due to the handy String.unpack() routine, which takes a concise format string to define how many bytes to read and what to do with them. “a3″ reads 3 bytes as a string, “N” reads 4 bytes in network order, “n” reads 2 bytes in network order, etc. The code above could be rewritten in Ruby like this:
array = data.unpack( "a1N")
len = array[1]
data = data[5..-1]
array = data.unpack( "a#{len}n" )
cost = array[1]
data = data[(len+2)..-1]
This code works fine, but its not much more readable than the C code. A first step would be do define a string.unpack!() routine, where the ‘!’ exclamation clues us in that it modifies the object we’re working with. In this case, the modification is to eat (discard) the data we just read. This shortens the code to:
array = data.unpack!( "a1N")
len = array[1]
array = data.unpack!("a#{len}n")
cost = array[1]
But again, this isn’t much more readable (in my opinion) than the C code. Additionally, it doesn’t help us understand the code much better in the case where our format string is “a3Nna5″ and we need to remember which item in ‘array’ corresponds to the ‘n’ in the string (in this case, it is array[2]). After a test iteration or two, what I finally hit upon was to encapsulate the behavior we want in a separare Unpacker class, that automatically eats the data it reads and stores the results in an internal Hash object, to map the name ‘len’ or ‘cost’ to the data. I also combined the format string and the resulting variable so we can clearly see the relationships. The result looks like this:
u = Unpacker.new(data)
u.u! "a1 => unused
N => len"
u.u! "a#{u.len} => unused
n => cost"
Now we can clearly see which values are ignored, which are given meaningful names, and how the format codes relate to the meaning of the data. Changing it to reflect a better understanding of the underlying data will be very easy. Note that the only reason its in two statements is to define a value for u.len before we use it – blocks of fixed-length data can be one statement.
The code to implement the Unpacker class is only about 30 lines of Ruby – including the string.unpack!() routine that can be reused separately.
class String
def unpack! format
array = self.unpack(format+"a*")
self.replace array.pop
return array
end
end
class Unpacker < Hash
attr_reader :data
def initialize string
@data = string
super
end
def u! format
format.gsub(/\s*=>\s*/,'=>').strip.split(/\s+/).each do |segment|
src,dst = segment.split(/=>/)
self[dst] = @data.unpack!("#{src}")[0]
end
end
def method_missing(meth,*args)
meth = meth.id2name
if meth =~ /=$/
self[meth[0..-2]] = (args.length<2 ? args[0] : args)
else
self[meth]
end
end
end
Update: An even cleaner and shorter way would be to implement a DSL as a module so the code above could look like this:
a 1, :unused
N :len
a :len, :unused
n :cost
(and yes, this is valid Ruby code)
Permalink
01.29.08
Posted in Uncategorized at 12:53 pm by JohnB
The Rails Way is a Ruby on Rails reference book that I bought on Josh Susser’s recommendation. I’ve actually, to my family’s dismay, been reading the darn thing instead of just referring to it like one would a, well, reference book. A lot of Rails’isms that I had a vague idea about I now understand with much more clarity. It will definitely come in handy soon.
Permalink
« Previous entries Next Page » Next Page »