[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

I don't even know what to ask for

dkmd_nielsen

10/16/2006 8:36:00 PM

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.

26 Answers

????? ????

10/16/2006 9:23:00 PM

0

> 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

????? ????

10/16/2006 9:31:00 PM

0

aIEAN oUEA wrote:
>> 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

Or the second way:

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

Justin Chan

10/16/2006 11:53:00 PM

0

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?

Zach Dennis

10/17/2006 10:19:00 PM

0

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

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

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

Trevor Squires

10/17/2006 10:50:00 PM

0

Hi,

does this plugin do what you want?

http://agilewebdevelopment.com/plugins/acts_as_...

Regards,
Trevor

On 16-Oct-06, at 4:53 PM, Justin Chan wrote:

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


dkmd_nielsen

10/18/2006 2:41:00 PM

0

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

dvn


????? ???? wrote:
> äÏÈÁÑ òÙËÁ wrote:
> >> 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
>
> Or the second way:
>
> logicFileName='updatevalue.rb'
> # self, Module or Class object
> self.instance_eval(File.new(logicFileName).read)
> testMe('asd')

Alias

9/14/2012 2:53:00 PM

0

On 9/14/2012 3:59 PM, Dan C wrote:
> On Fri, 14 Sep 2012 13:47:51 +0200, Alias wrote:
>
>> On 9/14/2012 1:46 PM, Jim_Higgins wrote:
>>> September 11, 2012?and Obama Apologizes http://tinyurl.c...
>>>
>>>
>> Not an apology, dumb fuck,
>
> Yes, it *was* an apology, issued by the Embassy in Cairo, which speaks
> for the Sec of State and the President.
>
> Did you read the article?

I read what the Embassy wrote:

?The Embassy of the United States in Cairo condemns the continuing
efforts by misguided individuals to hurt the religious feelings of
Muslims ? as we condemn efforts to offend believers of all religions.
Today, the 11th anniversary of the September 11, 2001, terrorist attacks
on the United States, Americans are honoring our patriots and those who
serve our nation as the fitting response to the enemies of democracy.
Respect for religious beliefs is a cornerstone of American democracy. We
firmly reject the actions by those who abuse the universal right of free
speech to hurt the religious beliefs of others.?

>> but a condemnation of those who seek to provoke violence.
>
> No, it was an apology, in advance. Really. Yes, it was.
>
>

No, it was a condemnation of the film. So, no, really, it wasn't an apology.

--
Alias

chatnoir

9/14/2012 5:59:00 PM

0

On Sep 14, 8:53 am, Alias <aka@masked&anonymous.com.invalid> wrote:
> On 9/14/2012 3:59 PM, Dan C wrote:
>
> > On Fri, 14 Sep 2012 13:47:51 +0200, Alias wrote:
>
> >> On 9/14/2012 1:46 PM, Jim_Higgins wrote:
> >>> September 11, 2012—and Obama Apologizeshttp://tinyurl.c...
>
> >> Not an apology, dumb fuck,
>
> > Yes, it *was* an apology, issued by the Embassy in Cairo, which speaks
> > for the Sec of State and the President.
>
> > Did you read the article?
>
> I read what the Embassy wrote:
>
> “The Embassy of the United States in Cairo condemns the continuing
> efforts by misguided individuals to hurt the religious feelings of
> Muslims — as we condemn efforts to offend believers of all religions.
> Today, the 11th anniversary of the September 11, 2001, terrorist attacks
> on the United States, Americans are honoring our patriots and those who
> serve our nation as the fitting response to the enemies of democracy.
> Respect for religious beliefs is a cornerstone of American democracy. We
> firmly reject the actions by those who abuse the universal right of free
> speech to hurt the religious beliefs of others.”
>
> >> but a condemnation of those who seek to provoke violence.
>
> > No, it was an apology, in advance.  Really.  Yes, it was.
>
> No, it was a condemnation of the film. So, no, really, it wasn't an apology.
>
> --
> Alias

Romney's people are getting desperate!

Alias

9/14/2012 11:35:00 PM

0

On 9/14/2012 7:58 PM, chatnoir wrote:
> On Sep 14, 8:53 am, Alias <aka@masked&anonymous.com.invalid> wrote:
>> On 9/14/2012 3:59 PM, Dan C wrote:
>>
>>> On Fri, 14 Sep 2012 13:47:51 +0200, Alias wrote:
>>
>>>> On 9/14/2012 1:46 PM, Jim_Higgins wrote:
>>>>> September 11, 2012?and Obama Apologizeshttp://tinyurl.c...
>>
>>>> Not an apology, dumb fuck,
>>
>>> Yes, it *was* an apology, issued by the Embassy in Cairo, which speaks
>>> for the Sec of State and the President.
>>
>>> Did you read the article?
>>
>> I read what the Embassy wrote:
>>
>> ?The Embassy of the United States in Cairo condemns the continuing
>> efforts by misguided individuals to hurt the religious feelings of
>> Muslims ? as we condemn efforts to offend believers of all religions.
>> Today, the 11th anniversary of the September 11, 2001, terrorist attacks
>> on the United States, Americans are honoring our patriots and those who
>> serve our nation as the fitting response to the enemies of democracy.
>> Respect for religious beliefs is a cornerstone of American democracy. We
>> firmly reject the actions by those who abuse the universal right of free
>> speech to hurt the religious beliefs of others.?
>>
>>>> but a condemnation of those who seek to provoke violence.
>>
>>> No, it was an apology, in advance. Really. Yes, it was.
>>
>> No, it was a condemnation of the film. So, no, really, it wasn't an apology.
>>
>> --
>> Alias
>
> Romney's people are getting desperate!
>

I almost feel sorry for him. Note, I wrote "almost".

--
Alias

rumpelstiltskin

9/15/2012 3:58:00 AM

0

On Fri, 14 Sep 2012 10:58:42 -0700 (PDT), chatnoir
<wolfbat359a@mindspring.com> wrote:

>On Sep 14, 8:53?am, Alias <aka@masked&anonymous.com.invalid> wrote:
>> On 9/14/2012 3:59 PM, Dan C wrote:
>>
>> > On Fri, 14 Sep 2012 13:47:51 +0200, Alias wrote:
>>
>> >> On 9/14/2012 1:46 PM, Jim_Higgins wrote:
>> >>> September 11, 2012?and Obama Apologizeshttp://tinyurl.c...
>>
>> >> Not an apology, dumb fuck,
>>
>> > Yes, it *was* an apology, issued by the Embassy in Cairo, which speaks
>> > for the Sec of State and the President.
>>
>> > Did you read the article?
>>
>> I read what the Embassy wrote:
>>
>> ?The Embassy of the United States in Cairo condemns the continuing
>> efforts by misguided individuals to hurt the religious feelings of
>> Muslims ? as we condemn efforts to offend believers of all religions.
>> Today, the 11th anniversary of the September 11, 2001, terrorist attacks
>> on the United States, Americans are honoring our patriots and those who
>> serve our nation as the fitting response to the enemies of democracy.
>> Respect for religious beliefs is a cornerstone of American democracy. We
>> firmly reject the actions by those who abuse the universal right of free
>> speech to hurt the religious beliefs of others.?
>>
>> >> but a condemnation of those who seek to provoke violence.
>>
>> > No, it was an apology, in advance. ?Really. ?Yes, it was.
>>
>> No, it was a condemnation of the film. So, no, really, it wasn't an apology.
>>
>> --
>> Alias
>
>Romney's people are getting desperate!


I saw on Democracy today somebody saying that Obama's going
to win because Romney just seems so - I'll say "sleazy" since I
don't remember the term they used but it amounted to "sleazy".
I hope, and think, that's true, and that the piratical nature (IMV) of
Bain Capital, no matter how "brilliantly managed" it was, may have
been working on people's minds as they think about whether this
is the kind of guy they want for prez.