Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby " yield row if block_given?" [duplicate]

Tags:

ruby

  # Get our data back
  def queryNewsTable
    @conn.exec( "SELECT * FROM newslib" ) do |result|
      result.each do |row|
        yield row if block_given?
      end
    end
  end

For this piece of code. I don't quite understand yield row if block_given?

can anybody point to any good article teaching about this or you can briefly explain it to me a little bit thanks so much

like image 979
BufBills Avatar asked Feb 09 '14 04:02

BufBills


People also ask

Does yield return Ruby?

The yield keyword instructs Ruby to execute the code in the block. In this example, the block returns the string "yes!" .

What is &Block in Ruby?

The &block is a way of sending a piece of Ruby code in to a method and then evaluating that code in the scope of that method.

How do you yield in Ruby?

To pass any argument from yield to the block we can write || inside the block as the name of the argument. {|number| puts “Your message”} and yield will call as yield 5. So here 5 will be the value for the |number|.

How do you pass blocks in Ruby?

In Ruby, methods can take blocks implicitly and explicitly. Implicit block passing works by calling the yield keyword in a method. The yield keyword is special. It finds and calls a passed block, so you don't have to add the block to the list of arguments the method accepts.


3 Answers

This yield row if block_given? means that block which could be passed into the #queryNewsTable method(!), is evaluated with yield operator, in other words, if you pass the block into function #queryNewsTable:

queryNewsTable do 
   #some code
end

You will get the call to the code, for each of rows in the result variable.

NOTE: That for your case it will be better to optimize the code (if not dbtrigger is used):

# Get our data back
def queryNewsTable
  @conn.exec( "SELECT * FROM newslib" ) do |result|
    result.each do |row|
      yield row
    end
  end if block_given?
end   
like image 142
Малъ Скрылевъ Avatar answered Oct 13 '22 00:10

Малъ Скрылевъ


Ask yourself how Hash.new works:

http://www.ruby-doc.org/core-2.1.0/Hash.html#method-c-new

It can take no argument, a single argument, or a block. If there is no argument, fetching the value for a non-existent key gives nil. If there is a block, fetching the value for a non-existent key gives whatever the block says to give. Clearly its implementation needs a way to ask "was there a block?" so that it knows which behavior to use. That is what block_given? does.

http://www.ruby-doc.org/core-2.1.0/Kernel.html#method-i-block_given-3F

As for yield, it is simply the way a method that has taken a block calls the block, passing it parameters as needed.

like image 43
matt Avatar answered Oct 12 '22 23:10

matt


When you use functions like Enumerable#each, you typically pass in a block using {|arg| ... } or do ... end. The line yield row if block_given? checks to see if a block was supplied to this function call, and if it was, calls that block with row as an argument.

One way you might use this function would be:

queryNewsTable {|row| puts row}

This example would print each row of the query to standard output, since the block that does the printing gets called for each row in the query result. If no block were given, then that line would be ignored.

like image 29
derekerdmann Avatar answered Oct 12 '22 22:10

derekerdmann