# Ruby eval function

**URL:** <https://www.ruby-forum.com/t/ruby-eval-function/197132>\
**Category:** Ruby\
**Created:** [April 27, 2010, 1:30pm UTC](https://www.ruby-forum.com/t/ruby-eval-function/197132 "2010-04-27T13:30:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kevid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@kevid](https://www.ruby-forum.com/u/kevid)\
**Post date:** [April 27, 2010, 1:30pm UTC](https://www.ruby-forum.com/t/ruby-eval-function/197132/1 "2010-04-27T13:30:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kevid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@kevid](https://www.ruby-forum.com/u/kevid)\
**Post date:** [April 27, 2010, 2:01pm UTC](https://www.ruby-forum.com/t/ruby-eval-function/197132/2 "2010-04-27T14:01:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kevid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@kevid](https://www.ruby-forum.com/u/kevid)\
**Post date:** [April 27, 2010, 3:27pm UTC](https://www.ruby-forum.com/t/ruby-eval-function/197132/3 "2010-04-27T15:27:23Z")

</div>

On Apr 27, 8:01 am, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![kevid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@kevid](https://www.ruby-forum.com/u/kevid)\
**Post date:** [April 27, 2010, 7:21pm UTC](https://www.ruby-forum.com/t/ruby-eval-function/197132/4 "2010-04-27T19:21:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kevid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@kevid](https://www.ruby-forum.com/u/kevid)\
**Post date:** [April 27, 2010, 7:27pm UTC](https://www.ruby-forum.com/t/ruby-eval-function/197132/5 "2010-04-27T19:27:16Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![kevid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@kevid](https://www.ruby-forum.com/u/kevid)\
**Post date:** [April 27, 2010, 7:21pm UTC](https://www.ruby-forum.com/t/ruby-eval-function/197132/6 "2010-04-27T19:21:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kevid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@kevid](https://www.ruby-forum.com/u/kevid)\
**Post date:** [April 27, 2010, 9:08pm UTC](https://www.ruby-forum.com/t/ruby-eval-function/197132/7 "2010-04-27T21:08:32Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![kevid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@kevid](https://www.ruby-forum.com/u/kevid)\
**Post date:** [April 27, 2010, 7:55pm UTC](https://www.ruby-forum.com/t/ruby-eval-function/197132/8 "2010-04-27T19:55:35Z")

</div>

On Apr 27, 6:27 pm, Roger P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
