Ruby Kata Two (First Approach)

The coding kata is a way to get programmers practicing their craft just like musicians and martial artists do by: “applying the theory over and over again, using feedback to get betterĀ every time“. You can read more about coding katas here.

The binary chop, the second kata in the series, tasks the programmer to implement a binary search routine using 5 different methods. A binary search is a way to find the position of a value (key) within a sorted array of values.

Example, given an array_values = [1, 2, 4, 6, 8] with a key = 6, the method should return 3 when the array_values and key variables are passed in as arguments of the method, if the key can’t be found, the method should return -1. A quick implementation in ruby can be seen below.

This is one of the sweetness of ruby, however this defeats the purpose of the kata, which is to make the programmer implement the solution. Below is my first solution to Kata 2, I will appreciate your feedback.

Here are the test cases which all pass.

 

How to populate your table with default values in rails

I have a Roles table with :name and :long_name columns. I have only 4 types of roles, with name i.e. short name and long name; :tm => Team member, lm => Line manager, :hr => Human resource and :admin => Administrator. In order to populate the values in my Roles table, all I need to do is put the logic in my db/seeds.rb file and run the rake db:seed command.

seed.rb —

role_collection = { 
  :hr => 'Human Resource', 
  :tm => 'Team Member', 
  :lm => 'Line Manager', 
  :admin => 'Administrator' 
}
role_collection.each do |key, value|
  @a_role = { :name => key.capitalize, :long_name => value }
  Role.create(@a_role)
end

Command
rake db:seed

How to know what methods are available to an object in ruby

I was wondering if there was a plug-in for irb that allowed you to interrogate what methods were available to an object, but I found out thanks to the #ruby-lang channel on irc by Archiloque and drbrain, you can ask the object directly with the following code; my_object.public_methods.sort

Update: After a quick google search, found this stackoverflow.

Update: OOo, this even gets better, people are really willing to help, if only you ask. User “Kenichi” on #ruby irc gave this tip for tab completion in irb;

“raluxgaza: i have “require ‘irb/completion'” in my ~/.irbrc and i can hit tab-tab after a ‘Object.’ and it spits out all Object’s methods, similar to bash.”. Another sweet tip.

Then they suggested this gem

Technology Hype

Okay I admit it, I am very hyped about the hottest programming language out there and basic instinct is always to dive in head first (not very wise). As I said in my previous post, that I will be rewriting my uni project in rails, just before the dust settled, I came across this post which literally smashed my head against a brick wall. FKS stick to your goal damn it! I haven’t dived deep into my current language (C#), that I wanna pick another due to my technology junkie(ness), doing this won’t take me far, so I revert my decision to rewrite the project in rails and stick to the original plan (when it’s done, I’ll blog about it)

Why am I posting this incosistency? Well because I am me, and I have this challenge.

Thought Trails

I have been looking into ruby and rails for a while now and most say, the community ROCKS. One very cool thing about the ruby community, appart from the fact that they are totally cool (cool might mean something else to you), they have a strong focus on TDD and Agile Methodology in general. I’ve been mock about re-writing my uni project using ruby on rails (only done the class library so far) in order to gain some hands on experience in it, MySQL and GIT (of which people have been raving about these days). I am beginning to wonder whether coding with the help of intellisense in visual studio is actually making me lazy or more productive. There are camps that support the benefit of intellisense and others that don’t. Personally I like intellisense, but have I become too used to it that picking up a new language without any intellisense is scaring me away from my comfort zone? Have I become a woosie?

Well to prove to myself that I ain’t no woosie, I will rewrite the class library and site using rails and TDD methodology. FKS (For F*ck Sake) I am disciplined and I eat challenges for breakfast, lunch and dinner.