Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #4325
| From | Markus Schirp <mbj@seonic.net> |
|---|---|
| Newsgroups | comp.lang.ruby |
| Subject | Re: how to make eval() faster? |
| Date | 2011-05-12 01:42 -0500 |
| Organization | Service de news de lacave.net |
| Message-ID | <20110512064308.GA30754@mbj> (permalink) |
| References | <0229c425b2bf06473387c4f47263eb93@ruby-forum.com> <4DCB6EAA.7010101@gmail.com> <4c16a97bbcba0b1d4a451dc4f577e8be@ruby-forum.com> <4DCB7A92.9020505@seonic.net> <21d0dceba517129670c3be73b83e50b3@ruby-forum.com> |
Too difficult?
The most "stupid simple" dsl is IMHO block based...
yourlib.rb
----------
module Yourlib
class << self
def define_processor(key,&block)
processors << [key,&block]
end
def processors
@processors ||= []
end
def run(row) # row should be an hash
processors.each do |key,block|
input[key]=block.call(row)
end
end
end
end
settings.rb
-----------
require 'yourlib'
Yourlib.define_processor :outcome_a do |current|
current[:event_a] * current[:event_b]
end
Yourlib.define_processor :outcome_b do |current|
current[:outcome_b] = current[:event_b] * current[:outcome_a]
end
File.readlines(somepath) do |row|
out = Yourlib.run(preprocess(row))
do_something_fancy(out)
end
As long as your users do not use outcomes before they are produced you
can do calculations based on the outcomes...
If you need namespacing etc you can simply make an convention about the
output key names... and maybe about the input keys too... or try to add
(nested) namespacing.
I new this DSL is messy and redundant in usage, but a better approach
than using eval ;)
Regards,
Markus
On Thu, May 12, 2011 at 03:18:56PM +0900, Zd Yu wrote:
> Markus Schirp wrote in post #998159:
> > The configuration file can be plain ruby!
> > You can define your own DSL to manage the formulars.
> > So you can load the file once and the formulars do not have to be
> > evaled...
>
> Nice catch. Actually I did try to implement it as DSL at the beginning,
> but finally I realized it is difficult to implement such a DSL in my
> problem. So at last I went to the XML format configuration way.
>
> Below is the post I asked about DSL in 5 months ago:
> http://www.ruby-forum.com/topic/696974#new
>
> --
> Posted via http://www.ruby-forum.com/.
Back to comp.lang.ruby | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
how to make eval() faster? Zd Yu <zdyu2000@gmail.com> - 2011-05-11 23:53 -0500
Re: how to make eval() faster? Joel VanderWerf <joelvanderwerf@gmail.com> - 2011-05-12 00:22 -0500
Re: how to make eval() faster? Zd Yu <zdyu2000@gmail.com> - 2011-05-12 01:08 -0500
Re: how to make eval() faster? Markus Schirp <mbj@seonic.net> - 2011-05-12 01:13 -0500
Re: how to make eval() faster? Zd Yu <zdyu2000@gmail.com> - 2011-05-12 01:18 -0500
Re: how to make eval() faster? Markus Schirp <mbj@seonic.net> - 2011-05-12 01:42 -0500
Re: how to make eval() faster? Robert Klemme <shortcutter@googlemail.com> - 2011-05-12 02:26 -0500
Re: how to make eval() faster? Zd Yu <zdyu2000@gmail.com> - 2011-05-12 01:37 -0500
Re: how to make eval() faster? Stu <stu@rubyprogrammer.net> - 2011-05-12 01:55 -0500
Re: how to make eval() faster? Xavier Noria <fxn@hashref.com> - 2011-05-12 02:14 -0500
csiph-web