[PreAnn] Labrador v0.0.1 or broken @ key

R

Robert Dober

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
 

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,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top