# Question on blocks

**URL:** <https://www.ruby-forum.com/t/question-on-blocks/56558>\
**Category:** Ruby\
**Created:** [April 1, 2006, 3:14am UTC](https://www.ruby-forum.com/t/question-on-blocks/56558 "2006-04-01T03:14:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![reversejt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/41988e/32.png) [@reversejt](https://www.ruby-forum.com/u/reversejt)\
**Post date:** [April 1, 2006, 3:14am UTC](https://www.ruby-forum.com/t/question-on-blocks/56558/1 "2006-04-01T03:14:12Z")

</div>

I am new to ruby from java and had a question on blocks.

Can a block be applied to a writeable attribute or setter? I keep  
getting  
errors so I

am assuming no but I wanted to double check in case my syntax is  
incorrect.

Thanks for the help,

Jeff

class Test

def initialize(one, two)

```
@one = one

@two = two

```

end

def one=(one)

```
@one = one;

Yield;

```

end

attr\_reader :one, :two

attr\_writer :two

end

test = Test.new(“Hello”, 2);

test.one = 3 { puts “Test Block” }

---

<div class="post-metadata">

**Author:** ![reversejt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/41988e/32.png) [@reversejt](https://www.ruby-forum.com/u/reversejt)\
**Post date:** [April 1, 2006, 3:50am UTC](https://www.ruby-forum.com/t/question-on-blocks/56558/2 "2006-04-01T03:50:35Z")

</div>

Can anyone explain this: ?

class A  
def x  
yield  
end

```
 def x=a
     yield
 end

```

end

a=A.new  
a.x{p “called”}  
a.x=1 do  
p “called”  
end

…/test.rb:13: parse error, unexpected kDO, expecting $

lopex

---

<div class="post-metadata">

**Author:** ![reversejt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/41988e/32.png) [@reversejt](https://www.ruby-forum.com/u/reversejt)\
**Post date:** [April 1, 2006, 4:02am UTC](https://www.ruby-forum.com/t/question-on-blocks/56558/3 "2006-04-01T04:02:46Z")

</div>

Patrick,

Thanks for the quick response. I agree that it doesn’t make sense  
unless I was passing the new value back to the block. I am about  
15 minutes into the Picaxe and was just trying to get a handle on the  
syntax. The language looks great. My brain is just hardwired with java  
syntax at this point 🙂

Cheers,  
Jeff

---

<div class="post-metadata">

**Author:** ![reversejt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/41988e/32.png) [@reversejt](https://www.ruby-forum.com/u/reversejt)\
**Post date:** [April 1, 2006, 3:50am UTC](https://www.ruby-forum.com/t/question-on-blocks/56558/4 "2006-04-01T03:50:40Z")

</div>

On 3/31/06, Jeff T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> attr\_reader :one, :two
> 
> test = Test.new(“Hello”, 2);
> 
> test.one = 3 { puts “Test Block” }

You are correct it is not allowed, but I am curious why you want to do  
this? Also if I were to go and write a setter that took a block,  
wouldn’t you either pass the new value into the block or use the  
result of the block to set the variable?

class Test  
def a=(a)  
@a = a  
yield @a if block\_given?  
end

#or  
def b=(b)  
@b = (block\_given?) ? yield b : b  
end  
end

pth

---

<div class="post-metadata">

**Author:** ![reversejt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/41988e/32.png) [@reversejt](https://www.ruby-forum.com/u/reversejt)\
**Post date:** [April 1, 2006, 4:05am UTC](https://www.ruby-forum.com/t/question-on-blocks/56558/5 "2006-04-01T04:05:47Z")

</div>

On 3/31/06, Patrick H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> end
> 
> #or  
> def b=(b)  
> @b = (block\_given?) ? yield b : b  
> end  
> end
> 
> pth

Sorry just to be clear, I do not think that method= methods are  
allowed to take anything more than a single parameter – when using  
the normal “call” syntax.

Of course you can send a message to it with a block:

class A  
attr\_reader :a  
def a=(a,&b)  
@a = block\_given? ? yield(a) : a  
end  
end

a=A.new  
a.a = 7  
p a.a  
a.a = 1  
bl = Proc.new { |v| v \* 6 }  
a.send “a=”, 7, &bl  
p a.a
