# Some method help ... not really sure about proper title

**URL:** <https://www.ruby-forum.com/t/some-method-help-not-really-sure-about-proper-title/146304>\
**Category:** Ruby\
**Created:** [August 19, 2008, 10:26pm UTC](https://www.ruby-forum.com/t/some-method-help-not-really-sure-about-proper-title/146304 "2008-08-19T22:26:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lpc2148](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de053/32.png) [@lpc2148](https://www.ruby-forum.com/u/lpc2148)\
**Post date:** [August 19, 2008, 10:26pm UTC](https://www.ruby-forum.com/t/some-method-help-not-really-sure-about-proper-title/146304/1 "2008-08-19T22:26:22Z")

</div>

Hi guys!

I’m kinda new to ruby , and was wondering about the following stuff ,  
assume the following code :

Shoes.app {  
button(“Press Me”) { alert(“You pressed me”) }  
}

how could I write code like that , so that when I call a method like  
button , I don’t have to put an object or a class name in front of it .

To give another example , let’s assume the following code :

class Whatever  
attr\_accessor :value

def initialize  
yield self  
end

def some\_method  
puts “some method”  
end

end

I would like to do the following ( if it’s possible ) :

w = Whatever.new do |whatever|  
some\_method  
value = 20  
end

puts w.value # this should output 20

Is this possible , without adding those methods to Object ?

Thank you very much!

---

<div class="post-metadata">

**Author:** ![lpc2148](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de053/32.png) [@lpc2148](https://www.ruby-forum.com/u/lpc2148)\
**Post date:** [August 20, 2008, 9:42am UTC](https://www.ruby-forum.com/t/some-method-help-not-really-sure-about-proper-title/146304/2 "2008-08-20T09:42:22Z")

</div>

That was exactly what I was looking for!  
Thank you very much !

---

<div class="post-metadata">

**Author:** ![lpc2148](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de053/32.png) [@lpc2148](https://www.ruby-forum.com/u/lpc2148)\
**Post date:** [August 20, 2008, 9:30am UTC](https://www.ruby-forum.com/t/some-method-help-not-really-sure-about-proper-title/146304/3 "2008-08-20T09:30:37Z")

</div>

On Tue, Aug 19, 2008 at 10:23 PM, Lex W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> button , I don’t have to put an object or a class name in front of it .  
> def some\_method  
> end
> 
> puts w.value # this should output 20
> 
> Is this possible , without adding those methods to Object ?

Hi, if you use the following you can call certain methods like you want.

class Whatever  
attr\_accessor :value

def initialize &blk  
instance\_eval &blk  
end

def some\_method  
puts “some method”  
end  
end

This will allow to do:

w = Whatever.new {some\_method}

The only exception are the xxx= methods. This won’t work:

w = Whatever.new {value = 20}

The reason is that value= methods have to be called as self.value=20  
within the class, because it’s  
the only way for the interpreter to understand that you want a method  
call and not a local variable to the  
function. You could do:

w = Whatever.new {self.value = 20}

That works.

Hope this gives you some other ideas…

Jesus.
