From: steve ross Date: 2011-11-07T05:59:15+09:00 Subject: Re: "A" and "an" articles in front of words Sorry to be late to the party on this one, but a regex seems a bit of a big hammer. How about: def article_for(noun) article = %w(a e i o u).include?(noun[0..0]) ? 'an' : 'a' "#{article} #{noun}" end irb(main):022:0> article_for 'dog' => "a dog" irb(main):023:0> article_for 'animal' => "an animal" On Oct 31, 2011, at 7:14 AM, Dave Aronson wrote: > On Sun, Oct 30, 2011 at 22:23, jake kaiden wrote: > >> ruby Strings have an easy way of finding the first letter, the `[]` >> method... you can do something like this: >> >> string = "hi there" >> puts string[0] >> >> => h > > In 1.9 yes... but many people are still on 1.8, which will give a > number instead. Gotta use string[0:0] instead. :-( > >> you could use a regular expression >> (http://www.ruby-doc.org/core-1.9.2/Regexp.html) to determine whether to >> put an "a" or an "an" before each word based on its first letter... > > If she's having a hard time extracting a character from a string, I > don't think it's sporting to hit her with regexps quite yet. ;-) > > -Dave > > -- > LOOKING FOR WORK! What: Ruby (on/off Rails), Python, other modern languages. > Where: Northern Virginia, Washington DC (near Orange Line), and remote work. > See: davearonson.com (main) * codosaur.us (code) * dare2xl.com (excellence). > Specialization is for insects. (Heinlein) - Have Pun, Will Babble! (Aronson) >