Nested Namespace in Rails 2.0

Apr 13 2008 Published by under English,Ruby

$> 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'}
......

No responses yet

Update RubyGems to new Version on Leopard

Apr 03 2008 Published by under Apple,Mac,OS X,Ruby

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!

8 responses so far

First day in LearnHub

Mar 17 2008 Published by under English,Ruby

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?

No responses yet

Fizz, buzz, fizzbuzz

Feb 06 2008 Published by under English,Ruby

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?

  1. Coding Horror:  Why Can’t Programmers.. Program?
  2. Imran On Tech: Using FizzBuzz to Find Developers who Grok Coding
  3. Wikipedia: Bizz buzz
  4. RubyQuiz: FizzBuzz (#126)

Go back to practice more, and more…

2 responses so far

« Prev