# Yield self if block\_given?

**URL:** <https://www.ruby-forum.com/t/yield-self-if-block-given/180948>\
**Category:** Ruby\
**Created:** [January 1, 2010, 3:57pm UTC](https://www.ruby-forum.com/t/yield-self-if-block-given/180948 "2010-01-01T15:57:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [January 1, 2010, 3:57pm UTC](https://www.ruby-forum.com/t/yield-self-if-block-given/180948/1 "2010-01-01T15:57:04Z")

</div>

class AngstyNamedPerson  
attr\_reader :name

@@hated\_names = %w(Leroy Sparkles Thaddius)

def name=(new\_name)  
@name = new\_name  
raise “I have probems with being named #{@name}.” if  
@@hated\_names.include? new\_name  
end

def initialize(name)  
@name = name  
end

def transactionally(&block)  
old\_name = @name  
begin  
yield self if block\_given?  
rescue  
@name = old\_name  
end  
end  
end

mr\_pibbles = AngstyNamedPerson.new(‘Mr. Pibbles’)

–  
In the code above, what’s “yied self if block\_given?” in the function  
of transactionally?  
I can’t understand for the “self”.

Please help with it.  
Thanks.

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [January 1, 2010, 4:19pm UTC](https://www.ruby-forum.com/t/yield-self-if-block-given/180948/2 "2010-01-01T16:19:53Z")

</div>

I tried below, it can work,

irb(main):001:0\> class Myclass  
irb(main):002:1\> def mya  
irb(main):003:2\> yield self if block\_given?  
irb(main):004:2\> end  
irb(main):005:1\> def myb  
irb(main):006:2\> puts “it’s myb”  
irb(main):007:2\> end  
irb(main):008:1\> end  
=\> nil  
irb(main):009:0\>  
irb(main):010:0\* x=Myclass.new  
=\> #Myclass:0x83f81b0  
irb(main):012:0\> x.mya { puts “a”}  
a  
=\> nil  
irb(main):013:0\> x.mya { |c| c.myb }  
it’s myb  
=\> nil

* * *

in the code above, what’s “c” there?  
does it mean the instance of x itself?

Thanks again.

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [January 1, 2010, 4:54pm UTC](https://www.ruby-forum.com/t/yield-self-if-block-given/180948/3 "2010-01-01T16:54:59Z")

</div>

Hi

> in the code above, what’s “c” there?  
> does it mean the instance of x itself?

Yes it does.  
Try  
x.mya {|c| p c}

* * *

Ayumu AIZAWA

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [January 2, 2010, 2:54am UTC](https://www.ruby-forum.com/t/yield-self-if-block-given/180948/4 "2010-01-02T02:54:19Z")

</div>

Hi,

Am Freitag, 01. Jan 2010, 23:54:12 +0900 schrieb Ruby N.:

> ```
> @name = name
> 
> ```
> 
> end
> 
> mr\_pibbles = AngstyNamedPerson.new ‘Mr. Pibbles’
> 
> In the code above, what’s “yied self if block\_given?” in the function  
> of transactionally?  
> I can’t understand for the “self”.

I would like to tell it superfluous as in all regular cases the  
caller yet has the object in a variable.

p.transactionally { |p\_|  
p.equal? p\_ or raise “wrong library”  
}

It would rather make sense to check the new name before setting  
the instance variable, instead of resetting it after the caller  
changed it in some obscure place. Here’s what I would have done:

def check name  
some\_condition or raise “I have probems with being named  
#{new\_name}.”  
end  
private :check

def name= new\_name  
check new\_name  
@name = new\_name  
end

def transactionally tmp\_name  
check tmp\_name  
old\_name, @name = @name, tmp\_name  
begin  
yield  
ensure  
@name = old\_name  
end  
end

Everything untested.

Bertram
