# Some quick questions

**URL:** <https://www.ruby-forum.com/t/some-quick-questions/49856>\
**Category:** Ruby\
**Created:** [December 9, 2005, 11:58am UTC](https://www.ruby-forum.com/t/some-quick-questions/49856 "2005-12-09T11:58:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rosco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/50afbb/32.png) [@rosco](https://www.ruby-forum.com/u/rosco)\
**Post date:** [December 9, 2005, 11:58am UTC](https://www.ruby-forum.com/t/some-quick-questions/49856/1 "2005-12-09T11:58:43Z")

</div>

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:

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

---

<div class="post-metadata">

**Author:** ![rosco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/50afbb/32.png) [@rosco](https://www.ruby-forum.com/u/rosco)\
**Post date:** [December 9, 2005, 12:55pm UTC](https://www.ruby-forum.com/t/some-quick-questions/49856/2 "2005-12-09T12:55:51Z")

</div>

On Fri, 09 Dec 2005 10:50:29 -0000, Ross B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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… 😛

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  
🙂

---

<div class="post-metadata">

**Author:** ![rosco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/50afbb/32.png) [@rosco](https://www.ruby-forum.com/u/rosco)\
**Post date:** [December 9, 2005, 1:49pm UTC](https://www.ruby-forum.com/t/some-quick-questions/49856/3 "2005-12-09T13:49:21Z")

</div>

On Friday 09 December 2005 05:57 am, Ross B. 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 L.

> **[Troubleshooters.Com](http://www.troubleshooters.com)**
>
> The process and mindset of troubleshooting.

removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![rosco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/50afbb/32.png) [@rosco](https://www.ruby-forum.com/u/rosco)\
**Post date:** [December 9, 2005, 3:15pm UTC](https://www.ruby-forum.com/t/some-quick-questions/49856/4 "2005-12-09T15:15:23Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![rosco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/50afbb/32.png) [@rosco](https://www.ruby-forum.com/u/rosco)\
**Post date:** [December 9, 2005, 2:50pm UTC](https://www.ruby-forum.com/t/some-quick-questions/49856/5 "2005-12-09T14:50:41Z")

</div>

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

> 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 G. II

---

<div class="post-metadata">

**Author:** ![rosco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/50afbb/32.png) [@rosco](https://www.ruby-forum.com/u/rosco)\
**Post date:** [December 9, 2005, 4:16pm UTC](https://www.ruby-forum.com/t/some-quick-questions/49856/6 "2005-12-09T16:16:42Z")

</div>

James Edward G. II [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

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

> > 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

```

---

<div class="post-metadata">

**Author:** ![rosco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/50afbb/32.png) [@rosco](https://www.ruby-forum.com/u/rosco)\
**Post date:** [December 9, 2005, 5:26pm UTC](https://www.ruby-forum.com/t/some-quick-questions/49856/7 "2005-12-09T17:26:12Z")

</div>

On Sat, 10 Dec 2005 00:36:34 +0900, James B. wrote:

> > 
> 
> 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 😛

Cheers,  
Ross

---

<div class="post-metadata">

**Author:** ![rosco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/50afbb/32.png) [@rosco](https://www.ruby-forum.com/u/rosco)\
**Post date:** [December 9, 2005, 4:37pm UTC](https://www.ruby-forum.com/t/some-quick-questions/49856/8 "2005-12-09T16:37:11Z")

</div>

Mark J.Reed wrote:

> > > attr\_accessor :someattr  
> > > This line:  
> > > @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.ruby-doc.org](http://www.ruby-doc.org) - Ruby Help & Documentation  
[Ruby Code & Style](http://www.artima.com/rubycs/) - Ruby Code & Style: Writers wanted  
[http://www.rubystuff.com](http://www.rubystuff.com) - The Ruby Store for Ruby Stuff  
[http://www.jamesbritt.com](http://www.jamesbritt.com) - Playing with Better Toys  
[http://www.30secondrule.com](http://www.30secondrule.com) - Building Better Tools
