Articles

Ruby 1.9.0

Have you heard it was released?

http://www.ruby-lang.org/en/news/2007/12/25/ruby-1-9-0-released/

howto install:

autoconf
./configure --prefix=/usr/local/ruby1.9
make
sudo make install

For a long while we were expecting this product that should increase Ruby performance tremendously.
Mind you, I had yet seen a web application running slow because of Ruby. Most of the cases it was misusage of database and memory structures, the major down-pit Rails developers fall into (newbies as well as experts).

Tiny profiling utility


def prof(t = 1)
  starting_time = Time.now
  t.times { yield }
  dlta = (Time.now - starting_time).to_f
  puts "time: #{dlta.to_s}(s) . req/s: #{(1/dlta)}"
end

  • Use prof to measure how long a function or any block of code takes.
  • Use parameter t>1 to run the block code t times and return the average time per test

Examples:

>> prof { Flight.find_first }

12. class_eval and instance_eval in Ruby

In this screencast we will analyze the class_eval and instance_eval in depth through very simple examples.

15. How to develop Plugins in Rails - Part 3

In this episode we will see the files generated by the plugin generator and their purpose.

ActiveRecord::Base#exists?

class ActiveRecord::Base
def self.exists?(conditions, unique_id = nil)
count1(conditions, unique_id)>0
end
end

Returns true/false if at least one row that follows ceratin conditions is found

ActiveRecord::Base#count1 - fastest counter ever


class ActiveRecord::Base
def self.count1(conditions, unique_id = nil)
connection.execute("select count(1) from #{table_name} where #{sanitize_sql(conditions)} #{'and id<>'+unique_id.to_s if unique_id}").fetch_row.first.to_i
end
end

  • count(1) is the fastest way, as far as I know, for mysql to count table elements.
    Why should we use the relatively expensive count(*) when we can use this modest counter?

11. send method in Ruby

Learn about send and when to use it in your programs.

16. How to develop ActiveResource client and server with authentication

Rails core team recommends developing web applications that are small and very cohesive. This is documented in Domain Driven Design by Eric Evans. This allows the domain model to evolve independent of each other.

In Rails, when you need to interact between web apps you can use ActiveResource.

10. eval and binding in Ruby

Learn about eval and binding methods in Ruby. This is a basic screencast on Metaprogramming in Ruby.

14. How to develop Plugins in Rails - Part 2

Learn how to develop Rails plugins for views in your web application.

Learning Ruby Through Testing | Rails Fire

Learning Ruby Through Testing

We have finally published the Ruby Koans to github at edgecase/ruby_koans If you do not have a github account or use git, do not worry, you can click the download button on that page to receive a zip file containing the latest version of the source.

The idea was born thanks to Mike Clark’s blog post about learning Ruby through unit testing. It teaches both language and culture in a self-paced manner.

We look forward to comments and any feedback you have. Enjoy!