Skip to main content

Ruby: Interesting Things from the Rails Book

Here are a few interesting things I stumbled across while reading the Rails book recently:

Use debug(object) to dump an object to the screen in a template.

Here's how to use the debugger to debug your web app. First, "gem install ruby-debug". Then, start the server via "./script/server --debugger". Now, add "debugger" in your code.

Use "rake --tasks" to see the full list of rake tasks available.

Use "rake --describe task" to describe a particular task.

David Heinemeier Hansson said this on page 261, "Learn to cope with the panic attacks of unexplained errors."

To create a new, struct-like class, use "Rating = Struct.new(:name, :rating)".

ActiveSupport adds a ton of useful methods to the String class that alleviate some of the pain associated with Ruby strings. (I like the way Python treats characters and string slices a bit better.) For instance, it adds .from and .to for slicing.

Use "config.active_record.schema_format = :sql" to configure Rails to dump the schema to schema.rb in SQL instead of Ruby.

Use "config.action_view.field_error_proc" to configure how Rails outputs fields when there is an error.

Comments