Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blocks and objects

I have an object like this

class SomeObject
  def initialize &block
    # do something
  end
end

class AnotherObject < SomeObject
  def initalize &block
    super
    # do something with block
  end
end

When super is called in AnotherObject, the block seems to be passed to SomeObject. Is this the right behaviour and is there away round it?

like image 622
NebulaFox Avatar asked Feb 01 '12 17:02

NebulaFox


People also ask

What is an object block?

Block objects are a C-level syntactic and runtime feature that allow you to compose function expressions that can be passed as arguments, optionally stored, and used by multiple threads. The function expression can reference and can preserve access to local variables.

What is difference between block and object storage?

Object storage is best used for large amounts of unstructured data, especially when durability, unlimited storage, scalability, and complex metadata management are relevant factors for overall performance. Block storage provides low latency and high-performance values in various use cases.

What is block file 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. Containers are highly flexible and bring incredible scale to how apps and storage are delivered.

What are examples of block storage?

The most common examples of Block Storage are SAN, iSCSI, and local disks. Block storage is the most commonly used storage type for most applications. It can be either locally or network-attached and are typically formatted with a file system like FAT32, NTFS, EXT3, and EXT4.


1 Answers

According to rubyspec this is the correct behaviour, even if you pass explicit arguments to super (i.e. super('foo'))

If you don't want to pass that block, you could just pass a block that does nothing, although this isn't quite the same thing (e.g. if the method changes its behaviour based on block_given?)

It appears that

super(&nil)

is a way to pass no block at all to super, although I couldn't find this in ruby spec.

like image 188
Frederick Cheung Avatar answered Nov 06 '22 06:11

Frederick Cheung