[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

nested defs, what if...

Hugh Sasse

12/2/2004 2:44:00 PM

16 Answers

Austin Ziegler

12/2/2004 2:57:00 PM

0

On Thu, 2 Dec 2004 23:44:08 +0900, Hugh Sasse Staff Elec Eng
<hgs@dmu.ac.uk> wrote:
> This is too half-baked to be an RCR, but here goes...
>
> At the moment we cannot write
>
> class Thingy
> def somefunc
> def newfunc
> ...
> end
> end
> end

irb(main):001:0> class Thingy
irb(main):002:1> def somefunc
irb(main):003:2> def newfunc
irb(main):004:3> end
irb(main):005:2> end
irb(main):006:1> end
irb(main):007:0> Thingy.instance_methods.grep(/func/)
=> ["somefunc"]
irb(main):009:0> Thingy.new.somefunc
=> nil
irb(main):010:0> Thingy.instance_methods.grep(/func/)
=> ["newfunc", "somefunc"]

-austin
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca


Yukihiro Matsumoto

12/2/2004 3:08:00 PM

0

Hi,

In message "Re: nested defs, what if..."
on Thu, 2 Dec 2004 23:44:08 +0900, Hugh Sasse Staff Elec Eng <hgs@dmu.ac.uk> writes:

|So, my half-baked idea: let the above code be a shorthand for
|
|class Thingy
| def somefunc
| class << self
| def newfunc
| ...
| end
| end
| end
|end

I had the same half-baked idea before, but has not been sure how much
it is useful. If you come up a new insight, it might be happened.

matz.


Hugh Sasse

12/2/2004 3:20:00 PM

0

Hugh Sasse

12/2/2004 4:18:00 PM

0

Brian Schröder

12/2/2004 5:22:00 PM

0

On Thu, 2 Dec 2004 23:57:09 +0900
Austin Ziegler <halostatue@gmail.com> wrote:

> On Thu, 2 Dec 2004 23:44:08 +0900, Hugh Sasse Staff Elec Eng
> <hgs@dmu.ac.uk> wrote:
> > This is too half-baked to be an RCR, but here goes...
> >
> > At the moment we cannot write
> >
> > class Thingy
> > def somefunc
> > def newfunc
> > ...
> > end
> > end
> > end
>
> irb(main):001:0> class Thingy
> irb(main):002:1> def somefunc
> irb(main):003:2> def newfunc
> irb(main):004:3> end
> irb(main):005:2> end
> irb(main):006:1> end
> irb(main):007:0> Thingy.instance_methods.grep(/func/)
> => ["somefunc"]
> irb(main):009:0> Thingy.new.somefunc
> => nil
> irb(main):010:0> Thingy.instance_methods.grep(/func/)
> => ["newfunc", "somefunc"]
>

But where is this usefull? It seems only complicated and inefficent to me
(Doesn't it create a new instance method on each call?):

class A
def a()
def b()
self
end
self
end
end
==>nil
A.new.b
NoMethodError: undefined method `b' for #<A:0x402c0834>
from (irb):2
A.new.a
==>#<A:0x402a9cb0>
A.new.a.b
==>#<A:0x4029bad4>
A.new.b
==>#<A:0x40295864>

--
Brian Schröder
http://www.brian-sch...



T. Onoma

12/2/2004 11:21:00 PM

0

On Thursday 02 December 2004 12:22 pm, Brian Schröder wrote:
| But where is this usefull? It seems only complicated and inefficent to me
| (Doesn't it create a new instance method on each call?):
|
| class A
| def a()
| def b()
| self
| end
| self
| end
| end
| ==>nil
| A.new.b
| NoMethodError: undefined method `b' for #<A:0x402c0834>
| from (irb):2
| A.new.a
| ==>#<A:0x402a9cb0>
| A.new.a.b
| ==>#<A:0x4029bad4>
| A.new.b
| ==>#<A:0x40295864>

It is interesting. Does this mean that an _object_ could dynamically change
the state of all objects of its class? I have to test....

class T
def a
def b
puts "Hello"
end
end
end => nil
t = T.new => #<T:0x4032cb14>
t2 = T.new => #<T:0x4032ae90>
t.b => NoMethodError: undefined method `b' for #<T:0x4032cb14>
t2.b => NoMethodError: undefined method `b' for #<T:0x4032ae90>
t.a => nil
t.b => nil
Hello
t2.b => nil
Hello

Yep. It sure does. This is very strange indeed. I wonder if you could write
extentions in this way.

class String
def use(x)
case x
when :chars
def chars
self.split(//)
end
when :tab
# ...
end
end
end

Of course, you have to instantiate a string first.

Well, it's an idea, but I don't think a very good one. In general I don't
think this is useful, and would rather obfuscate code if actually used. I
think what would be more useful is if such methods were local methods, like
local variables:

class A
def a
def b(x)
x + 1
end
10.times{ |i| print b(i) }
end
end

o = A.new
o.b => NoMethodError
A.a => 12345678910
o.b => NoMethodError

This would allow for embedded subroutines --much more useful.

T.





nobu.nokada

12/3/2004 2:48:00 AM

0

Hi,

At Fri, 3 Dec 2004 08:20:34 +0900,
trans. (T. Onoma) wrote in [ruby-talk:122265]:
> Well, it's an idea, but I don't think a very good one. In general I don't
> think this is useful, and would rather obfuscate code if actually used. I
> think what would be more useful is if such methods were local methods, like
> local variables:

I also have thought the idea, though haven't implemented it.
I'm still worndering whether this should be valid or error.

class A
def a(y)
def b(x)
x + y # `y' in a or `undefined local variable'?
end
10.times{ |i| print b(i) }
end
end

--
Nobu Nakada


Gavin Sinclair

12/3/2004 5:01:00 AM

0

On Friday, December 3, 2004, 1:47:32 PM, nobu wrote:

> Hi,

> At Fri, 3 Dec 2004 08:20:34 +0900,
> trans. (T. Onoma) wrote in [ruby-talk:122265]:
>> Well, it's an idea, but I don't think a very good one. In general I don't
>> think this is useful, and would rather obfuscate code if actually used. I
>> think what would be more useful is if such methods were local methods, like
>> local variables:

> I also have thought the idea, though haven't implemented it.
> I'm still worndering whether this should be valid or error.

> class A
> def a(y)
> def b(x)
> x + y # `y' in a or `undefined local variable'?
> end
> 10.times{ |i| print b(i) }
> end
> end

Is there any difference between an "inner method" (with 'y' in a) and
a lambda?

I've happily used lambdas to define private functionality before, but
I'm concerned about efficiency:

class A
def a(y)
b = lambda { |x| x + y }
10.times { |i| print b(i) }
end
end

Does the lambda attached to b get regenerated with each call to a?

Gavin



nobu.nokada

12/3/2004 5:19:00 AM

0

Hi,

At Fri, 3 Dec 2004 14:00:44 +0900,
Gavin Sinclair wrote in [ruby-talk:122305]:
> > class A
> > def a(y)
> > def b(x)
> > x + y # `y' in a or `undefined local variable'?
> > end
> > 10.times{ |i| print b(i) }
> > end
> > end
>
> Is there any difference between an "inner method" (with 'y' in a) and
> a lambda?

Actually, no. Just a syntax sugar, and it would be implemented
using a lambda. And another question; should b be visible from
other methods called from a?

> I've happily used lambdas to define private functionality before, but
> I'm concerned about efficiency:
>
> class A
> def a(y)
> b = lambda { |x| x + y }
> 10.times { |i| print b(i) }
> end
> end
>
> Does the lambda attached to b get regenerated with each call to a?

I guess so.

--
Nobu Nakada


Gavin Sinclair

12/3/2004 5:37:00 AM

0

On Friday, December 3, 2004, 4:18:38 PM, nobu wrote:

> Hi,

> At Fri, 3 Dec 2004 14:00:44 +0900,
> Gavin Sinclair wrote in [ruby-talk:122305]:
>> > class A
>> > def a(y)
>> > def b(x)
>> > x + y # `y' in a or `undefined local variable'?
>> > end
>> > 10.times{ |i| print b(i) }
>> > end
>> > end
>>
>> Is there any difference between an "inner method" (with 'y' in a) and
>> a lambda?

> Actually, no. Just a syntax sugar, and it would be implemented
> using a lambda. And another question; should b be visible from
> other methods called from a?

I don't think so. I can't imagine a use case, and the _appearance_ of
the code is that b() is private to a().

>> I've happily used lambdas to define private functionality before, but
>> I'm concerned about efficiency:
>>
>> class A
>> def a(y)
>> b = lambda { |x| x + y }
>> 10.times { |i| print b(i) }
>> end
>> end
>>
>> Does the lambda attached to b get regenerated with each call to a?

> I guess so.

So an inner method, which _looks_ static, would actually be
inefficiently regenerated each time? (You said the inner method would
be syntax sugar for a lambda.)

Gavin