# I don't even know what to ask for

**URL:** <https://www.ruby-forum.com/t/i-dont-even-know-what-to-ask-for/72384>\
**Category:** Ruby\
**Created:** [October 16, 2006, 10:41pm UTC](https://www.ruby-forum.com/t/i-dont-even-know-what-to-ask-for/72384 "2006-10-16T22:41:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 16, 2006, 10:41pm UTC](https://www.ruby-forum.com/t/i-dont-even-know-what-to-ask-for/72384/1 "2006-10-16T22:41:14Z")

</div>

I have a command line shell that I plan to run over and over again.  
It’s base functionality is to accept a list of files names and a file  
name containing the logic to be applied to the contents of said files.  
My shell script contains the logic for accepting the shell commmands  
and list of files. However, I am unable to figure out how to accept a  
simple block of logic from a separate file and use it as a method  
within the shell. An example of the type of logic would be:

# Find parm arg that follows equal sign and upper case it.

# If the arg contains D:, then change to E:\ if it is

# not a “Work File Directory” parm.

def updatevalue(rec) # rec is the record to update  
i = nil # create i  
i = parm.index(’=’)+1 if parm =~ /^[\*\s]|^(BEGIN|END)/  
if i # If i has a value  
j = rec.length-i # determine length of value  
rec[i,j] = rec[i,j].upcase # upper case that portion of text  
i = rec.index(‘D:\’) # Does arg contain D:?  
rec = rec[0,i]+‘E:’+rec[i+3,rec.length-i] if i && (rec !~ /^Work  
File Directory/)  
end  
rec  
end

What I envision is:

E:\> ruby TemplateList.rb -v -f TemplateList.txt -m updatevalue.rb

Where updatevalue.rb is the module I would like to be considered the  
processing block applied to all records read.

Within the shell TemplateList.rb is currently: recs.each {|rec|  
tmp.write(updatevalue(rec)) }

I just don’t know the correct terminology that will lead me to the  
correct solution.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 16, 2006, 11:25pm UTC](https://www.ruby-forum.com/t/i-dont-even-know-what-to-ask-for/72384/2 "2006-10-16T23:25:42Z")

</div>

> E:\> ruby TemplateList.rb -v -f TemplateList.txt -m updatevalue.rb

Try this

\*\*\* Your TemplateList.rb \*\*\*  
logicFileName=‘updatevalue.rb’  
eval(File.new(logicFileName).read)  
testMe(‘arg1’)

\*\*\* Your updatevalue.rb \*\*\*  
def testMe(arg)  
puts “testMe called arg = #{arg}”  
end

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 16, 2006, 11:31pm UTC](https://www.ruby-forum.com/t/i-dont-even-know-what-to-ask-for/72384/3 "2006-10-16T23:31:42Z")

</div>

äÏÈÁÑ òÙËÁ wrote:

> \*\*\* Your updatevalue.rb \*\*\*  
> def testMe(arg)  
> puts “testMe called arg = #{arg}”  
> end

Or the second way:

logicFileName=‘updatevalue.rb’

# self, Module or Class object

self.instance\_eval(File.new(logicFileName).read)  
testMe(‘asd’)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 17, 2006, 1:54am UTC](https://www.ruby-forum.com/t/i-dont-even-know-what-to-ask-for/72384/4 "2006-10-17T01:54:30Z")

</div>

Why doesn’t the following code work?

require ‘rubygems’  
require\_gem “activerecord”

conf = YAML::load(File.open(‘database.yml’))  
ActiveRecord::Base.establish\_connection(conf[‘test’])

class StuffType \< ActiveRecord::Base  
has\_many :Stuff  
end

class Stuff \< ActiveRecord::Base  
set\_table\_name “stuff”  
belongs\_to :StuffType  
def initialize  
StuffType.find\_all.each do |typ|  
class\_eval %(#{typ.name} = #{typ.id})  
end  
end  
end

StuffType is a DB table with two columns, name, and id. E.g. 1 = small,  
2 = medium, 3 = large.  
I want to use it to create an enum for my Stuff class.  
Stuff is another DB table with, among other things, a stuff\_type\_id  
column identifying what type it is.

Unfortunately when I plug this code into irb and type something like  
Stuff::small, expecting to get 1 returned, I get undefined method  
`small’ for Stuff:Class. What am I doing wrong? Is there a better way  
to do what I want?

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 18, 2006, 12:20am UTC](https://www.ruby-forum.com/t/i-dont-even-know-what-to-ask-for/72384/5 "2006-10-18T00:20:59Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Justin Chan wrote:

> end
> 
> class Stuff \< ActiveRecord::Base  
> set\_table\_name “stuff”  
> belongs\_to :StuffType  
> def initialize  
> StuffType.find\_all.each do |typ|  
> class\_eval %(#{typ.name} = #{typ.id})  
> end  
> end  
> end

When you create a new Stuff record you aren’t guaranteed it has an id,  
ie, has it been saved to the database or not? Also,  
ActiveRecord doesn’t create new records (when finding records from the  
database) using the “new” method, so “initialize” never  
gets it.

When do you want your “enum” like behavior to be created, after  
creation, after update, after find, when the class lods?

Also, do you want the relationship to StuffType to be an the instance  
level or at the class.

ie, do you want to say:  
stuff = Stuff.find( 1 )  
stuff.small # assume small is one of your fields in stuff\_types

or do you want to say:  
Stuff.small #assume small is one of your fields in stuff\_types

Zach  
[http://blogs.mktec.com/zdennis](http://blogs.mktec.com/zdennis)  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.2.2 (GNU/Linux)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)

iD8DBQFFNVcBMyx0fW1d8G0RAlCdAJ4nUvDqvmSLlRB3AW1L134nR3JRUwCeM9lX  
Jb79dYvk/9RFfUJx+9aNlvk=  
=8Ihi  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 18, 2006, 4:46pm UTC](https://www.ruby-forum.com/t/i-dont-even-know-what-to-ask-for/72384/6 "2006-10-18T16:46:53Z")

</div>

Thank you, cryptic name I cannot repeat. “eval” works like a champ.  
My future has been greatly simplified.

dvn

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 18, 2006, 12:51am UTC](https://www.ruby-forum.com/t/i-dont-even-know-what-to-ask-for/72384/7 "2006-10-18T00:51:02Z")

</div>

Hi,

does this plugin do what you want?

[http://agilewebdevelopment.com/plugins/acts\_as\_enumerated](http://agilewebdevelopment.com/plugins/acts_as_enumerated)

Regards,  
Trevor
