Ruby eval function

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

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 } }

On Apr 27, 8:01 am, Brian C. [email protected] wrote:

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.

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.

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

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.

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

On Apr 27, 6:27 pm, Roger P. [email protected] wrote:

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

Posted viahttp://www.ruby-forum.com/.

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs