Monday, May 24, 2010

Moving to jackdempsey.me

A final post here at blogger: I'm moving my efforts over to http://jackdempsey.me so if you're interested, please update accordingly. Thanks!

Wednesday, February 3, 2010

notes from yet another rails 3 upgrade

These tips and tricks are valid as of today, February 3rd, 2010. Who knows what next week will bring, but for now, I thought compiling some of the techniques and hacks I've seen might help others.

Rails Related

If you see a "no such file to require: rails/all" you probably don't have Rails installed correctly. I was basing my upgrade on the great screencast produced by Topfunky over at peepcode.com, and his method didn't work for me. To get things running I added this to my Gemfile:

path "/Users/jack/git/rails", :glob => "{*/,}*.gemspec"
gem "rails", "3.0.pre"
gem "rails", "3.0.0.beta" # Just changed today it seems


Obviously, change the path to wherever you have rails checked out locally.

HAML

If you see something like this: Missing template articles/index with {:formats=>[:html]} in view path

you probably need to install haml.


$ haml --rails .


and then you'll likely see an error about html_safe!

Geoff has a nice little idea going with a config/initializers/rails3_hacks.rb file which I've also done. You can drop this in and get by the errors for now:

projects/app master > cat config/initializers/rails3_hacks.rb
class String
def html_safe!() self end
def html_safe?() true end
def safe_concat(value) value+self end
end


WillPaginate

Looks like Mislav and other's have been doing a lot of work in upgrading the plugin. If you see a "no paginate method for Class...." error, try cloning the source down locally, check out the rails 3 branch,build and install that gem and then update your Gemfile:

...
gem 'will_paginate', '3.0.pre'
...

It also seemed that WillPaginate wasn't being required into my app. For now I've created a config/initializers/requires.rb file and dropped in random requires for attention later. Putting a "require 'will_paginate'" in there fixed that up nicely.

Update:
Seems something's broken now. I'm getting this error:

uninitialized constant ActiveRecord::Calculations::CALCULATIONS_OPTIONS

Seems WP relies on that constant without checking for its existence, and it looks like its gone now. Easy enough to quick patch it if you need to get by it, but I imagine it'll be fixed in the rails3 branch soon enough.
Assorted Issues

HTMLEntities doesn't seem to be working, so I commented that out for now. For a great beginning list of what gems should be working, take a look here: http://wiki.rubyonrails.org/rails/version3/plugins_and_gems

Devise: not ready for rails 3 yet. I think they're trying to see what routing changes will settle and how that will affect their code. I'm going to see if I can pop over into that and help out a bit as I'd love to keep using it in this app.

new_rails_defaults.rb: I had an error from this file and realized it's old anyway, so git rm and it's gone.

Passenger

I also overhauled a bunch of other ruby related items on my boxes recently, and one of those items was passenger. After updating to 2.2.9, reinstalling the module, changing RailsEnv to RackEnv, things are mostly good. Remember to point to the right ruby if you're using rvm. I also deleted the config.ru file per their recommendation: "Smart spawning (the mechanism with which REE’s 33% memory reduction is implemented) is *not* supported for Rack apps. This means that if you want to utilize smart spawning with Rails 3, then you should remove your config.ru file."

I think that's it for now. I imagine I'll update this page as I deal with new issues. Pretty excited about the new changes and starting to actually make use of them.

Update

Previous notes were from my macbook pro. Now that I'm back on the iMac I'm finding other issues. Latest is:

/Users/jack/git/rails/activesupport/lib/active_support/dependencies.rb:167:in `require': no such file to load -- rails/commands/rails (LoadError)

I'm actually not sure if that was something I did as it looks like its definitely just "require 'rails/commands'" in the latest source, but either way, if you're seeing that, just take off the second rails in that line. Correct line should be:

require 'rails/commands'

at the end of your script/rails.

Bundler

Somehow just found myself facing this:

projects/app master > bundle install
/Users/jack/.rvm/gems/ree-1.8.7-2010.01/bin/bundle: line 1: require: command not found

A quick cat and I see something's changed, no #!...ruby line to see. Realized I had bundler 0.9.0 and 0.9.0.pre4, so I uninstalled them both, and reinstalled with:

$ gem install bundler --pre

and now have bundler-0.9.0.pre5 installed.

Still things are borked, but it seems its a result of github being down: "Page did not respond in a timely fashion."

Will try again in a few to make sure this gets me back on track.

Update

Ok, github's back, and just saw a tweet from Wycats. Looks like 0.9.0 final is out now, but it's recommended to wait til tomorrow and 0.9.1 + some docs. YMMV.

Application != ApplicationController

So, the app I'm converting was originally written in Merb, was partially converted to Rails 2, and is now on its way towards Rails 3. In this transition I've generally remembered what pieces to change from Merb to Rails, but I had a nice stumble earlier in forgetting to change a controller. So, if you find yourself in a place where you have a route that you know works, but is not recognized, make sure your controller is named whatever_controller.rb, and contains the code:

class WhateverController < ApplicationController
...
end

not "< Application". You'll an error about a route missing and could easily spend a couple hours missing the obvious.

Routes

I keep tripping myself up with this. Lets say you have a route defined as:


match 'foobar/:foobar' => 'foobar#foobar', :as => :foobar


These calls will all fail:


ree-1.8.7-2009.10 > app.foobar_path
ActionController::RoutingError: No route matches {:action=>"foobar", :controller=>"foobar"}
... from (irb):3
ree-1.8.7-2009.10 > app.foobar_path('')
ActionController::RoutingError: No route matches {:use_defaults=>false, :action=>"foobar", :foobar=>"", :controller=>"foobar"}
... from (irb):4
ree-1.8.7-2009.10 > app.foobar_path('test')
=> "/foobar/test"


until that last one which passes in a value. This makes sense, as the route defines foobar as a requirement, and not passing in a value is an 'error'. It's a little less obvious that an empty string doesn't satisfy it to me, and definitely not obvious when you have this in a loop and an object just happens to be missing its value for foobar.

One way around this is to make the :foobar piece optional, and handle that reality in your controller elsewhere. You could also protect against this by checking that object.foobar exists in your loop, and not linking unless it does.

I've also just realized that for a certain route of mine, one that passes in domain names, sending in 'euraeka.com' will result in an error. I'm not sure if this is the recommended solution, but its letting me progress for now:


match 'domain/:domain' => 'search#domain', :as => :domain, :domain => /.+/


I'm using domain here instead of host as I think host might be reserved somewhere. Couldn't seem to get it to work before, so if you're having that issue, let me know.

Saturday, July 18, 2009

getting started with ruby 1.9

Yehuda Katz has a nice post over at his blog talking about Ruby 1.9 and what we need to do to move our community towards it.

Lots of great points are brought up, but for me, the biggest issue is just not having the time to fiddle with things, setup nice little aliases, etc. Thankfully, one of the nice guys over at Relevance has done this for us: spicy-config

On a quick side note, Chad's configs also helped me get up and running with zsh, and that's been a whole other fun trip.

You'll want to clone his repo and take a look in at his .zshrc file along with the ruby_installer and ruby_switcher files inside .zsh. All you have to do is source the files to get the functions defined, and run what you're interested in. I did set things up a little different given that I don't use the Leopard install of ruby...but if you're reading this, I assume you can see what you'd want to change if need be.

That's about it. I can now say "use_ruby_191" and it quickly switches me over, and then 'use_ruby' to jump back to 1.8.6.

Great stuff Chad, thanks!

Thursday, July 16, 2009

a couple quick git tips

The other day someone asked in #git how to fix something in the middle of an unsuccessful rebase. So, I thought I'd rebase some of my own code, reorder things so there must be a conflict, and then try to duplicate their question.

Only problem was that there was no error, and I hadn't really paid attention to which commits I reordered. Thankfully, I got a bit of help from a gent named doener.

I'm including a rather long gist showing the process. Basically, git reflog is your friend, and you should play with this at some point in a test repo like I've done here.

Note: anytime you git reset --hard be very sure and confident of what you're doing.

Thursday, July 2, 2009

Opening your mind with Clojure

I know, I know, clever title, right? It's true though--I've taken the last week or so to take a stab at learning Clojure. I've played with functional languages before, but unfortunately "play" is about as far as I've gotten. This time it was different. I actually made it all the way through Programming Clojure (Pragmatic Programmers), which is saying something (I'm king of "Oh, I'll buy THAT book too!" and never finishing it...). There are a variety of reasons I'm really enjoying Clojure, but for now I'd like to offer some tips in getting set up.

Getting the Source



Rich Hickey is the guy behind Clojure. I'd call him a mad genius, except he seems pretty nice and "happy genius" doesn't really work...Anyway, he's recently agreed to move the project to github, and you can find the two main sources you'll want in his account there. Using the wonderfully helpful github gem you can clone things down with


gh clone richhickey/clojure
gh clone richhickey/clojure-contrib


At this point I'm assuming you've got java installed and working, which may be a big assumption, but I don't have the heart to tackle that mountain today.

So, get into the source and start compiling:


cd clojure
ant


If all goes well, you should have some nice jars all sparkling and ready for use. Now for contrib:


cd ../clojure-contrib
git checkout 3073f0dc0614cb8c95f2debd0b7e6a75c1736ece ***
ant -Dclojure.jar=../clojure/clojure.jar


*** At this point in time, you need this revision to compile against clojure 1.0. I imagine this will change over time, but if you have any problems with compilation, a good place to check is the #clojure channel.

You need to pass in that -D flag so we can use the clojure.jar in compiling clojure-contrib. Once this is done you'll want to put the jars into a common place where java can load them up. I've created an /opt/jars directory on my system, and so I'll drop them into here.

Now, you'll want to make sure the CLASSPATH variable is setup correctly, and it took me a while to do this the first time, so here's a hint:



I think newer versions of java will just let you specify the main directory name. This is for java 1.5 which I somehow seem to still be running.

Almost there!

Setting up a clj file



I've blatantly stolen this file from Chouser, an ever present and extremely helpful soul found in #clojure (he's like Illari of #git for those who frequent that channel...if I ever get rich, a fat check is in their future).

This is what the clj file looks like:



If you're on OS X you'll want to install rlwrap:


sudo port install rlwrap


You'll also want an init file to help load up some useful functions like 'show' into your REPL. Create a file named "repl-init.clj" and put something like this inside:


(set! *print-length* 103)
(set! *print-level* 15)
(use '[clojure.contrib.repl-utils :only [source show]])
(use '[clojure.contrib.stacktrace :only [e]])


This should be executed by your clj script on startup, and setup things automatically for you.

Wrap-up



So that's about it for now. I'm having a great time so far, and honestly the hardest parts are getting your environment setup and keeping it up to date. If you're used to java development then this should be a breeze. Having been away from it for years, it took me a while, and hopefully this will save others that same pain.

Thanks to Paul Barry and Aaron Bedra for getting me interested in Clojure at Ruby Nation, and of course Rich et al for all the time and energy put into developing this great new language.

A few links



clojure.blip.tv - Rich gives some great presentations, and many of them are captured here.
clojure.org - Lots of great documentation here
#clojure on irc.freenode.net - over 100 clojurists and clojuristas. Very helpful and friendly.

Saturday, December 13, 2008

merb and sequel

Recently I needed to order some records by a mathematical formula, and I couldn't figure out a way to do it in DM (although it looks like the idea was well received and might make it into a future relase). I'd heard some great things about Sequel, so I decided to take a look and see what possibilities I had there.

If you haven't read anything about Sequel, head on over to its rubyforge page at http://sequel.rubyforge.org/documentation.html. There's a good amount of information from basics to how you can implement complex associations between models. There's even a link to Lori Holden's MerbCamp presentation.

Sequel piqued my interest in many ways: it uses the Active Record Pattern to create methods from column names, but you can also do a more DataMapper style definition of columns inside your model:



You can use Post.create_table! as a sort of automigrate (it will drop and recreate the table). The usage of set_schema is more for test code and experimenting, and I found it very useful when developing sequel_polymorphic and sequel_taggable.

So, while its easy enough to generate a new merb core app and customize it as you like, if you're going to be generating a lot of apps with the same configurations, its worth learning how to build a stack. Currently its mostly a manual process: you build a gem with a certain structure by hand, and then use your stack with merb-gen. Eventually this will probably be an easily streamlined process, but for now its still up to you to put things together. You can read more about the structure here and take a look at merb-sequel-stack as well.

If you're looking to get a bit closer to your DB and want a lot of flexibility and power, you should really take Sequel for a test drive. Its current maintainer, Jeremy Evans, has been a ton of help with answering dozens of questions in #sequel, even when its to answer something he doesn't agree with like polymorphic associations, which I'll go into next time.

Monday, December 1, 2008

a possible addition to www.builtbythenet.com

The same questions get answered many times a day in #merb and I have to think there's a better way, especially for dependency issues. Whether is a rubygems issue, one with Merb or any of the typical libraries people use it with, or just some random little bug you only see on windows, I haven't seen a good way to track and relate all of this information. Some goes on a wiki, some is remembered and routinely typed back by various people in #merb, some goes to the mailing list. It shouldn't have to be this way, and I'm hoping this idea will help:

dependency hell mitigator

The implementation would be dead simple, probably something like a tag cloud. It'd rely on people entering info and trying to keep things up to date...but hopefully this would result in a more organized and useful collection of all the various tricks we employ to get through those moments of keyboard tossing and mice bashing.

As usual anyone interested in coding this up is more than welcome to--the source is at jackdempsey/builtbythenet If enough people like this idea then I'll probably start in on it myself at some point and see where it goes.

Monday, November 24, 2008

great article on merb-auth

angryamoeba has written up a nice explanation and tutorial of merb-auth over at his blog. If you've been curious about how the pieces fit together, or just want to get merb-auth up and running, take a moment and head over.

Friday, November 14, 2008

I launched a site today

It takes a number and divides it by 86,400. The next iteration will include functionality to take a number and multiply by 86,400. Amazing right?

The site has no design. I don't have a business plan. I don't have sales, marketing, QA, or anyone else. And I couldn't care less.

I realized today that I agree with "release early, release often", that I try to live by those words when I can, but as a developer you only have so much control over what your boss thinks, plans, schedules, and so on. At night you're perfectly free to release any sort of crap you want, when you want to, and similar to my recent post on testing, I've realized I need to just get over my desire to perfect things and refactor ad infinitum, and just ship something.

So the site is simple. It basically does nothing right now but convert the number of requests per day into what it'd be in requests per second. This came out of a discussion with Topfunky who was wondering what 8 million requests a day would be in RPS. It was a perfect example feature to spawn the launch of www.builtbythenet.com

The idea is simple--GitHub brings you "Social Code Hosting" right? Well, think of this as Social Site Building. Users can submit feature requests at http://builtbythenet.uservoice.com/, vote for other's ideas, and take part in building a site like never before, because not only do the users drive development, the code is completely open: http://github.com/jackdempsey/builtbythenet/tree/master

People often say "I'd love to learn Merb, but am tired of building blogs. What should I build?" Well, now you have something else to try out. I imagine, at least I hope, that over time some great ideas will come out of the ether. The possibilities are endless. The 'goal' in my mind is less about what the end result is, and more about the process and the features along the way. I don't think this will ever be finished. Maybe in a few weeks it'll fall flat on its face and oh no, I'm out a $9.95 domain name. But at least I'll have tried...and if all that comes of this is another example of some Merb code, that's good enough for me.

Wednesday, November 12, 2008

rspec macro methods in Merb

While working on spec'ing out this merb-service-example app I came to a point where I had a lot of repetitive code, enough that I really wanted to DRY it up a bit. Long story short, after talking with David Chelimsky for a bit, I ended up with this: gists_spec.rb

I think the it_should_respond_with and it_should_return methods work well for adding clarity, reducing repetition, and making the specs even stronger. Still, I'm a bit unhappy with the method names. Some other possibilities thrown out there are:



Nothing feels perfect yet...and maybe it doesn't need to be...but, that's never stopped me from trying. What name(s) do you like? Any better suggestions?

Get over yourself and write that test

I've had to tell myself this a bit recently...and it wasn't the medium you'd expect.

I'd been working on a particularly tricky Sudoku puzzle...you know the kind where you've got lots of squares completely solved, and yet some are mostly blank? No matter what I did, I couldn't find a damn 7,8, or 9 to set off a finishing chain of selections. I also have this stubbornness that forces me to try and solve the hard puzzles without using notes. I almost always can, and yet this particular puzzle was besting me.

Its ok, I just have to try harder, right? Focus more, go over the various possibilities, what could this being here mean, and so on. Another 20 minutes pass and no progress. With a sigh I finally gave up, and started sketching out all of the possibilities. Within 3 seconds of doing so, I saw an obvious selection I had missed. This set off another, and another. 2 minutes later I was done.

Part of me felt like I "cheated"--any great Sudoku solver wouldn't need such help, right? At the same time, there's a point where the challenge of solving the puzzle degrades into frustration and really a waste of my time, and in this case I had long overshot that mark.

Feel familiar yet?

I couldn't help smiling as I thought about this on the metro in this morning. It reminded me of every time I'd sit down to write some code, get it wrong, redo some things, get it right...and then find out I really didn't...and repeat this process until I'd finally write a few tests and voila, be done with it.

I remember back in college hearing these epic stories of masters who could code in C, top to bottom, and at the end magically compile things without error. I think of people I know who can jump into a presentation, test, interview, or module, and just get what they need done without a moment's hesitation or need of 'backup' or notes.

I used to be one of those people...but I think lots of us were, back in 5th grade, just excited to learn the next set of formulas or geometric equations. I'm not anymore, and I'm ok with that. There's a certain peace of mind, a certain relaxation I feel, when I have code that's spec'ed, tested, and banged on to the point where I own it, and not the reverse.

Something I think people don't say enough, even with the tons of discussions about tests: even if your tests suck, even if they mock too much, are too brittle, and perhaps miss some important cases, the knowledge of the code that you receive while writing those tests is extremely valuable. The investment in your own ability to write those tests is extremely valuable. Testing really is like working out, and I've slipped as of late (in both areas), and can feel it.

I made the decision recently to change both of these things, and just like getting back into the gym has given me more energy and I just feel better, getting back into writing some tests has reminded me just why we should do it in the first place.

I've been flexing this rebirth with a simple merb 1.0 service example app located at http://github.com/jackdempsey/merb-service-example/tree/master and you'll see its using the new request helper. I'd talk more about that, but wycats has done so already, so I'll just say: you really, really want to test this way as opposed to mocking everything out. It allows for a very fluid and flexible inner workings of your controller, while verifying things come back as they should.

PS: I even TDD'ed this post via a friend checking it. As you'd expect, I was shown several 'bugs' and issues. Thanks again Doug

Tuesday, November 11, 2008

Installing Merb 1.0 on Debian

This has come up a few times in #merb: as a result of the webrat and nokogiri dependencies, if you're looking to install 1.0 on a Debian box, make sure you have the following packages installed:

libxml-ruby libxslt-ruby libxml2 libxslt1-dev

Thanks to mr-interweb and maxbaroi for some late night detective work. The relevant ticket is located at http://merb.lighthouseapp.com/projects/7433-merb/tickets/986-webrat-is-required-for-merb-core-app-running-in-console#ticket-986-14

Wednesday, November 5, 2008

acts_as_commentable

Recently I've been looking over my various github projects, and am trying to give some love to things I haven't touched in a while. First up: acts_as_commentable.

A small amount of history first: the plugin was originally developed at http://www.juixe.com/techknow/index.php/2006/06/18/acts-as-commentable-plugin/

There've been many comments on it since then. My original intention was to port it over to something we could use in Merb. Turns out that actually didn't require much...actually, it was basically just an extra require in the library. I've touched things up a bit and have updated the repo at http://github.com/jackdempsey/acts_as_commentable/tree/master

I've sent an email to the original author as I'm not trying to pirate his/her stuff...we'll see what comes of that, but I noticed the other day that there were 54 or so watchers (and on a tangent--I mentioned that in #github and this might have sparked some ideas along the line of being able to easily see who's watching your repos), so hopefully those who're interested can play with things, and finally have a vehicle to contribute if desired.

Please feel free to send me comments, patches, etc. Something this widely used is just being for community support and feedback.

Sunday, November 2, 2008

useful snippets

I rely on a number of aliases, shell scripts, and whatever time savers I can come up with. You never know what you might learn by seeing someone elses, so here's a few of my favorites:



I've mentioned the cdargs stuff before, and there's a great post talking about git bash completionfor those interested.

k, now it's your turn.

Monday, October 20, 2008

Creating a new Merb stack with Templater

In trying to remove Merb's dependency on ActiveSupport, Jonas Nicklas developed Templater. I'm going to show you briefly how to use it to create an ActiveRecord, TestUnit, and Prototype based Merb stack.

I'd like to make clear at this point that this is more for learning about Merb, generators, and to use as a reference point for creating other stacks. I prefer DataMapper, Rspec, and JQuery, and as such this example stack won't be supported officially or unofficially (unless someone else would like to...if so, let me know).

You can find the code here: http://github.com/jackdempsey/merb-ar-stack/tree/master

The first important piece is the Generators file:

scope 'merb-gen' do
dir = File.join(File.dirname(__FILE__), 'lib', 'generators/')
Merb.add_generators dir + 'merb_ar_stack'
end



This file is picked up by Templater and as such, when merb-gen is run, you'll see it listed as one of the available generators. Lets take a look at what merb_ar_stack actually is then.

merb-ar-stack/lib/generators/merb_ar_stack.rb


module Merb
module Generators
class MerbArStackGenerator < AppGenerator
#
# ==== Paths
#

def self.source_root
File.join(super, 'application', 'merb_ar_stack')
end

def self.common_templates_dir
File.expand_path(File.join(File.dirname(__FILE__), 'templates', 'application', 'common'))
end

def destination_root
File.join(@destination_root, base_name)
end

def common_templates_dir
self.class.common_templates_dir
end

def testing_framework
:test_unit
end

def orm
:activerecord
end

#
# ==== Generator options
#

option :template_engine, :default => :erb,
:desc => 'Template engine to prefer for this application (one of: erb, haml).'

desc <<-DESC
This generates a "prepackaged" (or "opinionated") Merb application that uses ActiveRecord,
TestUnit, helpers, assets, mailer, caching, slices and merb-auth all out of the box.
DESC

first_argument :name, :required => true, :desc => "Application name"

#
# ==== Common directories & files
#

empty_directory :gems, 'gems'
file :thorfile do |file|
file.source = File.join(common_templates_dir, "merb.thor")
file.destination = "tasks/merb.thor"
end

template :rakefile do |template|
template.source = File.join(common_templates_dir, "Rakefile")
template.destination = "Rakefile"
end

file :gitignore do |file|
file.source = File.join(common_templates_dir, 'dotgitignore')
file.destination = ".gitignore"
end

file :htaccess do |file|
file.source = File.join(common_templates_dir, 'dothtaccess')
file.destination = 'public/.htaccess'
end

file :doctask do |file|
file.source = File.join(common_templates_dir, 'doc.thor')
file.destination = 'tasks/doc.thor'
end

file :prototype do |file|
file.source = File.join(common_templates_dir, 'prototype.js')
file.destination = 'public/javascripts/prototype.js'
end

directory :test_dir do |directory|
dir = testing_framework == :rspec ? "spec" : "test"

directory.source = File.join(source_root, dir)
directory.destination = dir
end

#
# ==== Layout specific things
#

# empty array means all files are considered to be just
# files, not templates
glob! "app"
glob! "autotest"
glob! "config"
glob! "doc", []
glob! "public"
glob! "lib"
glob! "merb"

invoke :layout do |generator|
generator.new(destination_root, options, 'application')
end
end

add 'ar-app', MerbArStackGenerator
end
end


Pretty straightforward, right? You'll see a couple places easily configured to be whatever you like, as well as listing of various files you want to include (like the prototype.js reference).

Beyond that, you'll see inside lib/generators/templates an application directory that holds a directory for the common files you'll use as well as a directory that actually lays out what the generated app will look like.

The final important piece is inside the Rakefile:


gems = [
["merb-core", "~> #{GEM_VERSION}"],
["merb-more", "~> #{GEM_VERSION}"],
["activerecord", "~> 2.1.0"]
]


This array is used later in the rake file by add_dependency. It takes care of bringing in the various gems needed for your stack. Take a look here to see what the official stack depends upon.

Thats pretty much it. There are other pieces that you'll want to configure things (like in the config/init.rb file inside your templates/application/merb_ar_stack folder for instance). I'd recommend forking this repo and tinkering with things a bit to get a feel for how it all fits together.

Things have been a bit busy recently, so I haven't had as much time for blogging as I'd like. I hope to write more in the near future when 1.0 is out and people are looked to really dig into things.

Tuesday, September 9, 2008

Thor finds Mjolnir

If you've been following the development of Thor, you'll know there's been a bug that affected commands with several layers of namespacing. Tonight I'm happy to announce this bug was finally crushed by Yehuda Katz.

Just to briefly describe the pain this has brought me, a few facts:

* Class.constants only gets the top level constants. If you have Foo::Bar::Baz, don't expect to see Baz in Foo.constants

* Struct is mean:



(You can imagine how the first point led me to the second, right? Yeah...fun.

* Remember, when you ask for constants on a class, it:

Returns an array of the names of the constants accessible in mod. This includes the names of constants in any included modules (example at start of section).

It _also_ returns constants defined in any class you inherit from.

So, lets just say this all added up to a nice bit of hacking for me, and truthfully it was a good experience. It started to get old after a while, but I think this is something people don't talk about enough. Its the same with lifting weights: you purposely destroy and rip apart your muscles so they can build up bigger and stronger next time.

If you don't at least semi-often tackle hard problems, frustrating situations, and just fight through those moments when you think "screw it, PHP isn't that bad", you'll never get the kind of experience that really makes you grow as a Rubyist, and really, a programmer. You'll often find at the end not only did you learn some new tricks, but sometimes the solution is right there before your eyes....or Yehuda's eyes...but still, you'll get a lot out of it.

So anyway, I'm happy to point people to http://github.com/wycats/thor/tree/master and say 'Enjoy!' The soon to come Merb bundling tasks will be taking advantage of Thor's power, so if you haven't done anything with Thor yet, now's a great time to jump on board.

Saturday, September 6, 2008

router redirects

Merb's router supports a nice redirect feature:

r.match('/old-invalid-url').redirect("/hawt-new-goodness")

One item of note--this redirect defaults to a 301 status, so if you're playing around with this and want 302's instead, pass a false as a second parameter to redirect:

r.match('/').redirect("/login", false)

If you happen to confuse this and set a permanent 301, just add in the false, hit the page again, let the 302 be processed, and you should be good after taking out the redirect again.

Thursday, September 4, 2008

A script to help with Rails -> Merb conversions

People have asked often in #merb for something to help with converting Rails apps to Merb apps. There's no magic Wizard to do this for you, but hopefully this will help:

http://github.com/jackdempsey/find_rails/tree/master

Its a standalone Thor script that looks in your current directory (or a provided path to an app), finds the app/ directory, and scans through the code for a variety of things that people get tripped up on in Merb. I imagine this list will increase over time, but it will help with some of the obvious ones to start.

This is very much a work in progress. Running it standalone like this, especially given the way I've organized it, loses some of the flexibility Thor provides. It also makes it easier on the user to invoke. I imagine if this is used by people and grows to be anything bigger than a simple helper script, I'll refactor the way the task is started, so I'd recommend cloning this and playing with the source a bit as well.

Anyway, if you haven't played with Thor yet, you should give it a go. Its fast, flexible, and a great way to avoid the "fun" of parsing options yourself.

Currently there's a bit of an issue with installing tasks that have multilevel namespaces. For the time being, I've committed a fix to the constants branch of my fork at http://github.com/jackdempsey/thor/tree/constants

I foresee this fix getting cleaned up and merged into the main Thor repo soon. In the meantime, if you're looking at using Thor with tasks like foo:bar:baz, this fork will fix things up for you.

Update

I fixed things up a bit so that we can search on regex's as well. This came out of a discussion in #merb re: the tendency to "redirect && return" in Rails, which is the reverse of what you'll want to do in Merb.

http://github.com/jackdempsey/find_rails/commit/ea80df077c8cee0959ce1e7b4b17321924f19dc7

Saturday, August 30, 2008

Experimenting with Ubiquity

For those of you who know what Ubiquity is, I'll keep this brief. For the non-initiated, take a second to read about what many describe as Quicksilver for Firefox: http://labs.mozilla.com/2008/08/introducing-ubiquity/

You can install it here, read the tutorial and then come back when up to speed.

Cool stuff, right? So I took a look at their wiki earlier and was kind of surprised to find that nothing was listed there for Ruby...but you could search for php functions? Blasphemy...so I took their code and hacked it up a bit:



Easiest way to play with this in my opinion is, once you've installed Ubiquity, test it at chrome://ubiquity/content/editor.html

As you can see from the gist, its pretty easy to create a new command to play with and you can easily add in search capability for your favorite site. If its well known there's a shot someone's done that already.

Anyway, I thought this was pretty cool and imagine over time it will enable some pretty interesting mashups. Its almost a more powerful and approachable version of Yahoo Pipes, albeit a different sort of beast.

As I get time I'll be playing more with it, probably hooking up some nice Merb search capabilities. Feel free to drop some comments if you have some ideas, but perhaps aren't familiar with Javascript or coding in general. Enjoy!

Monday, August 18, 2008

Updated merb-thor tasks

Lots of questions have come up regarding sake in #merb recently. The current sake tasks at merbivore.com are out of date, so I updated the tasks inside merb-more itself. You can install using:


http://github.com/jackdempsey/dm-thor/tree/master



For those interested in Thor, I've also updated the tasks at:


http://github.com/jackdempsey/merb-thor/tree/master



They, like the sake tasks, are particular about directory structure, so make sure you've set things up, and run things from the correct directory.