Inserting surrounding scope into singelton method definition

S

Sebastia

I have a sorted array (in this case, an ActiveRecord result set) that i
want to give awareness of its sorting state. I defined a singelton
method on the array but i can't figure out how to insert a value from
the surrounding scope. Below is the best i could come up with, but it
seems quite horrid (and using plain eval would be even worse i
suppose). I seem to remember that there was a better way... Anybody?

Thanks,
Sebastian

sort_term = "label"
results = User.find:)all, :eek:rder => sort_term)

class << results
def sorted_by?
@sorted_by_term
end
end

results.instance_variable_set("@sorted_by_term",sort_term)
 
D

dblack

Hi --

I have a sorted array (in this case, an ActiveRecord result set) that i want
to give awareness of its sorting state. I defined a singelton method on the
array but i can't figure out how to insert a value from the surrounding
scope. Below is the best i could come up with, but it seems quite horrid (and
using plain eval would be even worse i suppose). I seem to remember that
there was a better way... Anybody?

Thanks,
Sebastian

sort_term = "label"
results = User.find:)all, :eek:rder => sort_term)

class << results
def sorted_by?
@sorted_by_term
end
end

results.instance_variable_set("@sorted_by_term",sort_term)

To get through the barrier of the class and def scopes, you can use
class_eval and define_method:

(class << results; self; end).class_eval do
define_method:)sorted_by?, sort_term)
end

or something along those lines.


David

--
Q. What's a good holiday present for the serious Rails developer?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
aka The Ruby book for Rails developers!
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 
S

Sebastian

Wonderful! For some reason the 2-argument notation in your example
didn't work for me, but with a Proc as the method definition body all
is dandy. I had tried class_eval before but ignorantly used it on the
array instance instead, which of course doesnt work. So, thanks very
much for your help.

Here's the final code:

returning find:)all, :eek:rder => "#{sort_by_term} #{sort_order_term}") do
|results|
(class << results; self; end).class_eval do
define_method:)sorted_by?) { sort_by_term }
define_method:)sort_order?) { sort_order_term }
end
end
 
D

dblack

Hi --

Wonderful! For some reason the 2-argument notation in your example didn't
work for me, but with a Proc as the method definition body all is dandy. I
had tried class_eval before but ignorantly used it on the array instance
instead, which of course doesnt work. So, thanks very much for your help.

Yes, I used the wrong construct -- I'm glad you figured it out :)


David

--
Q. What's a good holiday present for the serious Rails developer?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
aka The Ruby book for Rails developers!
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 
A

ara.t.howard

I have a sorted array (in this case, an ActiveRecord result set) that i want
to give awareness of its sorting state. I defined a singelton method on the
array but i can't figure out how to insert a value from the surrounding
scope. Below is the best i could come up with, but it seems quite horrid (and
using plain eval would be even worse i suppose). I seem to remember that
there was a better way... Anybody?

Thanks,
Sebastian

sort_term = "label"
results = User.find:)all, :eek:rder => sort_term)

class << results
def sorted_by?
@sorted_by_term
end
end

results.instance_variable_set("@sorted_by_term",sort_term)

why not just use an attr

results = User.find :all, :eek:rder => sort_term

class << results
attr_accessor 'sorted_by'
alias_method 'sorted_by?', 'sorted_by'
end

results.sorted_by = "label"

othewise you need to use module_eval and define_method due to the scoping.

-a
 

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,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top