Archive for the ‘Resources’ Category

Using VIM In Your Workflow

Tuesday, August 3rd, 2010

After a bit of a wait, my buddy Curtis McHale has posted the article I wrote for his site, yourworkflow.ca.  You can check out the Using Vim As A Text Editor article over on the site.  It’s not just about Vim, but more the day to day coding workflow that I was using, so it includes things like Firefox plugins, Unix utilities, etc.

So head over and check out the workflow site and read not only my fine fine article, but also the other news and podcasts he’s got going on over there.

Rails Tutorial Updated To Rails 3.0

Wednesday, July 28th, 2010

Huge congrats go out to Michael Hartl for upgrading his awesome RailsTutorial.org for Rails 3.0.  Now you have the choice of reading the 2.x version or the 3.0 version of the site.  Many thanks go to Michael for his hard work and huge contribution to the Ruby and Rails community!

Is A Site For Ruby Idioms Needed? [Update: Yes!]

Monday, May 3rd, 2010

I’ve noticed lately that there is definitely a “Ruby way” to write Ruby code. When I first read Effective Perl Programming years and years ago I went from writing code that looks like this (note that I know the “FILE” is wrong, but the wordpress auto-syntax highlighter thingy doesn’t seem to deal well with the correct bracketed syntax):

while (my $line = FILE ) {
    if( $line =~ /foo/ ) {
		print "$line";
	}
}

(which isn’t all that un-perl-y to begin with, but that’s 15 years of perl in my brain stopping me from writing really un-idiomatic code) to far more idiomatic:

while (FILE) {
	print if /foo/
}

The point being that there are certain conventions and ways that your programming style will adapt to the given language. The following loop

for (i = 0; i < 10; i++) { }

Is perfectly natural in C, but if you saw it in perl or ruby, while it might be perfectly valid, it would look way out of place in either language, and you’d get funny looks if you presented it to a code review.

In ruby some of these would be to not do this:

t.gsub!(/(http|https)biglonguglyandhardlyworksregex/, "<a href="\">\1</a>")

But instead do this:

    URI.extract(t, %w[ http https ftp ]).each do |url|
      t.gsub!(url, "<a href="\">#{url}</a>")
    end

Or maybe instead of this

sum = 0
list = [1,2,3]
for i in list do
  sum += i
end
# sum is now 6

you use the must more awesomeer (yes it’s a word)

list = [1,2,3]
sum = list.inject(0) { |s,v| s + v } # sum =&gt; 6

Of course, you can write bad code in almost any language pretty easily (note how I’m not making a php/python/whitespace/brainfuck joke here!)

So my question is this.  Is there some grand collection of these Ruby idioms?  Is there a need for them?  Would a fusion of StackOverflow and Refactor My Code be a useful collection to have somewhere?  Or are the resources out there (which are a bit scattered) good enough?

So far I’ve found:

Alternatively there are a lot of places where you can read other’s code to learn idiomatic Ruby by osmosis.  Github, RefactorMyCode, the popular Gems in the community, The Ruby Quiz are all great resources for this.

My vision is a melding of StackOverflow (maybe using their new StackExchange community software?) and Refactor My Code where you can search for an idiom or programming operation based on code, Class, or tag, vote, and comment or submit a different version.  Sort of like a Perl Golf contest except instead of the fewest keystrokes being the goal it’s the cleanest/nicest/most effective way of doing the operation.

So what do you think, would this be useful to work on with the Ruby community, or is there enough information out there already that is google-able enough?  Everyone will also have their own way of doing things, but Ruby is an opinionated language (or is that only Rails?) so maybe there is One (or two) “correct” ways to do things.

Your thoughts appreciated.

Update – So a bit of discussion here, and lots of great comments on proggit show me that this idea does deserve a go of it.  I’ve registered ruby-idioms.com (pointed here for now) and hope to have something up in the next couple of weeks, and will take a few beta tests to have a run at it.  Keep an eye here for any news by subscribing to the RSS or following me on Twitter.  Thanks everyone!

Favorite Ruby and Rails Podcasts

Tuesday, April 27th, 2010

Podcasts have been a great help to get some great information about Ruby and Rails, as well as to discover and connect with more of the community.  I figured I’d list a few that I listen to, and would appreciate folks passing back any good ones I might be missing.

Some will be old hat to the ruby folks, but there might be some new ones that could peak pique your interest.  If you’re a newbie to Rails like me, I suggest you add these to your podcast watcher of choice immediately!  Most of these will fall into either the “news of the week”, “interviews”, “howto” or “training” categories.  I’ll categorize them for you a bit so you can pick and choose if you prefer one or the other.  There are a few that I’ve picked out that aren’t strictly Rails or Ruby oriented, but they still get prime location in my weekly podcast listening.

  • Coderpath [itunes] (audio,interviews) @milesforrest and @curtismchale
    Ok, so even though this is a bit of shameless not-quite-my-own self promotion, and Miles and Curtis are buddies of mine, I do have to honestly recommend the Coderpath podcast.  They do interviews of some of the big names in the Rails community such as DHH, Ryan Bates, Wayne Seguin and others, and ask a lot of good questions about some of the “how” of the community, the sort of questions someone like me might ask (and I do).  Also keep an eye on Miles’ twitter feed as he’ll send out where you can submit questions for the podcast.
  • RailsCasts [itunes] (video, training) @rbates
    Ryan Bates’ great podcast is a staple of the Rails community, and he gives short, bite sized chunks of information on different aspects of rails programming.  One week it might be how to do something like nested routes, another will be a series of Rails 3 howtos.  Awesome stuff.  Personally I’d recommend watching these and not deleting them, so you can go back for further reference later on.
  • Rails Coach Teach Me To Code Podcast [itunes] (audio, interviews, training and howto) @charlesmaxwood Note: Updated link, RailsCoach is now Teach Me To Code Podcast
    This is a recent discovery of mine, and Charles does a great job in intermixing interviews and talking about how to do various issues, such as finding a mentor or how to take the best advantage of your first (Rails|Ruby)Conf.  My favourite part of this podcast is that he seems to be (and no insult intended) about on my level, or a bit above, so a lot of the questions he asks would be pretty much exactly what I’d want to know in the same situation.
  • VimCasts [itunes] (video, training) @nelstrom
    Not purely a Ruby or Rails podcast, but vim is a skill that every programmer should know, and Drew does a great job in giving both a newbie friendly, and old-hat educating, series of screencasts.  If it says anything to you, I’ve been using vi and vim since around 1995 and was still learning things from the first podcast.  Great stuff in easy to digest chunks like RailsCasts.
  • RailsLab Scaling Rails [itunes] (video, training) @greggpollack
    This is a 21 part series that isn’t being updated anymore, but is a must-watch for anyone who wants to know a bit more about Scaling Rails.  This isn’t going to be applicable to 90% of what 90% of the audience does, but is still invaluable information in giving you the background information to know why certain decisions you make in technology can have big (or small) impacts on scaling down the road.
  • Your Workflow [itunes] (audio, interviews) @curtismchale
    Ok this is another bit of not-quite-my-own self promotion, as Curtis is a buddy of mine from the Fraser Valley Ruby Brigade (FV.rb).  This podcast addresses a niche that isn’t really addressed in other places, and takes that tack of “how do you get your job done” and talks about the workflow that people have.  Curtis is a designer and so far his interview has focused on wordpress development, but the site and podcast is all about people’s workflow, and you’re sure to find something new.
  • Ruby Pulse [itunes] (video, training) aaalex
    Ruby Pulse is to Ruby what RailsCasts is to Rails.  Alex takes a small chunk of code, a gem, or technology concept and works with it in a 5-10 minute podcast.  Ruby Pulse is unique is that it’s done in one go, with no editing, so as the intro says, sometimes unexpected things will happen.  More often than not, you’ll get a nice introduction to a new and interesting gem or two.
  • Ruby 5 [itunes] (audio, news) Envy Labs
    Ruby5 is the bite size chunk news show, and gives you the Ruby and Rails news of the week in a five minute podcast a couple of times a week.  Ruby5 was split off of Rails Envy for people who wanted their news a bit more compressed and less “chatty”.  Great way to hear what’s new and interesting and keep yourself right on the edge.
  • The Ruby Show [i], The Dev Show [i] and other good stuff from 5by5 (audio, news, interviews)
    Those familiar with Rails Envy will recognize these as cut from that tree.  5by5 has a very good series of podcasts with various names in the Rails and open source community, and has a great mix of news and interview shows.  The Ruby Show and the Dev Show are the ones that are on my podcast weekly, but I’ve also recently discovered The Pipeline, which is an interview show with innovators and newsmakers, and it deserves a listen as well.
  • Stack Overflow Podcast [itunes] (audio, news, howto and interviews) @spolsky and @jeffatwood
    Another not-really Rails oriented, but Joel Spolsky is a name that should be familiar to anyone doing development, as will StackOverflow.com.  You’ll also recognize Jeff Atwood from his great codinghorror.com site.  The guys get into some of the programming practices they follow, business, and interviewing people in their circle.  It’s a really interesting look into the development and business of the StackOverflow community.  Just ignore the whole “.NET” thing :)
  • The Changelog [itunes] (audio, interview) @adamstac and @pengwynn
    These are the guys behind tail.thechangelog.com and the github.com/explore pages, and give a great weekly interview with someone in the open source community, and really get into it from the philosophical point of view.  Being able to get a dose of what’s going on in the Open Source community is definitely an asset and their show (and the opening and closing music) is my Saturday must-have-on-while-driving podcast.
  • 37signals Podcast [itunes] (interview, howto) @dhh and @jasonfried
    If you ask for a Rails oriented podcast, how can you not include one from the company that started it all.  This one features DHH and Jason Fried sitting down and talking about all things community, rails and 37signals related.  Sometimes they are just chilling talking about Rails, sometimes it’s interviewing the sysadmin team, and somtimes it’s chatting about how the business side of the uhm, business works.  DHH is never one to hide his opinion about things, so it’s always a great and educational listen.

So there you go, hope you enjoy the list and find something new.  Remember though that you don’t want to get caught up in listening to podcasts about coding, and forget to do the coding yourself.  These will definitely give you a boost of inspiration, training, or just a weekly re-connection with the community.  Also please comment if I’ve missed any good ones!

Personal Projects For Learning

Tuesday, April 20th, 2010

One of the biggest struggles I’ve had is when learning something starts getting “stale”.  It’s a bit like a relationship, you go from the fun and new “getting to know you” stage and eventually plateau into the “how was your day dear?” “grunt” “that’s nice dear” stage.

I plateaued with Perl a while back I think, I definitely didn’t learn anything (I’m still picking things up from the Perl Best Practices book I have), but I found the set of functionality that works for me for 99% of what I need, and that I can manhandle into working for the remaining 1%.  It’s not a bad thing, but it’s not a good one either, because I don’t have the drive to new things with it.

I don’t think I’m at that stage with Ruby yet, but I was thinking today on the treadmill about how to avoid that and how to keep things fresh and interesting.  I figure that just like in photography, having a Personal Project is a great way to keep things fresh and interesting.  I came up with a few ideas.

  • A Code Sprint – They use the term “sprint” at my day job to mean a team that is concentrating on getting a new chunk of functionality or application out and done with minimul distraction.  I figure that I can do the same thing, set up a new idea for a site or project and work on nothing but that in my personal coding time (never at the day job of course).  This means when you’re done the initial fun stuff, database setup, object relationships, etc, you keep on going through the functionality, contact forms, logo, design, etc until it’s done.  It doesn’t have to be huge or take a month of your time to do, just make sure you don’t sit down and say “you know, adding feature XYZ… I’m not feeling it, maybe I’ll just catch up on Lost some more” (this is my personal battle right now!)
  • Learn a Class or Set of Functions Really, Really Well – Sure it’ll just be one class (Hash for example), or one set of functions (grep and array searching for example), and there’s probably no way to learn everything, but imagine the power to know that you have Hash under your belt, and that you know six ways till Sunday how to search arrays and similar objects, and the best practices and best idioms for it?  And that next week you can choose another class or set of functions to learn.
  • Answer X Questions A Day – The best way to learn I’ve found is to teach others.  Even if you just know a little bit you can probably bumble your way into some knowledge, and in that way you both learn.  Why not take on a question a day (or 5, or 10) on a forum like the StackOverflow ruby questions, or ruby reddit, or the ruby forums, and fully and completely, to the best of your ability, answer them.  Even if you spend the whole day researching and come up with the same answer that 10 other people have already given, you end up with a greater understanding, and you get to help other people.
  • Duplicate A Plugin – Sure you can just install Restful Authentication, or CanCan for Authorization, or activerecord symbolize, but why not learn what they are actually doing, and create that same core functionality “by hand”.  No need to do every nuance of what the plugins do, but creating an auth(entication|orization) system by hand is a great challenge so you know what’s going on under the hood next time you rely on just running “script/plugin install….”

Those are some good suggestions, what do you other ruby and rails hackers do to learn more or keep yourself sharp?

At Home With Nested Routes and Resources

Tuesday, April 6th, 2010

When I first heard about nested routes, as with most things in Rails, I completely ignored them, discounting them as something I’d get to learning when I needed to, but right now they were yet another one of those things that I didn’t quite grok as I didn’t have the other base knowledge of the Rails system to know why I needed them. To be fair, I’m not that much closer now, and started to realize the the other day I didn’t really quite understand REST and having a RESTful site.

But when I started looking at how my site was structured, and how things were really encapsulated in each other, such as clubs “having” fields, and fields “having” games, I started to understand the need. Actually it was more doing the scaffolding of the site and seeing that I had to set field in a dropdown each time I created a game, or the club each time I created a field. It didn’t make sense and it seemed like a stupid way to do it.

Back in the old days in perl, I’d pass the ID of the main object when I created a sub-object. So if I had a club editing page with a ‘create new field’ button on it, that button would pass a hidden “club_id=$id” when it was pressed.

Rails comes with a nicer way of doing it. It allows you to nest related resources. Probably the best place to start is with Ryan Bates excellent Nested Resources screencast, or Adam’s Nested Resources in Rails 2 page.

For this I set up the following in my routes.rb as such:

map.resources :clubs, :has_many => :areas, :shallow => true

And then running “rake routes” will show you what the routes will actually be:

     club_areas GET    /clubs/:club_id/areas(.:format)            {:action=>"index", :controller=>"areas"}
                POST   /clubs/:club_id/areas(.:format)            {:action=>"create", :controller=>"areas"}
  new_club_area GET    /clubs/:club_id/areas/new(.:format)        {:action=>"new", :controller=>"areas"}
          clubs GET    /clubs(.:format)                           {:action=>"index", :controller=>"clubs"}
                POST   /clubs(.:format)                           {:action=>"create", :controller=>"clubs"}
       new_club GET    /clubs/new(.:format)                       {:action=>"new", :controller=>"clubs"}
      edit_club GET    /clubs/:id/edit(.:format)                  {:action=>"edit", :controller=>"clubs"}
           club GET    /clubs/:id(.:format)                       {:action=>"show", :controller=>"clubs"}
                PUT    /clubs/:id(.:format)                       {:action=>"update", :controller=>"clubs"}
                DELETE /clubs/:id(.:format)                       {:action=>"destroy", :controller=>"clubs"}

The biggest, biggest thing to notice here is the “:id” and “:club_id” in some of the routes. This tripped me up a few times.

Nested routes let you automagically pass “club_id” (in this case) to paths. So in your controller or views you can now use the variable from the first column. So you can do a

redirect_to clubs_path
# instead of:
redirect_to :controller => "clubs", :action => "index"

for example, and it would go to the /clubs/index path and do the right thing. It also lets you do things like:

redirect_to new_club_area(@club)
# sends you to /clubs/1/areas/new

Note that you have to pass the club before this will work. This took me forever to figure out, cause sometimes it worked and sometimes it didn’t. What I found was looking for either :id or :[object]_id in the routes path would tell you both if you have to pass the route the object or not, but also if you’re processing the path in the controller, that :club_id will be passed auto-magically to it. So for the “/clubs/:club_id/areas/new” route above, you’d process it in your areas_controller, and in the ‘new’ def, you’d automatically get club_id for free. IE:

# in app/controllers/area_controller.rb, possibly via new_club_area_path(@club)
def new
   @club = Club.find(params[:club_id])
   @area = @club.areas.new
end

You know that club_id is being passed because the rake_route for that path has “club_id” in it: “/clubs/:club_id/areas/new”.

Next up, finishing converting as much of the rest of my code from the controller/action syntax for redirecting and rendering to a more “restful” setup.

Update: There is a cool sounding TextMate bundle to help you with some of the brain games needed to understand nested resources.  Hat tip to The Ruby Show.