How to write a ruby function that can accept either a string or a regular expression

While working on our unit test framework, I wanted to write a function that checks if a given Ext JS windoid is opened. The function should accept a title argument. This was easily implemented as:

	# title is a string with the exact windoid title
	def windoidDisplayed?(title)
		spans = $browser.spans
		t = $browser.length && $browser.detect {|i| \
			i.class_name == 'x-window-header-text' \
			&& title == i.text \
		}
		return t && t.visible?
	end

I then realized that users might not have the full title of the window, and might only want to check for a windoid with a title that matches some regular expression. A simple and naive way to do it is

	# title can be a string or a regular expression
	def windoidDisplayed?(title)
		spans = $browser.spans
		t = $browser.length && $browser.detect {|i| \
			i.class_name == 'x-window-header-text' \
				&& (title.kind_of?(String) \
					? (title == i.text) \
					: (title =~ i.text)) \
		}
		return t && t.visible?
	end

However, I didn’t want to check in every loop iteration if the passed title argument is regular expression. Although performance is really a non-issue in our unit tests, it just seemed to be a cumbersome and ugly code. I thought of using the dynamic nature of ruby to add a method (e.g. “match?”) to both string and regular expression classes, so I can call it in a polymorphic way without having to detect the type of title:

	class String
		def match?(x)
			return self == x
		end
	end
	
	class Regexp
		def match?(x)
			return self.match(x)
		end
	end
	
	# title can be a string or a regular expression
	def windoidDisplayed?(title)
		spans = $browser.spans
		t = $browser.length && $browser.detect {|i| \
				i.class_name == 'x-window-header-text' \
				&& title.match?(i.text) \
		}
	return t && t.visible?

Before implementing it, I thought I’d check if a similar function already exists in Ruby’s Standard Library, and indeed, when I looked up the Ruby book for functions that are in both String and Regexp classes, I run into “===” (the Case Equality operator), which is meant exactly for this kind of things. Unlike JavaScript, where the === operator is meant for identity, in Ruby, the === method is usually called in case expressions to match the case target for each when clause. As said in the documentation, for the String class, === method tests for equality and is the same as ==, but for Regexp class, === method tests for matches, and is the same as =~. Exactly what I was looking for. So my little function can simply be implemented like:

	# title can be a string or a regular expression
	def windoidDisplayed?(title)
		spans = $browser.spans
		t = $browser.length && $browser.detect {|i| \
				i.class_name == 'x-window-header-text' \
				&& title === i.text \
		}
		return t && t.visible?
	end

BTW, while searching for that, I took a look at Watir‘s sources, hoping to learn from the experts as I’m new to Ruby.
I found that Watir source (watir.rb, lines 50-70) does a similar trick to what I initially thought to do – define a new “matches” method (and not “match?” like the ruby convention that is also used in Watir in methods like “exists?” and “visible?“), and don’t use the === method.
In addition, in several places in the code there are still explicit tests for String class, such as the verify_contains method in input_elements.rb and navigate_to_link_with_id and navigate_to_link_with_url methods in watir_simple.rb.

About these ads

3 Responses to “How to write a ruby function that can accept either a string or a regular expression”

  1. BrajMohan Singh Says:

    Hi,

    Very nice piece of information. I am facing a similar problem where we have to compare two strings. I applied the regex to match it and it works fine for the string which dont have space between. However in case the matching string containg space, then its failing.

    Please let me know how to tackle this.

    Thanks in advance.

  2. splintor Says:

    @BrajMohan: I think you are in the wrong place. I’m not a Ruby guru – I just posted about a feature I found and thought it would be useful to anyone. If you have a Ruby question (or a regex question), you should find a forum and ask it there….

  3. BrajMohan Singh Says:

    thanks a lot buddy. I have already posted this to few forums. Lets see.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: