Archive for the ‘ruby’ Category

RubyNation Etc

Tuesday, June 16th, 2009

I am back from RubyNation and reapplying my nose to the grindstone, wheel to the asphalt and hands to the keyboard. I am still planning a big summary and commentary post based on my notes, but as I started working on that, I realized it might need more than one night to see to completion.

So, I give you this:

Me
Hyatt Ceiling Pastry and Me in My Hat

Carrot Cake(Thanks Hyatt catering)

Mmm Carrot Cake

And Lys, the cat who is strangely fond of white ceramics.

Lys, Sink and You

Tune in some indeterminate point in the future for a fuller update. Or, you could just read this guy’s take.

Sprinkle and Passenger Stack

Tuesday, April 21st, 2009

I started using sprinkle with the passenger-stack recently. So far, it has been easy to get started with and fairly intuitive. I chose it rather than puppet or some of the other declarative server configuration DSL because it doesn’t need to install anything extra on the server to work.

This is great for the kinds of things I have been working on, because they are fairly small, and don’t need many servers.
I think if I were managing more things, I might choose puppet, because the overhead would be justified.

One of the things I like about sprinkle, is the DSL is pretty straight forward, eg the package definition for passenger looks like this:

package :passenger, :provides => :appserver do
  description 'Phusion Passenger (mod_rails)'
  version '2.1.3'
  gem 'passenger' do
    post :install, 'echo -en "\n\n\n\n" | sudo passenger-install-apache2-module'

    # Create the passenger conf file
    post :install, 'mkdir -p /etc/apache2/extras'
    post :install, 'touch /etc/apache2/extras/passenger.conf'
    post :install, 'echo "Include /etc/apache2/extras/passenger.conf"|sudo tee -a /etc/apache2/apache2.conf'

    [%Q(LoadModule passenger_module /usr/local/ruby-enterprise/lib/ruby/gems/1.8/gems/passenger-#{version}/ext/apache2/mod_passenger.so),
    %Q(PassengerRoot /usr/local/ruby-enterprise/lib/ruby/gems/1.8/gems/passenger-#{version}),
    %q(PassengerRuby /usr/local/bin/ruby),
    %q(RailsEnv production)].each do |line|
      post :install, "echo '#{line}' |sudo tee -a /etc/apache2/extras/passenger.conf"
    end

    # Restart apache to note changes
    post :install, '/etc/init.d/apache2 restart'
  end

I had some problems, like the version of passenger in the passenger-stack master branch on github is a point release behind the phusion’s, which is annoying because when passenger updates itself to 2.2.0, the configs are not updated and apache tells you it can’t find the mod_passenger.so file.

Also, it puts the passenger config in a slightly unusual place(/etc/apache/extras), for an apache module, as well as appending stuff to /etc/apache2/apache2.conf. This prevents it from idempotency, because if you run it twice, it will add additional lines to the config files. So, in the vein of scratching my own itch and what have you–you know, that open source thing–I rewrote it.

package :passenger, :provides => :appserver do
  description 'Phusion Passenger (mod_rails)'
  version '2.2.0'
  gem 'passenger' do
    post :install, 'echo -en "\n\n\n\n" | sudo passenger-install-apache2-module'

    # Create the passenger conf file
    loading = %Q(LoadModule passenger_module /usr/local/ruby-enterprise/lib/ruby/gems/1.8/gems/passenger-#{version}/ext/apache2/mod_passenger.so)
    conf = %Q(PassengerRoot /usr/local/ruby-enterprise/lib/ruby/gems/1.8/gems/passenger-#{version}
PassengerRuby /usr/local/bin/ruby
RailsEnv production)

    post :install, "echo '#{conf}' >> /etc/apache2/mods-available/passenger.conf"
    post :install, "echo '#{loading}' >> /etc/apache2/mods-available/passenger.load"
    post :install, 'a2enmod passenger'
    # Restart apache to note changes
    post :install, '/etc/init.d/apache2 restart'
  end

  verify do
    has_file "/etc/apache2/mods-enabled/passenger.load"
    has_file "/usr/local/ruby-enterprise/lib/ruby/gems/1.8/gems/passenger-#{version}/ext/apache2/mod_passenger.so"
    has_directory "/usr/local/ruby-enterprise/lib/ruby/gems/1.8/gems/passenger-#{version}"
  end

  requires :apache, :apache2_prefork_dev, :ruby_enterprise
end

Now, it takes advantage of the debian convention of keeping module loading and configuration files go in /etc/apache2/mods-available that are symlinked into /etc/apache2/mods-enabled by the a2enmod utility. This is immediately obviously awesome to anyone who has contemplated the horror of trying to parse the apache main config to see if the stuff they want to add is already there and needs updating.

I also added some verifiers so that it won’t rerun if there were no errors. Pretty cool, no?

Ruby is awesome–lazy scripting

Monday, April 20th, 2009

Sometimes I find myself doing tedious things like trying to grab the dollar amounts from text copied from the web. More and more often I turn to irb for these sort of things. For example, a few minutes ago, when I wanted to analyze my spending habits I copied the data into a file and manipulated it.

I grabbed the lines from the file

irb(main):001:0> lines = File.readlines 'transactions'


And selected those with dollar signs (with a little effort–sometimes I forget whether it is =~ or ~=).

irb(main):002:0> monies = lines.select {|l| l ~=  /\$(.*)/}
SyntaxError: compile error
(irb):2: syntax error, unexpected '='
monies = lines.select {|l| l ~=  /\$(.*)/}
                               ^
	from (irb):2
	from :0
irb(main):003:0> monies = lines.select {|l| l =~  /\$(.*)/}

Then I grabbed the numbers using map.

irb(main):008:0> nums =monies.map {|m| m.sub( /\$(.*)/,$1).to_f}

From there, I could do all sorts of things.

I could sum numbers.

 total =nums.inject(0){|sum,i|sum+i}

I could get the average.

total/nums.length

I could even see how much of the total was due to transactions under $20.

nums.reject {|i|i>20}.inject{|s,i|s+i}

In short, I like ruby.

Ruby in Practice is Good

Thursday, April 16th, 2009

I have been reading Ruby in Practice over the past week or so as you know. Currently, I am on page 114 which talks about using active resource to consume RESTful webservices( you could use RESTClient, but thats another story) and has a box about the ever awesome BlankSlate class.

I haven’t really learned many “whoah, ruby does what?? Sweet” things from it, most of those are behind me. Now, I get more “Whoah, X project really abstracts that annoying thing into a nice interface.” kind of feelings, which are more satisfying in some ways and less in others. I guess what I am saying is that I have grown to know ruby much better over the past six months or so using it at work every day, than the past, uh, five? years. Which is sweet.

But back to the book( I ramble when I feel braindead):

The things I have learned from reading this book remind me of ruby itself. It has glued together various bits of ruby that I know, in the same way you can use ruby to glue together other technologies. Most of my ruby smarts have been picked up through hacking at things and reading blog posts by knowledgeable people, but that sometimes leaves some gaps in my knowledge. This book has helped me to tie some of the things I have learned together(w00t Hebbian Learning).

For instance, spec tasks

While I know that rspec has a set of extensions for rake, I had not used them outside rails, in fact in the project I was working on I just wrote the following.

task :spec do
  sh "spec --colour --reverse #{FileList['spec/**/*_spec.rb']}"
end

The problem with this is that when any of the specs fail, I get a huge trace from the ran shell script that I have to scroll past before I can see the failed specs.

But, if I included the spec tasks I could do stuff more like this.

require 'spec/rake/spectask'
Spec::Rake::SpecTask.new('spec') do |task|
  task.spec_files =FileList['spec/**/*_spec.rb']
  task.spec_opts = ['--colour','--reverse']
end

Now things work like they do when you generate the tasks from within rails. I had read the lib/tasks/rspec.rake file when I was looking over the generated code in one of my rails apps, but it was a little too complicated to grok easily.

The other things I am looking for are cool projects that I should be more aware of. Sometimes, they are only mentioned but that doesn’t mean they are not awesome. Some that I would like to play with include:

  • Chronic a date string parser that tries to be able to handle stuff like: “next tuesday” and “summer”
  • FasterCSV I saw this at Mountain West Ruby Conf and made a mental note to read it at some point(so much for mental notes)
  • Heckle runs tests. Breaks your code. Sees if the tests fail like they should.

Later.

Merb in Action: Ch 1 Thoughts

Wednesday, April 8th, 2009

Last night I read the first chapter of what will become Rails 3 in Action. So far, I really am enjoying it.

One of the things that frustrates me the most in my own projects is this feeling that things need to be big and get abstracted before they can be useful. I guess it might be a hold over from being a student and wanting to over achieve, or maybe just the classic problem of the hobbyist who tinkers but never manages to really build anything.

It is a gumption trap, to use a Zen and the Art of Motorcycle Maintenance-ism; a thought that sort of stalls my work on a project.

“How will this be useful to anybody?”

“How can I get this to a usable state?”

I think I am getting better at this problem, but sometimes I feel like I have this irresistible urge to over-architect things that gets me into trouble.

What this has to do with Merb in Action?

Well, the thing I thought was really cool was where the authors linked to the pastie that started it all, clocking in at ~120 lines.

I look at that and I think two things.

  1. I usually over think things
  2. I can do this.

The beginning of merb is completely grokable. It reminds me that something doesn’t need to be fully designed. It just needs to be useful enough.

or you could just say:

YAGNI

You Ain’t Gonna Need It.

YAGNI is part of the reason why I have been trying to use BDD in a more disciplined way. By using the Feature->Spec->Implementation workflow, there is less temptation to add neat features on the backend that are never used. I have worked on a number of projects where I ended up writing more code than I needed, than was used.

Writing from the outside in helps to manage that some. After all “no code is faster than no code.”

New Books

Tuesday, April 7th, 2009

I just ordered Ruby in Practice and Rails 3 in Action from Manning(they had a one day only 45% off promo).

The Rails 3 book won’t actually be coming out for a while, but in what I think is an ingenious publishing move, people who preorder can look at drafts of chapters as the book is worked on. It reminds me a little of how Lawrence Lessig is doing the next edition of his books. Except of course totally different.

The thing that makes them similar is the added participation of future readers. It isn’t that this is a new idea. People have been soliciting comments on publications for as long as they have been around. Technology just changes the timescale and the scale of the projects.

Anyway, cool.

Right now, I have Ruby in Practice and through the fourth chapter of Merb–Rails 3 in Action. More later.

Sweet.

Mountain West Ruby Conf Day 2

Monday, March 16th, 2009

After yesterday, I have a lot of grokking to do. And, some projects to start.

The talks, like the day before were great. And having the opportunity to talk with so many people about what they do with ruby was enlightening.

I wanted to write up my thoughts about yesterday, but they haven’t really congealed yet.

Short summary:

  • Ruby is awesome.(of course)
  • I need to be more aware of my metaworkflow
  • Rhodes looks really slick.
  • Adhearsion would be fun to build something with(eg a podcast engine)
  • Destroy is a funny word to users(rails)
  • Of the bicycle gears of software development,cucumber is the outmost one
  • Suite.add(test) if test.value > test.cost
  • be extremely pedantic when you first start trying to use a new methodology–even if it sucks(in the ‘man, why all this typing’ sort of suckage you know makes you want to be lazy).
  • don’t confuse concise with terse.
  • maybe I should try to make a theramin sim with the wiimote
  • energy not time is the most precious resource