Python-style decorators

However, even though I think such a change is a good thing to pursue

That sounds good to me. It would easily solve this problem and make
many other things possible.

(I'm even tempted to revive Suby just to offer this one distinct
feature), I offer this:

[snip]

Honestly, I half-expect I'm delusional, b/c I'm not sure how this
manges to work, but it seems to do so. Maybe you can use it as a
jumping board.

That almost works! See why:

class X

  class Foo

    def initialize(klass)
      @klass = klass
      @x = 5
    end

    def singleton_method_added(meth)
      return if meth == :singleton_method_added
      m = method(meth)
      @klass.send(:define_method, meth, m.to_proc)
    end
  end

  def initialize
    @x = 3
  end

  def self.fooized
    @foo ||= Foo.new(self)
  end

  def fooized.try
    10 + @x
  end

end

p X.new.try

If try were really bound to the instance of X, this would produce 13.
But it produces 15.

I was going to try dumping the method object with the Marshal module
as a roundabout way of getting the source code, but that doesn't work
either.

kr

···

On 8/31/07, Trans <transfire@gmail.com> wrote:

That's just what I was looking for. I don't understand why this isn't
built in to ruby. I would use this feature if it didn't add an
external dependency.

kr

···

On 9/4/07, Paul Brannan <pbrannan@atdesk.com> wrote:

Nodewrap lets you do this, but it's not safe:

In my case, I wanted to use the notation in stay of ClassMethods
tricks. For example:

  module M

    def ext.foo
       "foo"
    end

  end

  class X
    include M
  end

  X.foo #=> foo

This proves impossible to do in a modular fashion because you can't
extend a class with a singleton class.

But you may well be right about the current case, hence the potential
solution I posted.

T.

···

On Aug 31, 7:37 am, "Robert Dober" <robert.do...@gmail.com> wrote:

On 8/31/07, Trans <transf...@gmail.com> wrote:

> So you've hit the same wall I did with module inheritance. That's
> unfortunate.

Hopefully I am not too egocentric about my own approach, but if so you
could maybe explain ;)?
I fail to grasp where exactly you hit that wall, I have this
impression that it is the fact that you bind your methods too early,
if you would just refrain from using methods that are defined inside
the class, but would always do some include/extend trick, would the
wall not just come trembling down?
Sorry if I am missing the obvious here.

Fudge Nuts! Well, I knew something was a miss. I know Ruby too well to
expect that to work :wink:

And I don't think Pit Captain could even wrangle up a solution to this
one. Oh well. Guess it's back to the method_added or block notation.
Which is too bad, b/c not you have to make a choice between the
readability to the former and the reliability of the later.

T.

···

On Aug 31, 11:27 am, "Keith Rarick" <k...@essembly.com> wrote:

On 8/31/07, Trans <transf...@gmail.com> wrote:

> However, even though I think such a change is a good thing to pursue

That sounds good to me. It would easily solve this problem and make
many other things possible.

> (I'm even tempted to revive Suby just to offer this one distinct
> feature), I offer this:

> [snip]

> Honestly, I half-expect I'm delusional, b/c I'm not sure how this
> manges to work, but it seems to do so. Maybe you can use it as a
> jumping board.

That almost works! See why:

class X

  class Foo

    def initialize(klass)
      @klass = klass
      @x = 5
    end

    def singleton_method_added(meth)
      return if meth == :singleton_method_added
      m = method(meth)
      @klass.send(:define_method, meth, m.to_proc)
    end
  end

  def initialize
    @x = 3
  end

  def self.fooized
    @foo ||= Foo.new(self)
  end

  def fooized.try
    10 + @x
  end

end

p X.new.try

If try were really bound to the instance of X, this would produce 13.
But it produces 15.

Hi,

At Wed, 5 Sep 2007 07:21:41 +0900,
Keith Rarick wrote in [ruby-talk:267626]:

> Nodewrap lets you do this, but it's not safe:

That's just what I was looking for. I don't understand why this isn't
built in to ruby. I would use this feature if it didn't add an
external dependency.

Because it's too dangerous rather than unsafe.

···

--
Nobu Nakada

Consider: what would happen if I moved #close from the IO class to the
Array class?

Paul

···

On Wed, Sep 05, 2007 at 07:21:41AM +0900, Keith Rarick wrote:

On 9/4/07, Paul Brannan <pbrannan@atdesk.com> wrote:
> Nodewrap lets you do this, but it's not safe:

That's just what I was looking for. I don't understand why this isn't
built in to ruby. I would use this feature if it didn't add an
external dependency.

Still trying to get the problem
I know that you know (do I?) that you can do
module M
  def foo; "foo" end
end
class X
   extend M
end

where's the catch?
R.

···

On 8/31/07, Trans <transfire@gmail.com> wrote:

On Aug 31, 7:37 am, "Robert Dober" <robert.do...@gmail.com> wrote:
> On 8/31/07, Trans <transf...@gmail.com> wrote:
>
> > So you've hit the same wall I did with module inheritance. That's
> > unfortunate.
>
> Hopefully I am not too egocentric about my own approach, but if so you
> could maybe explain ;)?
> I fail to grasp where exactly you hit that wall, I have this
> impression that it is the fact that you bind your methods too early,
> if you would just refrain from using methods that are defined inside
> the class, but would always do some include/extend trick, would the
> wall not just come trembling down?
> Sorry if I am missing the obvious here.

In my case, I wanted to use the notation in stay of ClassMethods
tricks. For example:

  module M

    def ext.foo
       "foo"
    end

  end

  class X
    include M
  end

  X.foo #=> foo

This proves impossible to do in a modular fashion because you can't
extend a class with a singleton class.

But you may well be right about the current case, hence the potential
solution I posted.

--
I'm an atheist and that's it. I believe there's nothing we can know
except that we should be kind to each other and do what we can for
other people.
-- Katharine Hepburn

Hi,

At Wed, 5 Sep 2007 09:31:12 +0900,
Nobuyoshi Nakada wrote in [ruby-talk:267662]:

> > Nodewrap lets you do this, but it's not safe:
>
> That's just what I was looking for. I don't understand why this isn't
> built in to ruby. I would use this feature if it didn't add an
> external dependency.

Because it's too dangerous rather than unsafe.

...and you have to know too deep implementation details very
well, to use it.

···

--
Nobu Nakada

There are various reasonable ways to handle errors such as this.

At first, I would expect the same behavior that would result from
taking the ruby source code for the #close method and pasting it in to
the Array class. If #close isn't written in ruby, then don't allow it
to be moved.

kr

···

On 9/7/07, Paul Brannan <pbrannan@atdesk.com> wrote:

Consider: what would happen if I moved #close from the IO class to the
Array class?

When you want class-level methods that go along with your instance
methods.

  module M
    def self.foo
      @foo ||= {}
    end
    def foo
      self.class.foo
    end
  end

  Class X
    include M
  end

  X.new.foo #=> ERROR

If one could include singletons, the solution would be as simple as:

  class X
    extend(class << Foo; self; end)
  end

But as things are you need some sort of "trick".

T.

···

On Aug 31, 12:47 pm, "Robert Dober" <robert.do...@gmail.com> wrote:

Still trying to get the problem
I know that you know (do I?) that you can do
module M
  def foo; "foo" end
end
class X
   extend M
end

where's the catch?

Ok I think I got it, thx, I was indeed trying to make Keith's
  def self.fooized
  def fooized.try
code work but did not see how this was related to the general problem
of yours. That is because I got confused what is defined where, well
this really is sometimes quite embarrassing, I was sure to get it
done, but no such luck .... :frowning:
Thx
Robert

···

On 8/31/07, Trans <transfire@gmail.com> wrote:

On Aug 31, 12:47 pm, "Robert Dober" <robert.do...@gmail.com> wrote:

> Still trying to get the problem
> I know that you know (do I?) that you can do
> module M
> def foo; "foo" end
> end
> class X
> extend M
> end
>
> where's the catch?

When you want class-level methods that go along with your instance
methods.

  module M
    def self.foo
      @foo ||= {}
    end
    def foo
      self.class.foo
    end
  end

  Class X
    include M
  end

  X.new.foo #=> ERROR

If one could include singletons, the solution would be as simple as:

  class X
    extend(class << Foo; self; end)
  end

But as things are you need some sort of "trick".

--
I'm an atheist and that's it. I believe there's nothing we can know
except that we should be kind to each other and do what we can for
other people.
-- Katharine Hepburn