[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

using ala pascal

Simon Strandgaard

10/26/2003 12:19:00 PM

I have some class hierarchies located in different modules, but with
clashing names, thus I cannot 'include' the modules.
Basicly what im interested in is something similar to 'using' in pascal.
Any advice would be appreciated :-)

Q1: using ?

Q2: Is it possible to make an alias for a module, so that I for instance
only have to type X::Test when I refer to LongModuleName1::Test ?

--
Simon Strandgaard


module LongModuleName1
class Test
def to_s; "1" end
end
end

class Test
def initialize
using(LongModuleName1) { # would be nice
@value = Test.new
}
end
def to_s
@value.to_s
end
end

p Test.new.to_s # => "2"
14 Answers

Jason Williams

10/26/2003 12:20:00 PM

0

In article <pan.2003.10.26.12.18.37.847429@sneakemail.com>, Simon
Strandgaard wrote:
> Q2: Is it possible to make an alias for a module, so that I for instance
> only have to type X::Test when I refer to LongModuleName1::Test ?

This bit's easy enough - "X = LongModuleName1" should work fine, I
think.

Simon Strandgaard

10/26/2003 12:39:00 PM

0

On Sun, 26 Oct 2003 12:20:10 +0000, Jason Williams wrote:

> In article <pan.2003.10.26.12.18.37.847429@sneakemail.com>, Simon
> Strandgaard wrote:
>> Q2: Is it possible to make an alias for a module, so that I for instance
>> only have to type X::Test when I refer to LongModuleName1::Test ?
>
> This bit's easy enough - "X = LongModuleName1" should work fine, I
> think.

Sorry.. that doesn't work


server> ruby a.rb
a.rb:9: dynamic constant assignment
X = LongModuleName1
^
server> expand -t2 a.rb
module LongModuleName1
class Test
def to_s; "1" end
end
end

class Test
def initialize
X = LongModuleName1
@value = X::Test.new
end
def to_s
@value.to_s
end
end

p Test.new.to_s # => "1"
server>

Jason Williams

10/26/2003 12:41:00 PM

0

In article <pan.2003.10.26.12.39.26.895888@sneakemail.com>, Simon Strandgaard
wrote:
> On Sun, 26 Oct 2003 12:20:10 +0000, Jason Williams wrote:
>
>> In article <pan.2003.10.26.12.18.37.847429@sneakemail.com>, Simon
>> Strandgaard wrote:
>>> Q2: Is it possible to make an alias for a module, so that I for instance
>>> only have to type X::Test when I refer to LongModuleName1::Test ?
>>
>> This bit's easy enough - "X = LongModuleName1" should work fine, I
>> think.
>
> Sorry.. that doesn't work

Oh, peculiar. I tried this code, and it worked okay -

module LongModuleName
class Wibble ; end
end

X = LongModuleName

puts X::Wibble


Simon Strandgaard

10/26/2003 12:42:00 PM

0

On Sun, 26 Oct 2003 12:20:10 +0000, Jason Williams wrote:

> In article <pan.2003.10.26.12.18.37.847429@sneakemail.com>, Simon
> Strandgaard wrote:
>> Q2: Is it possible to make an alias for a module, so that I for instance
>> only have to type X::Test when I refer to LongModuleName1::Test ?
>
> This bit's easy enough - "X = LongModuleName1" should work fine, I
> think.

However using a lowercase letter, then it works :-)


server> ruby a.rb
"1"
server> expand -t2 a.rb
module LongModuleName1
class Test
def to_s; "1" end
end
end

class Test
def initialize
x = LongModuleName1
@value = x::Test.new
end
def to_s
@value.to_s
end
end

p Test.new.to_s # => "1"
server>

gabriele renzi

10/26/2003 12:45:00 PM

0

il Sun, 26 Oct 2003 13:39:26 +0100, Simon Strandgaard
<qj5nd7l02@sneakemail.com> ha scritto::

>On Sun, 26 Oct 2003 12:20:10 +0000, Jason Williams wrote:


>
>Sorry.. that doesn't work
>
>
>server> ruby a.rb
>a.rb:9: dynamic constant assignment
> X = LongModuleName1
> ^
>server> expand -t2 a.rb
>module LongModuleName1
> class Test
> def to_s; "1" end
> end
>end
>
>class Test
> def initialize
> X = LongModuleName1
> @value = X::Test.new
> end
> def to_s
> @value.to_s
> end
>end

this fails cause you are doing it in a method.
You can do it safely with const_set

irb(main):001:0> def f
irb(main):002:1> Object::const_set 'X',Math::PI
irb(main):003:1> end
=> nil
irb(main):004:0> f
=> 3.14159265358979
irb(main):005:0> X
=> 3.14159265358979

Simon Strandgaard

10/26/2003 3:49:00 PM

0

On Sun, 26 Oct 2003 13:18:38 +0100, Simon Strandgaard wrote:

> I have some class hierarchies located in different modules, but with
> clashing names, thus I cannot 'include' the modules.
> Basicly what im interested in is something similar to 'using' in pascal.
> Any advice would be appreciated :-)
>
> Q1: using ?


Damn.. Actually I am thinking of the 'with' keyword in pascal.
Question: Any ideas on how to accomplish something similar in Ruby ?

--
Simon Strandgaard


> module LongModuleName1
> class Test
> def to_s; "1" end
> end
> end
>
> class Test
> def initialize
> with(LongModuleName1) { # would be nice
> @value = Test.new
> }
> end
> def to_s
> @value.to_s
> end
> end
>
> p Test.new.to_s # => "2"

gabriele renzi

10/26/2003 7:06:00 PM

0

il Sun, 26 Oct 2003 16:49:24 +0100, Simon Strandgaard
<qj5nd7l02@sneakemail.com> ha scritto::


>
>Damn.. Actually I am thinking of the 'with' keyword in pascal.
>Question: Any ideas on how to accomplish something similar in Ruby ?

Oh I was wondering what the hell that keyword meant :)

the trick is easy:

def with(object, &block)
object.instance_eval(&block)
end

with 'ciao' do
p length
p id
end

Simon Strandgaard

10/26/2003 10:17:00 PM

0

On Sun, 26 Oct 2003 19:05:53 +0000, gabriele renzi wrote:

> il Sun, 26 Oct 2003 16:49:24 +0100, Simon Strandgaard
> <qj5nd7l02@sneakemail.com> ha scritto::
>
>
>>
>>Damn.. Actually I am thinking of the 'with' keyword in pascal.
>>Question: Any ideas on how to accomplish something similar in Ruby ?
>
> Oh I was wondering what the hell that keyword meant :)
>
> the trick is easy:
>
> def with(object, &block)
> object.instance_eval(&block)
> end
>
> with 'ciao' do
> p length
> p id
> end

What am I doing wrong here ?

--
Simon Strandgaard

server> ruby a.rb
a.rb:11:in `test': uninitialized constant A (NameError)
from a.rb:10:in `instance_eval'
from a.rb:2:in `instance_eval'
from a.rb:2:in `with'
from a.rb:10:in `test'
from a.rb:16
server> expand -t2 a.rb
def with(object, &block)
object.instance_eval(&block)
end
module ModuleTest
class A
def test; 42 end
end
end
def test
with(ModuleTest) {
with(A.new) {
p test
}
}
end
test
server>

Robert Klemme

10/27/2003 9:20:00 AM

0


"gabriele renzi" <surrender_it@remove.yahoo.it> schrieb im Newsbeitrag
news:h6gnpv08iqeuj6krbc7b9d51ugk42aeua8@4ax.com...
> il Sun, 26 Oct 2003 13:39:26 +0100, Simon Strandgaard
> <qj5nd7l02@sneakemail.com> ha scritto::
>
> >On Sun, 26 Oct 2003 12:20:10 +0000, Jason Williams wrote:
>
>
> >
> >Sorry.. that doesn't work
> >
> >
> >server> ruby a.rb
> >a.rb:9: dynamic constant assignment
> > X = LongModuleName1
> > ^
> >server> expand -t2 a.rb
> >module LongModuleName1
> > class Test
> > def to_s; "1" end
> > end
> >end
> >
> >class Test
> > def initialize
> > X = LongModuleName1
> > @value = X::Test.new
> > end
> > def to_s
> > @value.to_s
> > end
> >end
>
> this fails cause you are doing it in a method.
> You can do it safely with const_set
>
> irb(main):001:0> def f
> irb(main):002:1> Object::const_set 'X',Math::PI
> irb(main):003:1> end
> => nil
> irb(main):004:0> f
> => 3.14159265358979
> irb(main):005:0> X
> => 3.14159265358979

Much simpler - just directly assign it in class scope:

module LongModuleName1
class Test
def to_s; "1" end
end
end

class Test
X = LongModuleName1

def initialize
@value = X::Test.new
end
def to_s
@value.to_s
end
end

p Test.new.to_s # => "1"

robert

Robert Klemme

10/27/2003 9:22:00 AM

0


"Simon Strandgaard" <qj5nd7l02@sneakemail.com> schrieb im Newsbeitrag
news:pan.2003.10.26.12.42.21.484769@sneakemail.com...
> On Sun, 26 Oct 2003 12:20:10 +0000, Jason Williams wrote:
>
> > In article <pan.2003.10.26.12.18.37.847429@sneakemail.com>, Simon
> > Strandgaard wrote:
> >> Q2: Is it possible to make an alias for a module, so that I for
instance
> >> only have to type X::Test when I refer to LongModuleName1::Test ?
> >
> > This bit's easy enough - "X = LongModuleName1" should work fine, I
> > think.
>
> However using a lowercase letter, then it works :-)

Well, then x is a locl variable. But if you generally want to refer to
LongModuleName1 as X inside class Test then a constant is much better,
because it's more efficient (only one assignment):

> server> ruby a.rb
> "1"
> server> expand -t2 a.rb
> module LongModuleName1
> class Test
> def to_s; "1" end
> end
> end
>
> class Test

X = LongModuleName1

> def initialize
> @value = x::Test.new
> end
> def to_s
> @value.to_s
> end
> end
>
> p Test.new.to_s # => "1"
> server>

Cheers

robert