Feeds:
Posts
Comments

Posts Tagged ‘rubyonrails’

Understanding Ruby blocks, Procs and methods is not easy for a Ruby beginner, especially if Procs and Lambdas are involved. Yet the basic elements are simple, as Matz says, blocks are basically nameless functions. You can pass a nameless function to another function, and then that function can invoke the passed-in nameless function.
An ampersand in [...]

Read Full Post »

Nil or Empty

Do you use statements like

.. if !name.nil? && !name.empty?
.. if params[:name] and !params[:name].empty?

in your code? Using the blank? function of Rails, they can be simplified to

.. if !name.blank?
.. if !params[:name].blank?

But we can go even further. Using the present? function, this is equal to

.. if name.present?
.. if params[:name].present?

See the documentation [...]

Read Full Post »

In Rails the pluralizations are managed by the Inflector class: it knows that the plural of “user” is “users”, but the plural of “person” is “people”. To add new inflection rules, one can add new rules in the environment.rb, but it is important to add the Inflector.inflections block after the Initializer.run block.

ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular [...]

Read Full Post »

Although the development of applications is fast in Rails, the applications themselves are often a bit slow. The elegant framework increases the productivity of the programmer but reduces the performance of the application. The DRY principle is great to prevent code repetition, but it is less optimal to increase speed: it is of course much [...]

Read Full Post »

Date Arithmetic

Date arithmetic in Ruby is possible because Ruby’s time objects are stored internally as numbers. Additions to dates and differences between dates are handled by adding to and subtracting the underlying numbers. Time and DateTime objects are a bit different. Time is a wrapper for the Unix or POSIX standard time defined as the number [...]

Read Full Post »

Recently I stumled upon a blog post which asked if design patterns are useless. Are they useless? In fact the classic GOF design patterns (COMPOSITE, STRATEGY, MEMEMTO, etc) seem to be useless for developing Ruby on Rails web applications. I haven’t seem them in many Ruby on Rails application so far, and I haven’t use [...]

Read Full Post »

If you want to add additional parameters to a link using a path helper, for example for nested restful resources, it is useful to use a hash. All additional parameters are added to the query string:

group_post_path(:group_id => group, :id => post)
>> /groups/4/posts/2
group_post_path(:group_id => group, :id => post, :option => 1)
>> /groups/4/posts/2?option=1

If [...]

Read Full Post »

The counter cache for belongs_to associations is a useful Rails feature to speed up your application. It allows to cache the number of objects which belong to the associated class. All you need to do is to add :counter_cache => true to the belongs_to association:

class Comment
belongs_to :post, :counter_cache => true

And you need to [...]

Read Full Post »

There are a couple of functions which you have to look up in any
language again and again, because they are slighty different in
each language, especially those for formatting of dates and
floating point numbers.
To display dates and datetimes in different formats in Ruby on
Rails applications, the following list for default formats
may help. The default date formats [...]

Read Full Post »

If you want to tackle challenging tasks, for example climbing a big mountain, you have the choice: either you join a large climbing team and use a lot of expensive equipment, or you try it alone using free climbing.
In the software world, the task is the software to be written, the climbing equipment are the [...]

Read Full Post »

Rails Conference Europe 2008 is over and the slides are available here. In my opinion the best talks were the ones from Yehuda Katz about jQuery and from Justin Gehtland about Small Things, Loosely Joined, Written Fast. Both are great speakers and the talks were awesome.

Read Full Post »