On 5/8/08, globalrev <skanemupp / yahoo.se> wrote:
> On 8 Maj, 15:12, Heesob Park <pha... / gmail.com> wrote:
> > globalrev wrote:
> > > i then changed to !~ just like haesob said but if i do /aeiouy / it
> > > doesnt react to space " ". how do i get it to treat " " as "a"?
> >
It's all in the book...

From "Programming Ruby", in Standard Types/ Regular Expressions
/Character Classes:
" A character class is a set of characters between brackets: ...
[aeiou] will match a vowel ... addition, you can use the abbreviations
...  so that (for example) \s matches any whitespace character"
and a bit further down:
"Put a ^ immediately after the opening bracket to negate a character
class: [^a-z] matches any character that isn't a lowercase alphabetic"

That should give you all the information to build the expression you need:
c !~ [\saeiouy]
c
!~        #does not match
/[        #any
\s        #whitespace
aeiouy]   #or vowel ...

which is exactly what Park Heesob said:
> > You can do with like this:
> > if str[x].chr !~ /[\saeiouy]/

An equivalent version moves the 'not' inside the character class:
if str[x].chr =~ /[^\saeiouy]/

>>
> > BTW, your encryption equals to
> > str.gsub(/([^\saeiouy])/,"\\1o\\1")
>
> that doesnt seem to work, it repeats the sma ething alot of times
> befre next letter.
>
> if this is what u meant (why did u add the "s" ?):
>
> for x in (0..str.length()-1)
>   str = str.gsub(/([^\aeiouy])/,"\\1o\\1")
> end
> print str
>

That's not what he meant, check the docs for gsub:
"Returns a copy of str with *all* occurrences of pattern replaced with
either replacement or the value of the block".   So essentially,  gsub
does the loop over all the characters for you.   Your whole encode
function can be as simple as:

def encode str; str.gsub(/([^\saeiouy])/,"\\1o\\1"); end

I'd modify it to deal with case:
str.gsub(/([^\saeiouy])/i){|c|"#{c}o#{c.downcase}"}

will encode "Hi Oliver" into
"Hohi Ololivoveror", instead of
"HoHi OoOlolivoveror"

HTH,
-Adam