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 Returning

Posted by anthony crumley, Wed Dec 05 14:02:00 UTC 2007

The Rails returning method can clarify your code. Its basic purpose is to semantically group code that creates the return value for a method. We could discuss its K Combinator Lambda Calculus roots but we won’t go there. Heck, I don’t even remember what that crap is.

Returning accepts two parameters. The first parameter initializes the return value. The second parameter is a block that creates the return value.

Returning doesn’t usually save lines of code or help with performance but it does make things a little clearer. One thing it does well is separate setup code from code that actually creates the return value. Consider the following helper method that returns the full name of a user.

def full_name(user)
  user = current_user unless user

  name = user.first_name
  name << ' ' + user.middle_name unless user.middle_name.blank?
  name << ' ' + user.last_name
  name
end

Now the same method using returning to clarify what is being returned.

def full_name(user)
  user = current_user unless user

  returning String.new do |name|
    name << user.first_name
    name << ' ' + user.middle_name unless user.middle_name.blank?
    name << ' ' + user.last_name
  end
end

0 comments | Filed Under: | Tags:

Tip: Ruby On Rails to And from

Posted by anthony crumley, Tue Dec 04 17:33:00 UTC 2007

In Rails 2.0 arrays will have to and from methods that work like the ones on strings. The to(position) method will return elements from the beginning of the array up to and including the position passed in. The from(position) method will return elements from the position passed in to the end of the array.

[1, 2, 3].to(1) => [1, 2]
[a, b, c].from(1) => [b, c]

0 comments | Filed Under: | Tags:

iChat Video vs. Netopia DSL Modem

Posted by anthony crumley, Sat Dec 01 09:49:00 UTC 2007

Last night there was a Technology Death Match between iChat Video and my Netopia DSL Modem. Fortunately iChat won in the end but it was a bloody battle.

Ever since I switched to BellSouth DSL, iChat video connections quit working. iChat would give an error message that the other person on the chat did not respond. After many hours of searching the net, reading, and experimentation I finally got it working. There must be a better way but this is the only one I have found. Disclaimer: This solution works with OS X 10.4 and above. You can really mess up your modem by changing its settings so if you brick it don’t call me. :)

  1. Connect your modem directly to your mac with a network cable. This will allow you to modify your modem settings. If there is a router, wireless or otherwise, between your computer and the modem you won’t be able to modify the modem settings.
  2. Open your web browser and go to http://192.168.1.254 to access the modem settings. If you are prompted to login the default username is admin and the password is 1234. If the username and password have been changed from the defaults you will have to use the new ones.
  3. Select expert mode and say yes to the scary warning message.
  4. Select configure and some additional options will appear.
  5. Select the NAT option under configure.
  6. Now the fun begins. The problem we are solving is that the modem is blocking some of the ports iChat needs. You are going to forward all the ports iChat needs to your computer so everything will work properly. Twelve different forwards need to be added.
    1. Select define custom service.
    2. Select Port Forwarding: Range of Ports.
    3. Select next.
    4. Enter “iChat 5190 TCP” as the Service Name.
    5. Enter 5190 into both Global Port Range boxes.
    6. Enter 5190 into the Base Host Port box.
    7. Select TCP.
    8. Select next.
    9. When the Set-up Complete message is displayed select done and you will go back to the NAT Configuration page.
  7. Add four more forwards with ports 5220, 5222, 5223, and 5298. Select TCP on each of these.
  8. Add six more forwards with ports 5060, 5190, 5297, 5298, 5353, and 5678. Select UDP on each of these.
  9. Add one more forward with 16384 in the first Global Port Range box and 16403 in the second. Put 16384 in the Base Host Port box. Select UDP.
  10. Now all the forwards you created should be listed at the bottom of the Service Name drop down list with an asterisk in front of the name. Each of these need to be enabled before they will do anything.
    1. Select a forward you created from the Service Name list.
    2. Select the enable button.
    3. Select your computer from the Select Host Device drop down list. (A limitation of this solution is that only one computer behind the modem will be able to use iChat Video)
    4. Select the enable button.
    5. You should return to the NAT Configuration page.
    6. Repeat for each forward you created.
  11. You are done. Hopefully you can start video chatting with your friends now.

8 comments | Filed Under: | Tags: