Rubyize this : 4th edition

The 3rd edition marked the beginning of a new association between Rubyize this and RefactorMyCode. I did things in a rather clumsy manner, telling people about how cool RmC was while asking them to use pastie to submit their refactored version of the code. At that time I didn’t realize that RmC and Rubyize this were ready to work together.
Well, RmC is now the official way to submit your rubyized solution. With the trackback system in place, a link to your refactored code will appear here at the moment you hit the submit button on RmC.
Before diving into the 4th edition, I’d like to take a moment to talk about the results of the 3rd one. We started with something pretty ugly that turned onto something pretty awesome!. On a side note, I really liked how some participants transformed the already mean sentence into something even meaner. As an exemple, the test sentence was : « You truly are a moron, sir! » and someone changed it to « you truly are a stupid moron sir! ». Someone else add fuel to the fire by writing « you truly are a ‘stupid’ moron, stupid sir! ». And finally, someone went as far as to write : « Stupid wabbit! Stupid, stupid, stupid! ». The situation was completely uncontrollable!
Enough said. Let’s get the ball rolling.
I have an array of insects and I want to be able to display on the screen the name, the iq and the annoyance factor of every members of a certain type. This “solution” works but has a ruby rating of 0.5/5. Moreover, it only works for butterflies… which feels rather incomplete and limiting. Remove stuff, add stuff and move stuff around. This dumb code must get smart and pretty!

class Bug
	attr_accessor :type
	attr_accessor :name
	attr_accessor :iq
	attr_accessor :annoyance_factor
	def initialize(name,type,iq,annoyance_factor)
		if !type
			@type="spider"
		else
			@type=type
		end
		if !name
			@name="unnamed"
		else
			@name=name
		end
		if !iq
			@iq = 8
		else
			@iq = iq
		end
		if !annoyance_factor
			@annoyance_factor = 4
		else
			@annoyance_factor = annoyance_factor
		end
	end
end
def keep_butterflies(bugs)
	butterflies = Array.new
	bugs.each do |bug|
		if bug.type == "butterfly"
			butterflies.push(bug)
		end
	end
	return butterflies
end
#create some bugs!
bugs = Array.new
bugs.push(Bug.new("ron","spider","4","2"))
bugs.push(Bug.new("don","spider","2","3"))
bugs.push(Bug.new("jake","ant","9","4"))
bugs.push(Bug.new("chris","ladybug","2","4"))
bugs.push(Bug.new("fred","cockchaffer","0","5"))
bugs.push(Bug.new("greg","butterfly","2","0"))
bugs.push(Bug.new("jason","butterfly","0","2"))
butterflies=keep_butterflies(bugs)
butterflies.each do |butterfly|
	puts "I am a butterfly and my name is #{butterfly.name}. I have an IQ of #{butterfly.iq} and an annoyance factor of #{butterfly.annoyance_factor}"
end

Give it a shot now!

The voice of the community

I am thinking about that for quite some time now… how about having some posts written by other people once in a while? (Once every month maybe?)
If you have something relevant to share with us about anything conerning Ruby, I offer you to do it right here on Ruby Fleebie! just send me your post at frank[at]rubyfleebie.com and if it’s good enough, I’ll publish it.
I hate to say this but to be accepted, your post must follow these guidelines :

  1. Do not use the language of a 12 years old AOLer (e.g. Ruby iz da shit, it rEaLlY r0ckz hArd!!!1).
  2. Do not preach about Jesus. This is a Ruby blog.
  3. No mother jokes (e.g. Your mother is so fat one could believe she is not a human but a demolition truck!!). These jokes are not the slightest bit funny and more importantly they don’t make any sense on a ruby blog. Avoid them at all costs.
  4. Don’t go all crazy writing how much you hate me and how stupid I was to let a troublemaker like you spit his hatred on the blog without any precaution.
  5. Don’t scare my readers by telling them how much it would rock if someone would invent COBOLOnRails, ADAOnRails, FileMakerProOnRails or FoxtranOnRails.
  6. Don’t start your post like this : “And this is why I’d like to hear you on the subject”.
  7. Don’t threaten my readers by telling them you will suck their blood.
  8. And finally, under any circumstance, DO NOT insult Fleebie! Let this friendly and peaceful red alien alone for God sake!

That’s all I can think of right now.
Seriously, I cannot guarantee WHEN and IF I will publish your post, but if it’s relevant in one way or another, I will.

Rubyize this : 3rd edition

It’s time for another phenomenal edition of the fantastic Rubyize this serie (how humble is that?)
But before we go any further, I’d like to briefly talk about a brand new web 2.0 rails app that has been developed by Marc-André Cournoyer, a Ruby/Rails guru from Montreal. The app is called RefactorMyCode and became instantly popular among the coding community. The principle behind : People submit chunks of their own code that they would wish to see improved. Every members can then try their luck and submit their refactored version of the code. Marc told me that the Rubyize this serie partly inspired him in the creation of this application… it’s always cool to hear things like that!
Ok, now here is what we have to rubyize today :

def remove_insults(input)
   ctr = 0
   input.each do |word|
     word = word.downcase
     if word == "stupid" || word == "moron" || word == "dumbass" || word == "retard"
       i=0
       word.length.times do
          input[ctr][i,1] = "*"
          i+=1
       end
     end
     ctr += 1
   end
   puts input.join(" ").to_s
end
remove_insults "you truly are a moron sir!".split(" ")

Yikes! Pretty ugly this week… that’s how I like them.
IMPORTANT!
Wordpress doesn’t want that you write code… I suggest that you use RefactorMyCode to post your solution instead. You can also use pastie and just write a comment here with the URL to your snippet. The trackbacks are working correctly now so I strongly suggest that you use RefactorMyCode.
If for some reasons you do not wish to use RmC or Pastie, here are some precautions you should take :

  1. Code indentation with spaces doesn’t work. Use another character… an underscore maybe?
  2. Do not use <% or %> (it shouldn’t be a problem this week)
  3. COPY your code in your clipboard before sending it for safety measure… that way if wordpress screw up your code, you will be able to post it again rapidly by just removing what WordPress doesn’t like

Have fun!

Recap of the last MontrealOnRails

I’m sorry for all of those who prefer my regular tutorial-like posts. This one is just a recap of the last MontrealOnRails conference that was held last tuesday in the StandoutJobs offices. (Psst! They’re hiring… so if you know your Rails, you might want to get in touch with them!). I went there along with Dan Simard, the other brain behind TimmyOnTime.
3 presenters talked to the audience :
#1 Gary Haran
Gary made an introduction of the popular javascript framework Prototype, which is nicely integrated with Rails. With Prototype, writing javascript is easier, faster, and more ruby-like. The only thing Gary seemed to dislike about Prototype is the name. Like he said, it’s confusing because Prototype is already a reserved keyword in javascript. (and if I can add my 2 cents, it’s probably one of the most boring product name in the world). That insignificant detail aside, you should really take a look at this library.
#2 François Beausoleil
François talked about Piston, an interesting ruby library that allows your svn repositories to be up to date with the code you do not own (i.e. 3rd parties). Say for example that you’re working on a Rails application and that you have installed a plugin called foobar. What if the folks who maintain foobar released a new version and that you want to have it in your svn repository? The answer : piston update vendor/foobar. The up-to-date remote files will then be copied in your vendor/foobar directory. The next time you commit, your repository will have a fresh copy of the latest foobar version!
#3 James Golick
James talked about 2 pretty slick Rails plugins : make_resourceful (warning, its a PDF!) and shoulda.
make_resourceful ease the creation of your RESTful controllers. With just a few lines of code, you get a functional Create, Read, Update and Delete function for your entity.
Nothing is better than an example :

class Posts < ApplicationController
  make_resourceful do
    actions :show, :index, :create, :edit, :update, :destroy
  end
end

That ultra readable and short code above will create all the basic actions for your restful controller.
Shoulda is basically a set of macros that seems to really cut the time one can spend writing functional and unit tests. Say for example that I want to test if the flash is currently set to "Thank you for your payment!" when a user has sent his payment, I could just use the shoulda macro should_set_the_flash_to "Thank you for your payment!" and bingo, I'd be fine!
I also had the chance to talk with great people like Marc-André Cournoyer, Fred Ngo, Gary Haran, James Golick, Daniel Haran and a few others that I unfortunately don't remember the name.
All in all, this edition of MontrealOnRails was a great success. Thanks to StandoutJobs for hosting this event, to the presenters and to everyone who showed up.


mor2.JPG