RUBY EVAL FUNCTION

K

kevid

Hi all,

I have the code below from "mechanize gem" [ the file is module.rb].
am a newbie in ruby. Could anyone help me redesign the code below to
use either
(a) block eval OR (b) not use eval at all.

Thoughts ?

class Module
def attr_finder(*syms)
syms.each do |sym|
class_eval %{ def #{sym.to_s}(hash = nil)
if hash == nil
@#{sym.to_s}
else
@#{sym.to_s}.find_all do |t|
found = true
hash.each_key \{ |key|
found = false if t.send(key.to_sym) !=
hash[key]
\}
found
end
end
end
}
end
end
end
 
B

Brian Candler

It's pretty reasonable to use eval in the code you provide. I think the
method it defines will be faster because there is no closure created.

But if you really want to avoid it:

class Module
def attr_finder(*syms)
syms.each do |sym|
define_method(sym) do |*args|
val = instance_variable_get("@#{sym}")
return val if args.empty?
hash = args.first
val.find_all do |t|
found = true
hash.each_key { |key|
found = false if t.send(key) != hash[key]
}
found
end
end
end
end
end

# Test
class Foo
attr_finder :bar
end

f = Foo.new
f.instance_eval { @bar = ["a","bb","cc"] }
puts f.bar
puts f.bar:)length => 2)

I would also be inclined to simplify your finder loop to this:

hash = args.first
val.find_all { |t| !hash.find { |k,v| t.send(k) != v } }
 
I

Intransition

It's pretty reasonable to use eval in the code you provide. I think the
method it defines will be faster because there is no closure created.

+1

In cases like this, when you can use a string eval, it is usually the
best approach. There are cases where you cannot do this b/c you need
access to a local variable.
 
K

kevid

Thanks all.


Brian Candle solution just did for me.

Please, not to be asking too much, I need to make use of "Hpricot gem"
files. I don't want to install it as a gem. I copied lib/hpricot/*{all
files} and lib/hpricot.rb to my application.

below is lib/hpricot.rb

begin
require 'encoding/character/utf-8'
rescue LoadError
end

#require 'hpricot_scan'
require 'hpricot/tag'
require 'hpricot/modules'
require 'hpricot/traverse'
require 'hpricot/inspect'
require 'hpricot/parse'
require 'hpricot/builder'

ALL files loaded properly except "hpricot_scan". i can't find anything
like hpricot_scan.rb in the gem's folder.

Any help is highly appreciated.
 
K

kevid

Thanks all.


Brian Candle solution just did for me.

Please, not to be asking too much, I need to make use of "Hpricot gem"
files. I don't want to install it as a gem. I copied lib/hpricot/*{all
files} and lib/hpricot.rb to my application.

below is lib/hpricot.rb

begin
require 'encoding/character/utf-8'
rescue LoadError
end

#require 'hpricot_scan'
require 'hpricot/tag'
require 'hpricot/modules'
require 'hpricot/traverse'
require 'hpricot/inspect'
require 'hpricot/parse'
require 'hpricot/builder'

ALL files loaded properly except "hpricot_scan". i can't find anything
like hpricot_scan.rb in the gem's folder.

Any help is highly appreciated.
 
R

Roger Pack

ALL files loaded properly except "hpricot_scan". i can't find anything
like hpricot_scan.rb in the gem's folder.

doing a
$ gem which hpricot_scan

might help you locate it.
-rp
 
K

kevid

doing a
$ gem which hpricot_scan

might help you locate it.
-rp

thanks,,

But this provided "hpricot_scan.so" {i.e /usr/lib/ruby/gems/1.8/gems/
hpricot-0.8.2/lib/hpricot_scan.so }

I have added this file but no luck
 
R

Roger Pack

But this provided "hpricot_scan.so" {i.e /usr/lib/ruby/gems/1.8/gems/
hpricot-0.8.2/lib/hpricot_scan.so }

I have added this file but no luck

make sure it's in the load path ($:) or add it explicitly, like 'require
/hpricot_scan' or what not.
-rp
 

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,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top