Archive for the 'Ruby' Category
RailsConf 2008 Summary and Review
We three of us in Learnhub, Wesley Moxam, Carsten Nielsen and my self have attended RailsConf 2008 last weekend. It was a great event for sure. After came back, we did a presentation for Toronto Ruby on Rails Project Night yesterday. Here is the presentation file which we’ve used:
More details are on LearnHub Rails Community:
DHH & Libin
DHH & Libin, originally uploaded by Wes and Faye.
Thanks to my dear co-worker Wesley took this nice picture for me!
It was after David has finished his Keynote in RailsConf 2008.
RailsConf 2008
Stay tuned! More details about RailsConf 2008 will be here soon.
See you in Portland!
(Re)Enable TextMate Ruby Bundle
We all love TextMate, by all means. It is the best friend for Ruby Developers.
After our project upgraded to Rails 2.0+ at LearnHub.com, I noticed that there are some very powerful Ruby Bundle Commands just stopped working. Especially these two:
- Run Focused Unit Test
This is an awesome lovely command in Ruby Bundle, absolutely my favorite! It allows you to run a single unit test out of a whole suite of tests by simply positioning your cursor into the test you’d like to run.
- Run Rake Task
This is another cool command too. It shows a popup window which list all the available rake tasks you have and it will show a pretty HTML result window after you choose from the list.
This thing really bothers me a lot. I could be fine for the second one, as I do have terminal windows open all the time. But I really really miss the first one, as our test files get longer very soon.
I have to make it work!
After couple of hours trying today, I’ve pretty much got the right reason and the simple solution, for now.
The reason is that TextMate includes its own version of builder.rb, which is conflicting with Rails 2.0′ version of builder.rb.
The simple solution:
- Open TextMate Bundlers Editor
- Find Ruby -> Run Focused Unit Test
- Search for RUBYLIB=”$TM_BUNDLE_SUPPORT/RakeMate${RUBYLIB:+:$RUBYLIB}”
- Change to RUBYLIB=”$TM_BUNDLE_SUPPORT/RakeMate”
- Reload Bundlers
That’s it! And apply the same changing method to other commands such as “Run Rake Task” as well.
And just want to point out the alternate solution is:
mv /Applications/TextMate.app/Contents/SharedSupport/Support/lib/Builder.rb /Applications/TextMate.app/Contents/SharedSupport/Support/lib/Builder.rb.bak
It’s up to you to choose one of them.
Hope Google can pickup this post quickly so we can save somebody else sometime too.
Enjoy!
Nested Namespace in Rails 2.0
$> mate config/routes.rb
map.namespace :admin do |admin| admin.resources :user end
$> rake routes
admin_user_index GET /admin/user {:controller=>'admin/user', :action=>'index'} ......
$> mate config/routes.rb
map.namespace :admin do |admin| admin.namespace :user do |user| user.resources :profile end end
$> rake routes
admin_user_profile_index GET /admin/user/profile {:controller=>'admin/user/profile', :action=>'index'} ......
Update RubyGems to new Version on Leopard
RubyGems just updated to version 1.1.0. Couple of the major changes are “Index updates are much faster now” and “only updates from a latest index by default”. So, time to update.
As Leopard already has Ruby and RubyGems preinstalled (Thanks, Apple!). So the default update way:
$ sudo gem update –system
will NOT work well.
Here is what you should do on Leopard 10.5.2:
$ sudo gem install rubygems-update
$ sudo update_rubygems
Enjoy!
First day in LearnHub
Today is the day!
It’s my first day in LearnHub. Pretty exciting! Time to have fun with Ruby on Rails fulltime!
Here is our development team at March 17, 2008:
Want to know more?
Fizz, buzz, fizzbuzz
I was hit by a simple question today: write a fizzbuzz in ruby. Pretty fun anyway.
What’s Fizzbuzz?
Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.
I started from this:
class Fixnum ... end
I think it was a little overkill and slow the coding speed a lot. Here is the result:
def fizzbuzz (1..100).each { |item| puts print_value(item) } end def print_value(item) print_value = '' print_value += 'Fizz' if item.div_by_3? print_value += 'Buzz' if item.div_by_5? print_value.empty? ? item : print_value end class Fixnum def div_by_3? self % 3 == 0 end def div_by_5? self % 5 == 0 end end fizzbuzz
Here are some simple samples from internet:
One liner:
1.upto(100) { |n| puts n % 3 == 0 ? n % 5 == 0 ? "fizzbuzz" : "buzz" : n % 5 == 0 ? "fizz" : n }
Another one liner:
puts (1..100).to_a.collect { |i| i % 15 == 0 ? ‘fizzbuzz’ : (i % 5 == 0 ? ‘fizz’ : (i % 3 == 0 ? ‘buzz’ : i)) }.join(’ ‘)
One more one liner
puts (1..100).map{|i|i%15==0?’FizzBuzz’:i%5==0?’Buzz’:i%3==0?’Fizz’:i}
Regular one, pretty close to mine:
(1..100).each{|i| x = '' x += 'Fizz' if i%3==0 x += 'Buzz' if i%5==0 puts(x.empty? ? i : x); }
Want to know more?
- Coding Horror: Why Can’t Programmers.. Program?
- Imran On Tech: Using FizzBuzz to Find Developers who Grok Coding
- Wikipedia: Bizz buzz
- RubyQuiz: FizzBuzz (#126)
Go back to practice more, and more…