[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

[PreAnn] Labrador v0.0.1 or broken @ key

Robert Dober

6/11/2007 7:36:00 PM

Hi list

I eventually decided to submit some code to Rubyforge, just waiting
for the Rubyforge decision...

In the meantime I wanted to share some of the code to be released. It
seems that Robert Klemme broke his "@" key ;) so I decided to replace
ivars in my Ruby.
Closure based properties, here we go

PropertyIllegalMonitorState = Class::new RuntimeError
class Module
def property name, type = :rw, &blk
props = {}
define_method name do
props[object_id] ||= blk.call if blk
props[object_id]
end
case type
when :rw
define_method "#{name}=" do |val|
props[object_id] = val
end
when :const
define_method "#{name}=" do |val|
raise PropertyIllegalMonitorState,
"const property #{name} cannot be reassigned" if
props.has_key? object_id
props[object_id] = val
end # define_method "#{name}=" do |val|
end
end
end

Ideas? Believes? Opinions?
Here is what I believe of it:
+ Real const properties that cannot be circumvented by metaprogramming
+ A different style never hurts
+ Quite short code to achieve this
+ Lots of place for development, locked properties to implement
Critical Sections, access fine graining e.g.

- Way slower than ivars I guess (no time to benchmark so far)
- Python style property assignment on self cannot be avoided;
self.prop = 42 not prop=42
- You will tell me ;)


BTW the rest of the package wil be enum trickery, things like
%w{a b c}.map.succ => %w{ b c d }
[*1..10].map(:+, 2) => [*3..12]

It is just for people who share my style (even if they do not like Labradors;).

Cheers
Robert
--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw