From: Max Schmidt Date: 2010-03-31T23:58:03+09:00 Subject: Re: RubyDictionary - First Try >> 3 - >>>Why don't you allow one letter words? >> Because strings with one letter aren't any words, they are characters. >> My aim was to develop a dictionary where you can organize "real-life" >> words > > "a" is a word :-) This is a good argument, I was obviously too focused on my native language :) >>>if (match!=nil && match[0].length == word.length) then true >> >>>def self.is_word? word >>> �word.match(/\A[:alpha:][:alpha:]+\z/) #you can remove the first >>>[:alpha:] to allow 1-letter words >>>end >> >> I wanted to limit the word's length to 15 as you can see in my regular >> expression. Are the "\A" (start position) and "\z" (end position) >> necessary? > > It's a way of saying that the full string should match your > restrictions. It avoids having to check that the match is equal to the > length of the original string: > > def self.is_word? word > word.match(/\A[:alpha]{2,15}\z/) > end > > should do what you want. Oh, now I understand. Checking the length of the original string was a work-a-round, because I did not know about this regexp feature :) >> Do you mean to avoid exceptions when the words are frozen? If yes, I >> would throw an Exception if either of the two words is frozen. > > No, what I mean is that the client can modify the String object after > inserting it in the dictionary: You surely mean the DictionaryEntry > @word1 = word1.dup unless word1.frozen? > @word2 = word2.dup unless word2.frozen? > > The frozen check is just an optimization, because if the string is > frozen it's not going to be modified by the client, and so you are > safe just using it. > If you assume that word1 is frozen, then @word1 would be assigned to nil, wouldn't it? Or is the unless refering to the - and exclusively - to the ".dup"? > If it's just for > convenience, I think you should drop it, because it's giving those > objects a behaviour that they shouldn't have. Do you mean that the boolean equality operator (==) normally exclusively compares with the class it is defined by? > Another idea: if want to see the dictionary as a set of things, a > common thing is to include Enumerable and provide an each method, so > that the clients of the dictionary can use the cool methods Enumerable > provides. Nice idea, I implemented this. Last question: I posted this code on an other ruby forum and was criticized about my coding style: Indents are always done with two whitespaces rather than with tabs like I have. Have you heard anything about such a "unwritten rule"? Thanks -- Posted via http://www.ruby-forum.com/.