Ruby Elegance Nuances

July 28th, 2007

I’ve recently been doing some very fun, elegant, and exciting things with Ruby at work and in my personal time. Although I have to admit that I learned Ruby because of the amount of hype Rails got on Digg back in the day, I am proud and overjoyed to consider myself a Ruby developer now.

Even though these bits of knowledge are obvious to advanced Ruby developers, I think there is value in recognizing these in context and expanding upon them. Below is my version of a summary of a presentation, courtesy Glenn Vanderburg.

  • Punctuation gets in the way for short lines, but makes long lines more readable (this applies to the use of single versus multi-line blocks too.)
  • The minimal syntax to open and close blocks makes their use feel natural. This naturalness is powerful, because blocks seem to allow powerful abstractions to be developed without much thought.
  • Sigils are used for scope (@@, @, CAPS, $, etc) rather than typing (like in Perl.) This use of sigils helps, because the most common scopes (classes and local variables) do not have them. However, they provide an effective language feature that eliminates an annoying standard like using an underscore before instance variables (Java.)
  • There are usually two (or possibly more) ways of doing something, allowing for the coded implementation to fit the context.
  • Mixins/modules cannot create instance variables (because they cannot be instantiated), but they can reference them (because the instance variables spring into life inside of the object the module is mixed into.)
  • The Enumerable module, along with blocks, allows Ruby to apply some functional programming techniques.
  • Operator overloading.
  • “Three-quals” (===) allows for beautiful case statements by defining the concept of a match.
  • “Open classes” allow for nice common interfaces and DSL’s to be defined when patterns are discovered (ie: Rails’ blank? method on NilClass and String.)
  • Classes being defined can execute code on themselves to define themselves further because they are objects.
  • Although classes are just object instances, creating them still uses a declaration (unlike Smalltalk) and feels natural.

As a closing note, Ruby makes trade-offs in its design like any other language. These range from dynamic typing (which makes tool creation harder), to the “Principle of Least Surprise” (which make consistency harder), to many other things.

Glenn points out, and I agree, that Ruby seems to strike all the right balances (big ups to Matz.) However, this is a personal preference and we should respect the languages choices of others as a matter of taste… there’s room for everyone =)


BarCampOrlando and why you should be there

July 26th, 2007

BarCampOrlando is an opportunity for the Central Florida tech crowd to finally come together.

At BarCamp people from diverse groups, comprising of developers, designers, bloggers, and people that just plain love the web, can get together to share information, talk, and have a good time. It’s events like this that spur discussion to shape the future of the net, which makes BarCamp one of the most exciting conferences to attend. Best of all, it’s free and open discussion is encouraged, if not required!

My first BarCamp was in Jacksonville, and that’s where realized how serious blogging is thanks to presentations by Josh Hallet and Joey Marchy. I want to reiterate that this is just as much about ideas and discussion than presentations on specific technologies.

We have tons of great local user groups here in Orlando, for Ruby, Java, .NET, PHP, Adobe, Linux, Ubuntu, Creatives, and probably some things I’m forgetting. Additionally, the similar Refresh06 conference was a blast. I think we have an extraordinary opportunity to create an amazing event, so show your support by blogging about it and putting badges on your sites! Without blogging and your help, events like this don’t happen.

I’ll end this with a video explaining BarCamp and its history… see you there!


The Best Thing About Rails is the Community

May 22nd, 2007

Wow… RailsConf07 was absolutely amazing. The interesting thing is that I am not talking about the presentations, although they were top-notch. Instead, the Rails community is what made this conference so… heart-touching, even.

Rails is Love

Chad Fowler directly announced that he picked the keynotes and sessions as his personal mold for what we should be thinking about. To this end, I thank him deeply for his choices.

While there, I came to recognize the Rails community in a new light. I don’t think I could have understood this unless actually being there either (as cliche as it sounds.) Our community stereotype as elitist, arrogant developers did not show its face.

I hope that the community stays like this, and that everyone has a chance to go to at least one conference like this.

RailsConf Crowd

All of the attendees were teeming with positive energy and friendly. The diversity of people was great, including people from different states and countries, and people in suits (maybe one or two) to people dressed like hippies and supermodels (you can guess here…). Breakfast and lunch was served in an enormous open room full of tables and seats. But, everyone there was inviting, and immediately started conversations naturally. This was true of people in the hallways too, and included speakers and even DHH himself. Chad even told us to introduce ourselves to somebody new during one of the breaks, although this was happening naturally anyway.

The ad-hoc organization of lightning talks and RejectConf led to some of the more interesting demonstrations. Similarly, post-session conversations with speakers that led to group discussions, which then led to BoFs ,fully capture the spirit of the conference. The BoFs in general stood on their own as hallmarks of open, informative discussion… not just presentation.

Each day opened with one of the hilarious Mac VS PC spoofs by Gregg and Jason. Keynotes were accompanied by accordion and ukulele (courtesy Joey deVilla and Chad Fowler, respectively), and even a spoof serenade (to the tune of ) to our beloved creator DHH.

DHH stressed the importance of plugins both as a community way to share common functionality and testing-grounds for Rails core-features.Avi Bryant (creator of Seaside) gave a jaw dropping and eye opening talk exposing similarities between Smalltalk and Ruby. He strongly recommending stealing from Smalltalk’s maturity in IDE and especially VM implementations. Specifically, a highly distributed virtually machine network by a local Portland company called Gem
Stone. Supposedly, converting this to Ruby would not be overly difficult. But, it would make Ruby run as fast as Smalltalk, which is approximately 10 times faster than Python (and 3 to 4 times slower than Java).
Besides that, Avi stressed his belief that state is good, and that like wine, “objects get better with time”. In his opinion, that is the path that Ruby should follow, effectively becoming Smalltalk =p. This was not a unique opinion though, as Chad seemed to support Smalltalk. As a bit of comic relief, Avi introduced himself as “from the future” and had a hilarious metaphor from 2001: A Space Odyssey (we are the apes…)Smalltalk VS Ruby

Dave Thomas even attacked the browser with its transition from form based requests to AJAX as a medium for the web. He compared it to a similar evolution of ideas that happened in early computers, and warned to not get stuck in a loop of repeating history. Dave went on to get the audience to come up with more examples of this “cargo cult”, evoking responses as extreme as REST, Mongrel, and Rails itself. Although, he did boldly reject the idea that conferences could ever be a cargo cult =p.

Dave Thomas had a donation-based all day tutorial that raised over $12,000 by itself. After witnessing this accomplishment, Chad Fowler (and many other speakers that followed) encouraged the audience to continue donating throughout the conference. Eventually, a challenge to make this the standard for tech conferences was put in place. It is still going on, so anyone is still encouraged to donate.

As a whole, RailsConf07 was one of the most spectacular things I’ve ever experienced. There were many other interesting things happening, such as the effort to understand how “the enterprise” will fit into Rails, and various thought provoking sessions. But, by far the Rails community showed that it is concerned, responsible, and innovative, and I am extremely proud to be a part of it. If you were there, please take the time to blog your version of the event (and encourage others to do so as well) so the rest of the community can understand what is happening and share the excitement.


Company Blogging with Yahoo Pipes

May 2nd, 2007

I’ve blogged about my gripes with multiple blogs in the past, concluding with an informal protest to only use one.

A specific instance of the problem that I mentioned was having a personal blog in addition to blogging under a company. Well, it turns out that you can have the best of both worlds with little to no effort.

Yahoo Pipes takes advantage of the fact that so many sites and services distribute some form of feed syndication. It allows you interact with feeds from different sources in an absolutely incredible amount of ways. Through graphically playing “connect-the-dots” with various modules and/or filters, you gain an unprecedented amount of easily accessible power to create mashup feeds. Of course you can export these to RSS (or JSON), but they even offer email or mobile exports!… and publicly sharing the “source” file so others can copy your project where you left off and continue it!

Okay enough fanaticism (although it is completed warranted), and lets get back to the topic. Grabbing feeds from separate employee blogs to combine on a company blog has always been an option. However, Pipes makes this so easy that you have no excuse to not use it.

Here’s a simple proof-of-concept… totally usable though. It uses the “fetch feed” module that you pass an RSS url to. Then, the sort module is used to sort for the most recent entries by date published, and even by new blog comments. And then… nope that’s it, just connect to the “pipe output” module and you’re done!
(Note that I only included my blog here in the RSS, but you can add as many as you want to get mixed in by just hitting the “plus” icons):
http://pipes.yahoo.com/pipes/pipe.info?_id=3PcgeID42xGTQKjsy6ky6g

Another cool thing you could do is make a filter to only grab posts out of the feeds to put on the company blog that have been posted under a certain category… sweet!


Cool Minimalistic Site Design

April 22nd, 2007

I just recently got Eric Allam to give me his blog URL, and I love his site design at last100meters. In trying to come up with a design for the project I’m working on, my aim is to strike a balance between minimalism and a flashy web 2.0 look, while still being unique and useful UI-wise.

Eric’s site definitely appeals to the minimalist in me though, and is the first that I’ve liked a lot since Google’s homepage. The posts/titles are big, prominent, and pure text, spanning the whole left pane down the page. Three links, the RSS feed, Google Reader shares, and Twitter feed are the only things with images, even then being tiny. White space is everywhere, and his contact information is immediately available. I love it!

In contrast, you can tell from my blog design that I like to just take everything I have ever touched and throw it all together… now that’s art =p


Multiple Blogs Are Not For Me

April 22nd, 2007

Whether or not to commit to other blogs has been bothering me for a while now. I started with this blog… When I had a job at New Media (seems their site is down right now, how ironic =), I was assigned another blogging account. It started there, I had to make decisions between what I considered better suited for my personal blog or my work blog. This type of thing is especially hard for a web developer, as the divide between personal and work related activities is hazy at best.

I never ended up using that account, and didn’t post here either because of never knowing where to post. Later, I was offered to blog Ruby and Rails specific stuff with the guys at Rails Envy. Although they are both great dudes whom I respect, for some reason I couldn’t get myself to say yes.

We were also thinking about how it might be cool to have a Rails/Ruby dev blog for PayPerPost. You know, get a bunch of developers all blogging in the same place about cool stuff we come up with daily. Yet again, that would mean splitting content between this blog and another (can you see what I mean about a developers work/play divide being slim?)

Just today I was asked whether I wanted to be apart of a neat idea for a Ruby blog with my friend/coworker Eric

As I hinted in my last post and my latest rockstartup confessional (doesn’t seem to be online yet, wait for it!), I am spending my spare time working on an awesome site to help people blog more (solving my own problems here, heh) and pump quality content into the web via microformats. Of course, I have to be cool and make a company blog for that, right?

Nope, I say all this worry and frustration ends here. I am making the decision now to devote all my time and effort blogging to this site. No more distractions, no more stress, and much more learning and content. I’m not sure if I’m the only one running into this problem, but if you felt anything like dejavu reading this I invite you to do the same.


Beast Source Code Snippet

April 18th, 2007

I downloaded and browsed the source code for the Beast forum today. Because it’s REST’ful, open source, and developed by Rick Olson, I recommend any Rails developer to look at it. What makes it even better is that a goal of Beast is to “stay around 500 lines”, meaning the app doesn’t take much time to figure out and can be studied leisurely.

Anything you define in YourController#rescue_action will be run when an error is raised, so check out what I found in Beast:

def rescue_action(exception)
exception.is_a?(ActiveRecord::RecordInvalid) ? render_invalid_record(exception.record) : super
end

def render_invalid_record(record)
render :action => (record.new_record? ? ‘new’ : ‘edit’)
end

As you can see, now any errors that are invalid AR objects will call a special method. This method render’s new or edit depending on whether the record exists yet or not.

So…rather than doing this in our controllers:

def create
@record = Record.new(params[:record])
if @record.save
flash[:notice] = ‘Saved successfully’
redirect_to :action => ‘index’
else
render :action => ‘new’
end
end

We simply do this:

def create
@record = Record.new(params[:record])
@record.save! and flash[:notice]=(’Saved successfully’) and redirect_to(:action => ‘index’)
end

The magic is in the save! method, as the “bang” raises errors instead of returning false when validation fails, not bad!

On another note, I’ve been spending a lot of my time recently on reading books about and developing a social site… As you know, my ambition to blog a lot since the creation of larrytheliquid.com has quickly diminished. I’m making this app to change that for myself and all bloggers… but that’s enough for now =)


Validating Positive With Infinity

March 18th, 2007

Ruby has a nifty feature to be able to use a special constant that represents infinity, accessible as follows:

irb(main):001:0> 1.0/0
=> Infinity

Recently, I needed to validate that a field in my Rails model would be positive. While there are countless ways to do this, I was very happy to find a fun and practical use for infinity.

validates_inclusion_of :some_attribute, :within => 1..1.0/0, :allow_nil => true, :message => 'should be positive'

The attribute is validated to be within positive one (inclusive) and positive infinity (technically exclusive.) This is done with Rails’ validates_inclusion_of for numeric data, Ruby’s range operator .., and the infinity constant 1.0/0.

Huzzah!


Portable and Organized Bookmarks

November 2nd, 2006

Across desktops, laptops, work computers, etc managing your bookmarks can become a pain. I used to use a Firefox plugin called “Bookmarks Synchronizer”:https://addons.mozilla.org/firefox/14/ and a free FTP service. But, having to download bookmarks on startup and upload them when they change got to be to annoying.

Eventually I stopped using it and got back into disorganized and multiple computer bookmark hell. This wasn’t good so I quickly sought a solution, and started using “del.icio.us”:http://del.icio.us/ . As it is too tedious to use the web interface to add bookmarks, the “Delicious plugin”:http://del.icio.us/help/firefox/extension is a must have, allowing you to go to your bookmarks and create a new bookmark from buttons on Firefox.

The nice thing is that throwing a bunch of tags onto something organizes your bookmarks for you. As you type, popular suggestions and bookmarks you’ve used are displayed to avoid tagging some things “rails” and others “rubyonrails”. Another nice benefit is that the popup to bookmark something also contains a description field, making it more convenient to describe the value of the bookmark instead of only naming like you do with traditional bookmarks (not that you can’t add descriptions to them, it’s just less convenient.)

I still use Firefox’s bookmarks toolbar for frequent sites that I use access like Gmail, Rails and Ruby API’s, etc. But, which of these are used usually varies computer-to-computer because they are situational (work, personal, etc), making the need for them to be portable less. Plus, the number of these are limited so you can always add them quickly.

In any case, another great tip is to use “Firefox Live Bookmarks”:http://johnbokma.com/firefox/rss-and-live-bookmarks.html to grab the feeds related to specific tags or delicious groups, and add them to your bookmarks toolbar. You could even make a “bookmarkstoolbar” tag that you subscribe to as a live bookmark, making the whole thing even more portable!


Simple Flash SEO

November 1st, 2006

100% Flash websites have the bad reputation of being associated with spam, not maintainable, unintuitive, and not search engine accessible. While those first few are myths to be busted elsewhere, this article will deal with the last issue.

Remember the old days of frames within sites? Now don’t try to cringe too much, but remember when not all browsers supported them? Well, there is an important parallel to draw here between framed sites and Flash sites. Both are seen as unconventional and have interesting problems dealing with information support among different mediums.

With framed sites, getting markup information to a user whose browser does not support frames is an obvious issue. The solution was the @

@ tag. In this tag, a webmaster of a framed site can include the complete or partially complete version of a site to people without frame support.</p> <p>”Yup, old news, so how does this apply to Flash?” Well, in a Flash based website you are able to get all the information to a user through the ubiquitous Flash player. Wait a second, what if the end user DOESN’T have the Flash player. Hm…now what if that end user is the Googlebot crawler? You guessed it, @<noframes>@ tag to the rescue solving essentially the same problem as described above.</p> <p>This method lets you enter info into a web page that gets indexed by Google, has fairly nice semantic meaning (noframes == noflash), and without resorting to the kind of trickery that results in being banned by Google (ie: invisible text with the same color as the background.) Better yet, it doesn’t affect your layout at all…unless of course the end user doesn’t have frames =p.</p> <p>Example:</p> <p><code><br /> <html><br /> <head></head><br /> <body><br /> <noframes>This info gets indexed by Google, even though the only thing in this web page is my Flash application!