Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between block and &block in Ruby

Tags:

ruby

People also ask

What is difference between block and file?

In block storage, data is stored in blocks, whereas, in file storage, data is stored as files in a single piece. A block is not a complete file, so integration is not a property of the block, but in file storage, you can integrate data in different folders.

What is difference between block and object storage?

File storage organizes and represents data as a hierarchy of files in folders; block storage chunks data into arbitrarily organized, evenly sized volumes; and object storage manages data and links it to associated metadata.

What is Page and block?

A block is the smallest unit of data that an operating system can either write to a file or read from a file. What exactly is a page? Pages are used by some operating systems instead of blocks. A page is basically a virtual block. And, pages have a fixed size – 4K and 2K are the most commonly used sizes.

What is a block in a file?

Blocks are fixed-length chunks of data that are read into memory when requested by an application. Block storage is used in SAN environments to deliver high-performance application storage and is characterized by a mapping of data blocks to the underlying disk drives.


block is just a local variable, &block is a reference to the block passed to the method.

def foo(block = nil)
  p block
end

foo # => nil
foo("test") # => test
foo { puts "this block will not be called" } # => nil

def foo(&block)
  p block
end

foo # => nil
foo("test") # => ArgumentError: wrong number of arguments (1 for 0)
foo { puts "This block won't get called, but you'll se it referenced as a proc." }
# => #<Proc:0x0000000100124ea8@untitled:20>

You can also use &block when calling methods to pass a proc as a block to a method, so that you can use procs just as you use blocks.

my_proc = proc {|i| i.upcase }

p ["foo", "bar", "baz"].map(&my_proc)
# => ["FOO", "BAR", "BAZ"]

p ["foo", "bar", "baz"].map(my_proc)
# => ArgumentError: wrong number of arguments (1 for 0)

The variable name block doesn't mean anything special. You can use &strawberries if you like, the ampersand is the key here.

You might find this article helpful.


In an argument list, &whatever takes the block that was passed to the method and wraps it in a Proc object. The Proc is stored in a variable called whatever (where that can be whatever name you typed after the ampersand, of course — usually it's "block"). After a method call, the &whatever syntax turns a Proc into a block. So if you define a method like so:

def thing(&block)
  thing2 &block
end

You're defining a method that takes a block and then calls another method with that block.


If you don't set the & before block, Ruby won't recognize it's relationship to the "block" you pass to the function. Here some examples.

def f(x, block); end
f(3) { 2+2 }    # gives an error, because "block" is a
                # regular second argument (which is missing)

def g(x, &block); end
g(3) { 2+2 }    # legal

def h(x); end
h(3) { 2+2 }    # legal

For later use in a function:

def x(&block)   # x is a 0 param function
  y(block)      # y is a 1 param function (taking one "Proc")
  z(&block)     # z is a 0 param function (like x) with the block x received
end

So, if you call z(&block) it's (nearly!!) the same as calling z { yield }: You just pass the block to the next function.