Ruby: No Block This Error

Am I continuing to get a "no block" error when trying to pass a string to is_tut? method. I'm new to Ruby and have no idea what I'm doing wrong. Any help would be appreciated.

class Tut @@consonants = ["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 yield else false end 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 #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 outout : 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. 
+8
ruby
source share
3 answers

If you have yield in the definition of your method, this means that you must pass the block when you use it (if the part, including the part, is not executed in accordance with the setting, etc.). (Perhaps you already know, but in case you do not: the block is what is described as {...} or do ... end ). And yield will refer to the block.

If you want to make the block optional, then one way to do this is to put the & symbol in front of the variable name.

 def method(argument, &block_argument) if block_argument # block is given block_argument.call(argument_for_block) # use call to execute the block else # the value of block_argument becomes nil if you didn't give a block # block was not given end end 

This will allow the use of an additional unit. Or, as suggested by Squeegy,

 def method(argument) if block_given? # block is given yield(argument_for_block) # no need to use call to execute the block else # block was not given end end 

will also work.

+15
source share

Since you are calling yield in your to_tut() method, this line will not execute:

 tut = Tut.to_tut( "Wow! Look at this get converted to Tut!" ) 

You either need to provide a block (as in the first call to Tut.to_tut() ), or you need to change the to_tut() function to make the code block optional :

 def self.to_tut string string.each_char do |c| c += "ut" if @@consonants.find { |i| i == c.downcase } yield c if block_given? end end 
+1
source share

yield requires passing the to_tut block.

When you do:

 Tut.to_tut( "Wow! Look at this get converted to Tut!" ) { |c| print c } 

This works because it has a block { |c| print c } { |c| print c } .

Without a block, this will result in an error.

0
source share

All Articles