Clint
3/8/2006 2:29:00 AM
ara.t.howard@noaa.gov wrote:
> On Wed, 8 Mar 2006, Mike Austin wrote:
>
>> I'm writing a property driven ui and would like to do things like:
>>
>> button = Button.new do
>> caption = 'Click Me'
>> end
>>
>> I've read that 2.0 will have this feature, i.e. Class.new will execute
>> it's block argument in it's own context. But how do I do it in 1.8?
>>
>> Thanks much,
>> Mike
>
> class Button
> def initialize(*args, &block)
> instance_eval &block
> end
> end
>
> note that you'll need to do
>
> self.caption = 'Click Me'
Thanks, that works well. But as you note, prepending 'self' kind of defeats
the purpose ;) Is there a way to make Ruby not create local variables, but use
'local var' or similar? Io (www.iolanguage.com) has this problem early on
and was solved by using := for creating slots, and = for updating slots. It
would be nice if it could be lenient when running in irb, and more strict when
executing .rb files.
> or else caption will be a local variable. for an elegant work around
> see the
> metakoan quiz or my traits library. with it a really nice syntax like
> this is
> possible
Metakoan quiz didn't turn up anything. I looked at your traits library and it
looks nice. I could use an operator instead of '=' I suppose.
> button = Button.new {
> caption 'Click Me'
> colour 'Orange'
> size '42'
> }
>
> here's a real example from something i'm coding now:
>
>
> class Flo5 < NRT::OLSSubscription::Geotiffed
> mode "production"
>
> roi 47,16,39,27
>
> satellites %w( F15 F16 )
>
> extensions %w( OIS )
>
> solarelevations -180, -12, 10.0
>
> hold 0
>
> username "flo"
>
> password "xxx"
>
> orbital_start_direction "descending"
>
> start_time "2004-12-08"
> end_time "2006-12-08"
>
> to_keep [ %r/.tif$/, %r/.hdr$/ ]
>
> tmpwatch "8 days"
> end
>
>
> hth.
>
>
> -a
Mike