[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

module_eval: create method that contains value passed to it?

Henrik Nyh

1/7/2006 7:49:00 PM

This is a Rails example, but I think the problem is a general Ruby
matter.

I am using class_eval within a class method, foobar(), to have it create
an instance method, get_foobar(). I am passing a value ("testing testing
123") to foobar() that I want to make part of get_foobar(). However, I
can't seem to include this value in the output of get_foobar().

Where I want the "render" line to output "A start testing testing 123
end O", it only outputs "A start end O". Extremely grateful for any
help. Code:

class HelpController < ApplicationController

def self.foobar(v)

class_eval %q{
def get_foobar()
"start #{v} end"
end
}

end

foobar "testing testing 123"

def index
render :text => "A "+get_foobar+" O"
end

end

Or syntax highlighted here: http://rafb.net/paste/results/igo...

--
Posted via http://www.ruby-....


3 Answers

james_b

1/7/2006 9:34:00 PM

0

Henrik wrote:
> This is a Rails example, but I think the problem is a general Ruby
> matter.

Perhaps, but it would be more helpful to isolate the problem by removing
all Rails influence, since there is every chance that Rails is munging
up core Ruby behavior.


But I think the problem is simply that you are not quoting your strings
correctly:

class HelpController
def self.foobar(v)
class_eval %Q{
def get_foobar()
"start #{v} end"
end
}
end

foobar "testing testing 123"
def index
puts "A " + get_foobar + " O"
end
end

HelpController.new.index # A start testing testing 123 end O

(Big Q, not little q)


I have no idea if this works on the railroad.


James



--

http://www.ru... - Ruby Help & Documentation
http://www.artima.c... - Ruby Code & Style: Writers wanted
http://www.rub... - The Ruby Store for Ruby Stuff
http://www.jame... - Playing with Better Toys
http://www.30seco... - Building Better Tools


Robert Klemme

1/7/2006 10:43:00 PM

0

Henrik <henrik@nyh.se> wrote:
> This is a Rails example, but I think the problem is a general Ruby
> matter.
>
> I am using class_eval within a class method, foobar(), to have it
> create an instance method, get_foobar(). I am passing a value
> ("testing testing 123") to foobar() that I want to make part of
> get_foobar(). However, I can't seem to include this value in the
> output of get_foobar().
>
> Where I want the "render" line to output "A start testing testing 123
> end O", it only outputs "A start end O". Extremely grateful for any
> help. Code:
>
> class HelpController < ApplicationController
>
> def self.foobar(v)
>
> class_eval %q{
> def get_foobar()
> "start #{v} end"
> end
> }
>
> end
>
> foobar "testing testing 123"
>
> def index
> render :text => "A "+get_foobar+" O"
> end
>
> end
>
> Or syntax highlighted here:
> http://rafb.net/paste/results/igo...

You don't even need class_eval - a simple closure is sufficient:

class X
def self.foobar(x)
define_method(:get_foobar) { x }
end

foobar "testing testing 123"
end

>> x=X.new
=> #<X:0x101c1198>
>> x.get_foobar
=> "testing testing 123"
>> X.foobar "qwert"
=> #<Proc:0x101c8530@(irb):3>
>> x.get_foobar
=> "qwert"

Kind regards

robert

Ara.T.Howard

1/10/2006 12:21:00 AM

0