I don't even know what to ask for

D

dkmd_nielsen

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.
 
Ä

äÏÈÁÑ òÙËÁ

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
 
Ä

äÏÈÁÑ òÙËÁ

äÏÈÁÑ òÙËÁ said:
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

Or the second way:

logicFileName='updatevalue.rb'
# self, Module or Class object
self.instance_eval(File.new(logicFileName).read)
testMe('asd')
 
J

Justin Chan

Why doesn't the following code work?

require 'rubygems'
require_gem "activerecord"

conf =3D 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|=20
class_eval %(#{typ.name} =3D #{typ.id})
end
end =20
end

StuffType is a DB table with two columns, name, and id. E.g. 1 =3D =
small,
2 =3D medium, 3 =3D 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?
 
Z

zdennis

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

Justin said:
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

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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

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

dkmd_nielsen

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

dvn
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top