# Methods with \[\] braces can't take block arguments?

**URL:** <https://www.ruby-forum.com/t/methods-with-braces-cant-take-block-arguments/142958>\
**Category:** Ruby\
**Created:** [July 13, 2008, 10:46pm UTC](https://www.ruby-forum.com/t/methods-with-braces-cant-take-block-arguments/142958 "2008-07-13T22:46:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 13, 2008, 10:46pm UTC](https://www.ruby-forum.com/t/methods-with-braces-cant-take-block-arguments/142958/1 "2008-07-13T22:46:01Z")

</div>

Hi All,

I want to create a L2 cache object based on Hash.

Since it’s a secondary cache, the object bound to the key may have  
expired, but can be recreated.

However I want the get method itself to do get-get-set if the “value”  
object has expired. Check the class definition:

#---------  
class L2Cache

# use Hash for the store

def initialize  
@h = Hash.new  
end

# try the hash… if not found and a block

# to recreate is provided, create the value

# and bind it again

def get(key)  
value = @h[key]  
if value.nil? && block\_given?  
puts “value is nil but recreating it using the block” if $DEBUG  
value = self[key] = yield  
elsif value.nil?  
puts “value is nil but not recreate block found” if $DEBUG  
end  
value  
end

# proxy to hash

def   
@h[key]  
end

# proxy to hash

def []=(key, value)  
@h[key] = value  
end  
end  
#---------

c = L2Cache.new  
c[:k] # \>\> nil  
c.get(:k) { “foo”} #\>\> foo  
c[:foo] # \>\> foo

#----------------

However I cannot make the the default get method using [] take a block  
as an argument.

If I define the method as:  
#--------  
def   
value = @h[key]  
if value.nil? && block\_given?  
puts “value is nil but recreating it using the block”  
value = self[key] = yield  
elsif value.nil?  
puts “value is nil but not recreate block found”  
end  
value  
end  
#--------

I can’t use c[:k] { block here }

#--------  
c = L2Cache.new  
c[:k] # \>\> nil  
c.[:k] { “foo”} # \>\> syntax error, unexpected ‘{’, expecting  
$end  
#--------

Is there a way to make methods names with special characters take  
block arguments?

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 14, 2008, 11:11am UTC](https://www.ruby-forum.com/t/methods-with-braces-cant-take-block-arguments/142958/2 "2008-07-14T11:11:56Z")

</div>

hi!

removed\_email\_address@domain.invalid [2008-07-13 22:40]:

> block arguments?  
> well, the problem is not that the method name has special  
> characters, but that what you’re using is just syntactic sugar for  
> that particular method name. it works perfectly fine when you use  
> the regular syntax:

c. { ‘foo’ } # pretty ugly, eh? 😉  
c.get(:k) { ‘foo’ }

alternatively, you could pass in a proc argument like this:

c[:k, lambda { ‘foo’ }]

but you’d have to change your code to take that into account.

cheers  
jens
