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 [...]
Posts Tagged ‘rubyonrails’
Blocks, procs, and arguments in Ruby
Posted in development, ruby, rubyonrails, tagged blocks, development, lambda, procs, ruby, rubyonrails on May 19, 2009 | Leave a Comment »
Nil or Empty
Posted in development, rubyonrails, tagged ruby, rubyonrails, tips, tricks on May 12, 2009 | 1 Comment »
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 [...]
Inflector: Rails Pluralization
Posted in development, rubyonrails, tagged pluralization, rails, rubyonrails on April 9, 2009 | Leave a Comment »
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 [...]
Increase the Performance of Ruby on Rails Applications
Posted in development, principles, rubyonrails, software, tagged rails, rubyonrails, cache, performance, speed on February 17, 2009 | Leave a Comment »
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 [...]
Date Arithmetic
Posted in ruby, rubyonrails, tagged date, ruby, rubyonrails, time on December 25, 2008 | Leave a Comment »
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 [...]
Are Design Patterns useless ?
Posted in development, rubyonrails, software, tagged design_patterns, development, patterns, rubyonrails on November 10, 2008 | Leave a Comment »
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 [...]
Adding Query Strings to Links
Posted in rubyonrails, tagged rails, rubyonrails on November 6, 2008 | Leave a Comment »
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 [...]
Update of counter-cache columns
Posted in rubyonrails, tagged cache, rails, rubyonrails, sql on October 13, 2008 | Leave a Comment »
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 [...]
Formatting Dates and Floats in Ruby
Posted in ruby, rubyonrails, tagged date, datetime, float, formatting, rails, ruby, rubyonrails on September 24, 2008 | 2 Comments »
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 [...]
Ruby is like free climbing
Posted in analogies, development, software, tagged development, rails, ruby, rubyonrails, software on September 5, 2008 | Leave a Comment »
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 [...]
Rails Conference Europe 2008
Posted in Uncategorized, tagged conference, rails, ruby, rubyonrails on September 4, 2008 | Leave a Comment »
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.