Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > some method help ... not really sure about proper title

Reply
Thread Tools

some method help ... not really sure about proper title

 
 
Lex Williams
Guest
Posts: n/a
 
      08-19-2008
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!
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Jesús Gabriel y Galán
Guest
Posts: n/a
 
      08-20-2008
On Tue, Aug 19, 2008 at 10:23 PM, Lex Williams <(E-Mail Removed)> wrote:
> 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 ?


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.

 
Reply With Quote
 
 
 
 
Lex Williams
Guest
Posts: n/a
 
      08-20-2008
That was exactly what I was looking for!
Thank you very much !
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the Proper Method to Upload Long File - WEBDAV using PUT Method Jitendra Sanghani ASP .Net 1 03-19-2012 09:36 AM
Dynamic class thingies? (Okay, not sure how to title this one) coigner Ruby 8 03-08-2008 05:15 PM
sure shot requirement for C++ with Unix Experience !!! sure interviewto day !!! Himayat66@gmail.com ASP .Net 0 02-07-2008 12:40 AM
CGI tags HTML problem, not really sure Mjolinor HTML 4 10-10-2005 07:18 AM
Rendering proper case/title case on bound data in datagrid? ASP .Net Web Controls 1 12-25-2004 01:27 AM



Advertisments