Common Ruby Regex Patterns

Filed Under (Ruby) by adam on 27-07-2008

Tagged Under : , ,

My earlier (rather lame) post on Ruby Regex’s (Regular Expressions) is getting some Google love, so I thought I would supplement it with some more useful information.

If you are searching for Ruby Regex help, my guess is you are looking for…

Validating an email address with a Ruby Regex

Something simple like this next one will get you started.

irb(main):023:0> “me@adamloving.com”.match /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i

For a much more complete email address ruby regex, try:

#
# RFC822 Email Address Regex
# --------------------------
#
# Originally written by Cal Henderson
# c.f. http://iamcal.com/publish/articles/php/parsing_email/
#
# Translated to Ruby by Tim Fletcher, with changes suggested by Dan Kubb.
#
# Licensed under a Creative Commons Attribution-ShareAlike 2.5 License
# http://creativecommons.org/licenses/by-sa/2.5/
#
module RFC822
  EmailAddress = begin
    qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]'
    dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]'
    atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-' +
      '\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+'
    quoted_pair = '\\x5c[\\x00-\\x7f]'
    domain_literal = "\\x5b(?:#{dtext}|#{quoted_pair})*\\x5d"
    quoted_string = "\\x22(?:#{qtext}|#{quoted_pair})*\\x22"
    domain_ref = atom
    sub_domain = "(?:#{domain_ref}|#{domain_literal})"
    word = "(?:#{atom}|#{quoted_string})"
    domain = "#{sub_domain}(?:\\x2e#{sub_domain})*"
    local_part = "#{word}(?:\\x2e#{word})*"
    addr_spec = "#{local_part}\\x40#{domain}"
    pattern = /\A#{addr_spec}\z/
  end
end

Find URLs using a Regular Expression in Ruby

Here is a simple URL matching regular expression.

irb(main):028:0> "http://www.adamloving.com/".match /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix

Ruby boolean operator (or ruby parsing) bug

Filed Under (Ruby) by adam on 09-07-2008

Tagged Under : ,

This has bitten me a couple times in the last few days. The Ruby “or” (or equals) operator appears to have a bug where, when used in an assignment, the first value is assigned rather than the results of the entire expression.

irb(main):001:0> x = false || true
=> true
irb(main):002:0> x
=> true
irb(main):003:0> z = false or true
=> true
irb(main):004:0> z
=> false

ruby version: ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-darwin8.10.1]

I would expect z to be true in that last example.

Zemanta Pixie

Downloading a Remote Image and Saving it with AttachmentFu

Filed Under (Ruby) by admin on 07-06-2008

Tagged Under : ,

I had some issues this week figuring out how to save a file using AttachmentFu. It is well suited to handling files uploaded via a Web form, but when you load a file in programmatically, it isn't obvious what object properties need to be set. Here is what I ended up with.

response = HttpClient.default.request(url)

mugshot = Mugshot.new

temp_file_name = mugshot.write_to_temp_file(response.body)

mugshot.temp_path = temp_file_name

mugshot.content_type = "image/#{content_type}"

mugshot.filename = "imported_photo.#{content_type}"

if person.mugshot

# remove existing database records of files for this person

thumbnail = Mugshot.find_by_parent_id(person.mugshot.id)

thumbnail.destroy

person.mugshot.destroy

end

mugshot.person = person

mugshot.save

Q: How do you stub a method call for a rails unit test such that it returns a…

Filed Under (Ruby) by admin on 07-06-2008

Tagged Under : ,

Q: How do you stub a method call for a rails unit test such that it returns a different value the second time it is called? A:  stubs(:foo).returns(1,2) returns 1 the first call, 2 the second, etc.

Rails rake command to run a single unit test

Filed Under (Ruby) by admin on 06-06-2008

Tagged Under : , ,

Q: What is the rake command to run a single rails unit test? A: There is none, use ruby test/unit/my_test.rb

Ruby Regex Guide

Filed Under (Ruby) by admin on 07-01-2008

Tagged Under : , ,

Regular expressions are powerful, I've never really mastered them. Here is an excellent page with help on using ruby regular expressions.