Need regex to check for consonants and put number after each consonant

Hello,

im working on code to check if a character is a consonant or not and if
it is to put a number after each one. This is what ive got so far.

class Num

def to_num( c )
c.each do |c|
if c =~ /![aeiou]/
c.join( “0” )
end
end
end

end

Num.to_num( “Wow! Look at this get a number after each letter!” ) { |c|
print c}

I keep getting the undefined method to_num for Num:class

Any suggestions on what i can do to get this to work? Any help is
appreciated! Thanks!

Hi,

this cannot work. First of all, to_num() is an instance method and not a
class method. You cannot call it on Num, but you’d have to create an
instance first and then call it on this object. Secondly, the method
doesn’t take a block, so the {|c| print c} is useless code. And the
method itself also looks strange. You keep overwriting the “c” variable
with completely different objects, I’m not even sure what will happen.
And the regex syntax is wrong.

So you want to put a “0” after each consonant? Then simply use
String#gsub:

str = ‘Wow! Look at this get a number after each letter!’
puts str.gsub(/(?<=[bcdfghj-np-tvwxz])/i, ‘0’)

Stefano C. wrote in post #1082269:

Also note that your regexp won’t work as you expected. To match a
character
other than those included in a list, you need to put a ^ at the
beginning of
the list:
if c =~ /[^aeiou]/

This matches any character that’s not a vowel, for example digits or
special chars $, !, %, … or whitespace etc.

If you want consonants, you either have to explictly write them down
(like I did in the previous post) or use the intersection of all
alphabetic characters [a-z] with the non-vowels [^aeiou]:

consonants = /[a-z&&[^aeiou]]/i

But since this syntax isn’t well-known, I’d probably just write down the
characters.

On Thursday 01 November 2012 Justin Gamble wrote

    c.join( "0" )

Any suggestions on what i can do to get this to work? Any help is
appreciated! Thanks!


Posted via http://www.ruby-forum.com/.

That’s because to_num is an instance method (that is, a method defined
on
instances of class Num) rather than a method of the class itself. You
can
solve the problem in two ways:

  • create an instance of num and call its to_num method:
    n = Num.new
    n.to_num
  • define to_num as a class method:
    class Num
    def self.to_num #note the self!

Also note that your regexp won’t work as you expected. To match a
character
other than those included in a list, you need to put a ^ at the
beginning of
the list:
if c =~ /[^aeiou]/

I hope this helps

Stefano

I think you misunderstand how methods and blocks work. You continue to
write a block after the method calls, but your method doesn’t use blocks
(there’s no “yield”). So all the “print c” and “num_string += c” etc. is
simply ignored. It has no effect at all.

What your method does is return the result of the conversion. So you can
output the return value, store it in a variable etc.:

#----------------------------------
num = Num.new

convert to num

num_converted = num.to_num(“Wow! Look at this get converted to Num!”)
puts num_converted

convert back to English

puts num.to_english(num_converted)
#----------------------------------

Of course you could use blocks, but then you’d have to define your
method accordingly. Judging from your examples, it seems you want to
pass the conversion result to the block:

#----------------------------------
class Num
def to_num( string )
res = string.gsub( /(?<=[bcdfghj-np-tvwxz])/i, ‘0’ )
yield res if block_given? # pass result to block (if there is one)
res
end

def to_english( num_string )
res = num_string.gsub( /[0]/, ‘’ )
yield res if block_given? # pass result to block (if there is one)
res
end
end

num = Num.new
num.to_num( “Wow! Look at this get converted to Num!” ) do |result|
puts result
end
#----------------------------------

However, I can’t see any use for this block variant, because the method
doesn’t do anything useful with the block. So I’d simply return the
value like above and not use blocks.

Thank you so much for your help guys! That part worked like a charm!

Now how would I go about taking the 0 out of a sentence that is passed
thru to_num?

I have this:

class Num
def to_num( string )
string.gsub( /(?<=[bcdfghj-np-tvwxz])/i, ‘0’ )
end

def to_english( num_string )
num_string.gsub( /[0]/, ‘’ )
end
end

num= Num.new

puts num.to_num( “Wow! Look at this get converted to Num!” ) { |c| print
c}

num_string = “”
num.to_num( “I’m in num but I want to be in english.” ) { |c| num_string
+= c }
puts num_string

puts num.to_english( num_string ) { |c| print c }

its the same instance of the first created class.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs