Help with regular expression and block given

Ruby validate string and return string for output

Page 1 of 1

2 Replies - 1847 Views - Last Post: 13 April 2010 - 12:02 PM Rate Topic: -----

#1 bellagirl   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-March 10

Help with regular expression and block given

Posted 23 March 2010 - 07:53 PM

I need help with some coding. Most of the program works, but I am not sure how to validate and return a string to print. Here is my code:

What should happen: The is_tut? method is passed a string - it is compared with the regular expression to find if the string is 'in_tut' or not. If the string is 'in tut' then the method should return true, otherwise it should return false.

'to_tut' returns 'tut' when called without a block
'to_english' returns 'english' when called without a block
'to_tut' returns nil when called with 'tut' and no block
'to_english' returns nil when called with 'english' and no block

class Tut
  	@@consonants = %w{b c d f g h j k l m n p q r s t v w x y z}
    
  def is_tut? string
    if string =~ /^(([b-df-hj-np-z]ut)|([aeiou\s])|[[:punct:]])+$/i
        true
    else
        false
    end
  end
  
    if is_tut? s
      puts "it matches"
    end

	def self.to_tut string
		string.each_char do |c|
			c += "ut" if @@consonants.find {|i| i == c.downcase }
			yield c
		end
	end
	
	def self.to_english string
		array = string.split //		
		array.each do |c|
			if @@consonants.find {|i| i == c.downcase}
				array.shift
				array.shift
			end
			yield c
		end
	end
end

#message = ""
#Tut.to_tut( "Wow! Look at this get converted to Tut!" ) {|c| message += c}
#puts message

#Tut.to_english( message ) {|c| print c}
puts

####
#Sample code. Your program should produce the same output as what is commented below using these statements.
Tut.to_tut( "Wow! Look at this get converted to Tut!" ) { |c| print c }
# should output : Wutowut! Lutookut atut tuthutisut gutetut cutonutvuteruttutedut tuto Tututut!
puts
puts

#tut = Tut.to_tut( "Wow! Look at this get converted to Tut!" )
#puts "from return: #{tut}"

#puts

Tut.to_tut( "Wutowut! Lutookut atut tuthutisut gutetut cutonutvuteruttutedut tuto Tututut!" ) { |c| print c }
# should output : Wutowut! Lutookut atut tuthutisut gutetut cutonutvuteruttutedut tuto Tututut!
puts
puts

#tut = Tut.to_tut( "Wutowut! Lutookut atut tuthutisut gutetut cutonutvuteruttutedut tuto Tututut!" )
#puts "from return: #{tut}"

#puts

tut_string = ""
Tut.to_tut( "I'm in tut but I want to be in english." ) { |c| tut_string += c }
puts tut_string
# should output : I'mut inut tututut bututut I wutanuttut tuto bute inut enutgutlutisuthut.

puts

Tut.to_english( tut_string ) { |c| print c }
# should output : I'm in tut but I want to be in english.



I would appreciate any help!

Is This A Good Question/Topic? 0
  • +

Replies To: Help with regular expression and block given

#2 bellagirl   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-March 10

Re: Help with regular expression and block given

Posted 23 March 2010 - 09:43 PM

Deadline. Still need would like to have help with this one though.
Thanks.

This post has been edited by bellagirl: 23 March 2010 - 10:04 PM

Was This Post Helpful? 0
  • +
  • -

#3 MitkOK   User is offline

  • D.I.C Regular
  • member icon

Reputation: 32
  • View blog
  • Posts: 403
  • Joined: 09-August 07

Re: Help with regular expression and block given

Posted 13 April 2010 - 12:02 PM

http://apidock.com/r.../block_given%3F
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1