Tut II

100 points

Tut II

Requirements

is_tut uses a regular expression to validate strings as tut or not tut20
to_tut yields tut when called with with tut and a block10
to_english yields english when called with english and a block10
to_tut returns tut when called without a block20
to_english returns english when called without a block20
to_tut returns nil when called with tut and no block10
to_english returns nil when called with english and no block10
Total100 pts

Resources

Objective

To practice working with regular expressions and block_given?

Instructions

For this lab you will be modifying your Tut class from the previous lab to give it the ability to return Tut or English from method calls in addition to yielding to blocks and to be able to recognize properly formatted tut strings.

To start this lab, create a regular expression that can be used to identify a proper tut string and include it in a new Tut class method called is_tut?. The is_tut? method should be passed a string which it should then compare with your 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.

Next, modify your to_tut method so that it checks the string it's passed to see if it's already in tut. If the passed string is already in tut then the string should be given one character at a time to the method's block without being further modified. If the string passed to the to_tut method is not already in tut then it should be converted to tut and given one character at a time to the block (just like in the last lab)

Next, modify your to_english method in the same way as the to_tut method so that it will only convert the passed string to english if it isn't already in english but will give the characters of the english string to the block one at a time.

Finally, modify both the to_tut and to_english methods so that they can both be called without a block. When either method is called with appropriate input it should return the converted output as a string. When either method is called with invalid input it should return nil.

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 outout : Wutowut! Lutookut atut tuthutisut gutetut cutonutvuteruttutedut tuto Tututut!

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

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

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

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.

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