Ruby: No Block Given error

22,875

Solution 1

If you have yield within your method definition, that means you have to obligatorily pass a block when you use it (unless the part including it is not executed according to conditioning etc.). (You might already know, but in case you don't: a block is something that is described as {...} or do ... end) And yield will refer to the block.

If you want to make a block optional, then one way to do it is to put the & symbol before 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 optional block. 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.

Solution 2

Because you're calling yield in your to_tut() method, this line will fail:

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

You either need to give a block (as you did in the first commented-out call to Tut.to_tut()), or you need to modify your 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
Share:
22,875

Related videos on Youtube

James Titus
Author by

James Titus

Please delete me.

Updated on April 03, 2020

Comments

  • James Titus
    James Titus about 4 years

    I keep getting a 'no block given' error when trying to pass the string to the is_tut? method. I am new to Ruby and have no idea what I'm doing wrong. Any and all 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.
    
    • PJP
      PJP about 13 years
      Just as a tip, a more Ruby-ish way to define your consonants would be: @@consonants = ('a' .. 'z').to_a - %w[a e i o u]
  • Alex Wayne
    Alex Wayne about 13 years
    Or you could just use block_given? to something like yield if block_given?
  • sawa
    sawa about 13 years
    That's another good way. block_given checks if there is a block, and if not, it returns false, and yield will not be executed, which means it will not cause an error.
  • James Titus
    James Titus about 13 years
    Sweet!! That worked! Thanks everyone! I may be back here with more questions later. Appreciate the help.

Related