[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Some quick questions

Ross Bamford

12/9/2005 10:50:00 AM

Hi,

I have a couple more questions. Hopefully they're not so dumb this time.

1) I need a hash that maintains insertion order. In Java, I'd use
LinkedHashMap. Does Ruby have one?

2) I like documentation. So far, Rdoc is great, but I wonder about the
following case:

class SomeClass
attr_accessor :someattr

# But I need to validate, for example, so ...
def someattr=(i)
@someattr unless i < 10
end
end

In the above case, Ruby warns about the method being redefined (and the
original being discarded). However, if I change it to attr_reader to avoid
that (i.e. manually make the reader) then RDoc lists the attribute as
read-only, with the writer shown as a normal method. This is perfectly
reasonable, but I feel there's probably a way around it.

Thanks in advance,
Ross

--
Ross Bamford - rosco@roscopeco.remove.co.uk
"\e[1;31mL"
8 Answers

Ross Bamford

12/9/2005 11:46:00 AM

0

On Fri, 09 Dec 2005 10:50:29 -0000, Ross Bamford
<rosco@roscopeco.remove.co.uk> wrote:

> Hi,
>
> I have a couple more questions. Hopefully they're not so dumb this time.
>

Ahh well... This is why I hate asking questions... :P

Forget this one:

> 1) I need a hash that maintains insertion order. In Java, I'd use
> LinkedHashMap. Does Ruby have one?
>

Got it, in facets. I discounted the sorted hash thread as not what I want,
but I see that someone there mentions it as unsuitable for their needs :)

--
Ross Bamford - rosco@roscopeco.remove.co.uk
"\e[1;31mL"

Steve Litt

12/9/2005 12:48:00 PM

0

On Friday 09 December 2005 05:57 am, Ross Bamford wrote:
> Hi,
>
> I have a couple more questions. Hopefully they're not so dumb this time.
>
> 1) I need a hash that maintains insertion order. In Java, I'd use
> LinkedHashMap. Does Ruby have one?

I've only done Ruby for 9 days now, but from the reading I've done, no. It
would be easy enough to insert the key in an array at the same time you
insert the key=>value in a hash. You could even make a class that does it all
for you.

Perhaps there's a better way, but that's one I'm sure would work.

By the way, why do you need initial insertion order? Do you ever need to look
up by the key value? If not, why not use an array of hashes, or an array of 2
element arrays?

SteveT

Steve Litt
http://www.troublesh...
slitt@troubleshooters.com


James Gray

12/9/2005 1:46:00 PM

0

On Dec 9, 2005, at 4:57 AM, Ross Bamford wrote:

> 2) I like documentation. So far, Rdoc is great, but I wonder about
> the following case:
>
> class SomeClass
> attr_accessor :someattr
>
> # But I need to validate, for example, so ...
> def someattr=(i)
> @someattr unless i < 10
> end
> end
>
> In the above case, Ruby warns about the method being redefined (and
> the original being discarded). However, if I change it to
> attr_reader to avoid that (i.e. manually make the reader) then RDoc
> lists the attribute as read-only, with the writer shown as a normal
> method. This is perfectly reasonable, but I feel there's probably a
> way around it.

You could just define both manually.

James Edward Gray II


Ross Bamford

12/9/2005 1:52:00 PM

0

On Fri, 09 Dec 2005 21:48:24 +0900, Steve Litt wrote:

> On Friday 09 December 2005 05:57 am, Ross Bamford wrote:
>> Hi,
>>
>> I have a couple more questions. Hopefully they're not so dumb this time.
>>
>> 1) I need a hash that maintains insertion order. In Java, I'd use
>> LinkedHashMap. Does Ruby have one?
>
> I've only done Ruby for 9 days now, but from the reading I've done, no. It
> would be easy enough to insert the key in an array at the same time you
> insert the key=>value in a hash. You could even make a class that does it all
> for you.
>

(I'm a relative newbie too :) And just loving Ruby more and more!)

I think I found something in facets that does the trick, but I've not
gotten to looking at it yet so I'm not certain. But anyway, I think a
variant of your suggestion is more suitable.

> Perhaps there's a better way, but that's one I'm sure would work.
>
> By the way, why do you need initial insertion order? Do you ever need to look
> up by the key value? If not, why not use an array of hashes, or an array of 2
> element arrays?
>

The idea was to make a Hash that accepted Regex keys, but allowed you to
look for them by a string, using backrefs in the value:

class RxHash < Hash
def [](key)
md = nil
if v = self.detect { |k,v| md = /^#{k}$/.match(key) }
v[1].gsub(/\$(\d)/) { md[$1.to_i] }
end
end
end

You can then have mappings like /123([a-z]+)/ => '321$1'. It was for
mapping file extensions. It works, but if there are multiple matches you
get one pretty much at random.

Thinking more on it, though, I see that the Hash is probably leading me
down the wrong path anyway, it's better done with arrays as you suggest.
I think I can have an array of two element arrays and use 'map' for easy
access.

Thanks for the reply,
Ross

--
Ross Bamford - rosco@roscopeco.remove.co.uk
"\e[1;31mL"

Ross Bamford

12/9/2005 2:11:00 PM

0

Yes, I suppose at least they're together in the doc then :)

I just hoped I might be able to get them shown in the 'attributes'
table, with their access listed and so on ( [RW] or whatever).

Thanks for the reply :)

Mark J. Reed

12/9/2005 3:09:00 PM

0

James Edward Gray II <james@grayproductions.net> writes:

>On Dec 9, 2005, at 4:57 AM, Ross Bamford wrote:

>> 2) I like documentation. So far, Rdoc is great, but I wonder about
>> the following case:
>>
>> class SomeClass
>> attr_accessor :someattr
>>
>> # But I need to validate, for example, so ...
>> def someattr=(i)
>> @someattr unless i < 10
>> end
>> end

Don't use attr_accessor, then. attr_accessor doesn't do any magic; it's
just a shorthand that defines default accessor methods.
This line:

attr_accessor :foo

has identical results to this code:

def foo
@foo
end

def foo=(new_foo)
@foo = new_foo
end

So if you're going to define your own foo and foo=, just leave out the
attr_accessor line. If you're only making your own foo=, you can use
attr_reader to get foo():

attr_reader :foo
def foo=
...
end

The other way around is less common, but still doable:

attr_writer :foo
def foo
...
end

james_b

12/9/2005 3:37:00 PM

0

Mark J.Reed wrote:
> James Edward Gray II <james@grayproductions.net> writes:
>
>
>>On Dec 9, 2005, at 4:57 AM, Ross Bamford wrote:
>
>
>>>2) I like documentation. So far, Rdoc is great, but I wonder about
>>>the following case:
>>>
>>> class SomeClass
>>> attr_accessor :someattr
>>>
>>> # But I need to validate, for example, so ...
>>> def someattr=(i)
>>> @someattr unless i < 10
>>> end
>>> end
>
>
> Don't use attr_accessor, then. attr_accessor doesn't do any magic; it's
> just a shorthand that defines default accessor methods.
> This line:
>
> attr_accessor :foo
>
> has identical results to this code:
>
> def foo
> @foo
> end
>
> def foo=(new_foo)
> @foo = new_foo
> end
>

Almost. The rdoc output is different. Using attr_accessor, rdoc does
not document 'foo' as being a method, but as being an attribute.

Unless you both use attr_accessor *and* define method bodies. Then
rdoc does both; foo is documented as both an attribute and a method.

James
--

http://www.ru... - Ruby Help & Documentation
http://www.artima.c... - Ruby Code & Style: Writers wanted
http://www.rub... - The Ruby Store for Ruby Stuff
http://www.jame... - Playing with Better Toys
http://www.30seco... - Building Better Tools


Ross Bamford

12/9/2005 4:14:00 PM

0

On Sat, 10 Dec 2005 00:36:34 +0900, James Britt wrote:
> Mark J.Reed wrote:
>> This line:
>>
>> attr_accessor :foo
>>
>> has identical results to this code:
>>
>> def foo
>> @foo
>> end
>>
>> def foo=(new_foo)
>> @foo = new_foo
>> end
>>
>
> Almost. The rdoc output is different. Using attr_accessor, rdoc does
> not document 'foo' as being a method, but as being an attribute.
>
> Unless you both use attr_accessor *and* define method bodies. Then
> rdoc does both; foo is documented as both an attribute and a method.
>
> James

Yup, that's what I mean. I found I could do that, but I didn't notice
it had documented them twice (just tried it and of course you're right),
but I did see that Ruby warns me that the previous definition was
discarded. It's not really a problem, but I do like to strive to be
warning-free :P

Cheers,
Ross

--
Ross Bamford - rosco@roscopeco.remove.co.uk
"\e[1;31mL"