module_eval: create method that contains value passed to it?

H

Henrik

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/igothN18.html
 
J

James Britt

Henrik said:
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.ruby-doc.org - Ruby Help & Documentation
http://www.artima.com/rubycs/ - Ruby Code & Style: Writers wanted
http://www.rubystuff.com - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com - Playing with Better Toys
http://www.30secondrule.com - Building Better Tools
 
R

Robert Klemme

Henrik said:
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/igothN18.html

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
=> "qwert"

Kind regards

robert
 
A

ara.t.howard

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/igothN18.html

this can be done with closures or a storage/search-path scheme. the traits
lib uses the latter, see here:

http://codeforpeople.com/lib/ruby/traits/

-a
--
===============================================================================
| ara [dot] t [dot] howard [at] noaa [dot] gov
| strong and healthy,
| who thinks of sickness until it strikes like lightning?
| preoccupied with the world,
| who thinks of death, until it arrives like thunder?
| -- milarepa
===============================================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top