Forum: Ruby Labrador v0.0.1 or broken @ key

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Robert D. (Guest)
on 2007-06-11 23:36
(Received via mailing list)
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 K. 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
This topic is locked and can not be replied to.