stdlib: result from join_keys_to_values() is unstable

31 views
Skip to first unread message

Gabriel Filion

unread,
Mar 11, 2014, 12:27:30 PM3/11/14
to puppet...@googlegroups.com
Hi there,

Say I have a hash like {a=>'1',b=>'2'} and I'd like to get a string that
looks like "a=1,b=2". I tried using a combination of
join_keys_to_values() and join() and the result looked correct, but was
unstable. That unstability is because hashes are not always "sorted" the
same way.

An unstable string means that the config file in which I use it gets
modified for no reason from time to time and this triggers an
unnecessary service refresh.

I'm currently using an inline template to achieve what I want, since
with ruby I can use h.sort.map to always get results in the same order
from a hash "h".

Thing is, stdlib's sort() function only accepts strings or arrays.


Is there a way to get a "stable" string with current means?

Otherwise, would it be an acceptable idea to add support for hashes in
sort() ?

--
Gabriel Filion

signature.asc

Matthaus Owens

unread,
Mar 11, 2014, 12:58:29 PM3/11/14
to Puppet Users
Gabriel,
I don't know about the specific implementation of join_keys_to_values,
but ruby's hash behavior changed between the 1.8 series and 1.9 and
later[1]. In ruby 1.8 hashes are enumerated in a seemingly unsorted
manner, while in ruby 1.9 and later hashes are enumerated in insertion
order. If you are on ruby 1.8, it would be worth seeing if you
experience the same issues in ruby 1.9 or later.

[1] - http://stackoverflow.com/questions/7065915/why-is-my-hash-autosorting-itself
--
Matthaus Owens
Release Manager, Puppet Labs

Join us at PuppetConf 2014, September 23-24 in San Francisco -
http://bit.ly/pupconf14
Register now and save $350!

Gabriel Filion

unread,
Mar 11, 2014, 2:34:29 PM3/11/14
to puppet...@googlegroups.com
On 11/03/14 12:58 PM, Matthaus Owens wrote:
> I don't know about the specific implementation of join_keys_to_values,
> but ruby's hash behavior changed between the 1.8 series and 1.9 and
> later[1]. In ruby 1.8 hashes are enumerated in a seemingly unsorted
> manner, while in ruby 1.9 and later hashes are enumerated in insertion
> order. If you are on ruby 1.8, it would be worth seeing if you
> experience the same issues in ruby 1.9 or later.
>
> [1] - http://stackoverflow.com/questions/7065915/why-is-my-hash-autosorting-itself

oh, well that's interesting.

thanks for the feedback, I'll se about using ruby 1.9.x
signature.asc
Reply all
Reply to author
Forward
0 new messages