CommonThread

Displaying articles with tag

Tip: Rails Options Helpers

Posted by anthony crumley, Wed Jan 09 14:54:00 UTC 2008

Ok, so these are not really helpers but they are helpful. Rails often uses a hash as the last method parameter to pass options. When the method has a variable number of parameters there is a need to pull the options hash out of the arguments array. To do this they added an extract_options! method to the Array. This method removes and returns the last argument in the array if it is a Hash.

def some_method(*args)
  options = args.extract_options!
  ..
end

Another common options problem is setting default values. Rails added reverse_merge to the Hash object to help out here.

def some_method(*args)
  options = args.extract_options!
  options.reverse_merge!({:limit => 10})
  ..
end

0 comments | Filed Under: | Tags:

Tip: TextMate Scan

Posted by anthony crumley, Tue Jan 08 13:18:00 UTC 2008

A nice search like feature in TextMate is the scan. Press ctrl-s and a text box opens in the status bar at the bottom of the editor window. As you type into the box TextMate will automatically select the first match in the open document. Press ctrl-s again to jump to the next match. Press escape or return to exit scan mode.

0 comments | Filed Under: | Tags:

Tip: Ruby Here Document

Posted by anthony crumley, Wed Dec 19 23:12:00 UTC 2007

“Here, document. Come here boy. That’s a good document.” Often times we need to create fairly complex strings in code. The result is usually multiple concatenations that tend to get a little wild and woolly. One solution is to put the string in a separate document or template that uses ERB. This can be overkill for a one-off situation. To tame this mess Ruby provides the here document. I assume the strange name comes from it being like putting the complex string in a separate document but it is right here instead of there.

Here documents begin with << or <<- followed by a word which will be the string terminator. If the document begins with << then the terminator must be in the left most column which doesn’t look good in most situations, although it does make the end easy to find.

The most straight forward use is assignment to a variable.

  xml = <<-XML_END
    <project>
      <owner>#{project.owner}</owner>
      <manager>#{project.manager}</manager>
    </project>
  XML_END

The here document can also be used as a parameter to a method. The first time I saw this one it freaked me out a little until I understood it.

def create(project, user)
  ...
end

update(<<PROJECT_XML, current_user)
  <project>
    <owner>#{project.owner}</owner>
    <manager>#{project.manager}</manager>
  </project>
PROJECT_XML

0 comments | Filed Under: | Tags:

Tip: Ruby Break, Redo, Next and Retry

Posted by anthony crumley, Sat Dec 15 09:16:00 UTC 2007

Ruby has some interesting loop flow control statements. These work with while, until, and for loops as well as iterators. For some reason, I find it very interesting that they work with iterators.

Break simply breaks out of the most immediate loop and resumes with the next statement after the loop. It is like Bobby Petrino leaving the Atlanta Falcons to coach at Arkansas. Just stop whatever is going on and move to the next thing without looking back.

Redo repeats the current iteration of the loop without rechecking the condition. It is like a do over or mulligan.

Next skips to the end of the current iteration and begins the next one normally. It is like the Soup Nazi, “No soup for you! Come back, one year. Next!!”

Retry starts the whole loop over again from the beginning. It is like the movie Groundhog Day. You can just keep repeating it until you get it right.

0 comments | Filed Under: | Tags:

Tip: TextMate New Ruby Method

Posted by anthony crumley, Tue Dec 11 09:33:00 UTC 2007

There are two ways to quickly create a Ruby method in TextMate. The first is to type def and then press the tab key. The result will be…

def method_name

end

The method_name will be highlighted and ready for you to type the name of your method with any necessary parameters. Once the name and parameters are entered you can press tab to begin writing the method.

The second way is to type the name of the method followed by shift-return. If the method name is find then the result will be…

def find(args)

end

The args will be highlighted and ready for you to enter the parameters. If the method does not require parameters then simply press delete once to remove args and the parenthesis. When you have deleted or entered the parameters press tab to begin writing the method.

0 comments | Filed Under: | Tags:

Tip: Ruby On Rails Random Array Element

Posted by anthony crumley, Thu Dec 06 21:56:00 UTC 2007

Rails 2.0 will include an array extension for retrieving a random element. The method name is rand. Since the active record find method returns an array, this method can be used to retrieve a random item from a database. In the following example a featured product will be randomly selected.

Product.find_featured.rand

0 comments | Filed Under: | Tags:

Tip: Ruby on Rails Array to_sentence

Posted by anthony crumley, Tue Nov 27 07:36:00 UTC 2007

Ruby on Rails extends many of the Ruby types with interesting and often useful methods. The to_sentence method on arrays returns the values in an array separated by commas with a conjunction between the last two. There are two options. :connector provides the conjunction with ‘and’ being the default. :skip_last_comma is a boolean that determines whether or not the last comma is displayed and the default is false.

favorite_fruit = ['apples', 'oranges', 'grapes']
puts "My favorite fruits are #{favorite_fruit.to_sentence}." 

Result:

My Favorite fruits are apples, oranges, and grapes.

2 comments | Filed Under: Ruby on Rails | Tags:

Tip: TextMate Overwrite Mode

Posted by anthony crumley, Sat Nov 24 08:10:00 UTC 2007

Overwrite mode does exactly what it sounds like. It overwrites existing text as you type. When in overwrite mode the caret is an underscore. Option-apple-O toggles overwrite mode on and off.

I don’t use overwrite as much as I should. I find myself using the delete key way to much. Often I will delete a bunch of text then type something in its place when overwrite mode would have eliminated the delete step. Next time you find yourself doing this, try switching to overwrite mode and save your delete key some grief.

0 comments | Filed Under: | Tags:

Tip: Ruby Range Operators

Posted by anthony crumley, Fri Nov 23 08:10:00 UTC 2007

There are actually two Ruby range operators. The .. operator is inclusive of the end points and the … operator is exclusive of the high value end point.

a = [0, 1, 2, 3]

a[0..-1] => [0, 1, 2, 3]
a[0...-1] => [0, 1, 2]

You would think … would be helpful for using a.length for the high end point but it doesn’t seem to matter.

a[0..a.length] => [0, 1, 2, 3]
a[0...a.length] => [0, 1, 2, 3]

0 comments | Filed Under: | Tags: