Array = Hash.sort ... but sometimes not

(sorry for my poor English : I’m French)

I created a little program in Ruby as an exercice to learn more about this
fascinating language.
The problem is that I can change the type of one of my variables without any
problem for Ruby.
A few lines from my program :

mots_tries = Array.new
mots_occur = Hash.new

I fill my Hash with some data and after that I sort it by values :

mots_tries = mots_occur.sort {|a,b| a[1]<=>b[1]}

My program works perfectly, … even when I declare mots_tries as an Hash :
mots_tries = Hash.new

But I know that the result of an Hash sorting is a nested Array.
For information I work with Ruby 1.6.8-8 and my program has nor Class nor
Def (I used Ruby as a procedural programming language).

Thanks for your answer if someone has ever heard of this problem with Ruby.

  • Teknophil -

When you say:
mots_tries = Array.new
you are assigning the variable mots_tries to be a new empty Array.

When you say:
mots_tries = mots_occur.sort {|a,b| a[1]<=>b[1]}
you are reassigning the variable to be result of the method sort.

When you do this, the old empty Array no longer has any references to
it, and it eventually will be garbage collected. Basically, there is no
reason to indicate that mots_tries = Array.new in this case, as you then
reassign it immediately to something else. The same thing happens when
you assign it to Hash.new… the next call reassigns the variable and
the empty hash is ‘lost’.

Variables in Ruby can point to objects of any type. Objects are typed,
but not variables. Does that make sense? This is the way Ruby works,
it is not a problem.

For example, this program is all completely legal:

foo = “a”
puts foo.type # this will be String
foo = 1
puts foo.type # this will be Fixnum
foo = 16.1
puts foo.type # this will be Float
foo = File.new(“temp”)
puts foo.type # this will be File

HTH,

Brett

···

On Jun 6, Anubis wrote:

(sorry for my poor English : I’m French)

I created a little program in Ruby as an exercice to learn more about this
fascinating language.
The problem is that I can change the type of one of my variables without any
problem for Ruby.
A few lines from my program :

mots_tries = Array.new
mots_occur = Hash.new

I fill my Hash with some data and after that I sort it by values :

mots_tries = mots_occur.sort {|a,b| a[1]<=>b[1]}

My program works perfectly, … even when I declare mots_tries as an Hash :
mots_tries = Hash.new

(sorry for my poor English : I’m French)

I created a little program in Ruby as an exercice to learn more
about this fascinating language.
The problem is that I can change the type of one of my variables
without any problem for Ruby.
A few lines from my program :

mots_tries = Array.new
mots_occur = Hash.new

I fill my Hash with some data and after that I sort it by values :

mots_tries = mots_occur.sort {|a,b| a[1]<=>b[1]}

My program works perfectly, … even when I declare mots_tries as an Hash :
mots_tries = Hash.new

But I know that the result of an Hash sorting is a nested Array.
For information I work with Ruby 1.6.8-8 and my program has nor Class nor
Def (I used Ruby as a procedural programming language).

Thanks for your answer if someone has ever heard of this problem with Ruby.

  • Teknophil -

Bienvenue.

(sorry for my absent French : I’m English :wink:

You don’t need ‘mots_tries = Array.new’. Just …

mots_occur = Hash.new

[… fill Hash with some data …]

mots_tries = mots_occur.sort {|a,b| a[1]<=>b[1]}

A variable is just an alias (reference / label) to important stuff.
As you said, the result of an Hash sorting is a nested Array,
which is the real object referred to now as - mots_tries.

This is not a problem unless you are expecting something different.
(Maybe learned from a different PL.)

Use Ruby as a procedural PL if you like but, as you see examples,
you’ll see you’re missing most of ‘le pouvoir’.

def indiq(*args)
args.each do |arg|
printf("%8s (%8d) ", arg.class, arg.id)
p arg
end
puts ‘-’ * 40
end

objet1 = [“sel”]
objet2 = “sucre”
objet3 = proc do
puts “grand bien vous fasse!”
end
indiq(objet1, objet2, objet3)
objet1 = objet2
indiq(objet1, objet2, objet3)
objet3

Array (19659692) [“sel”]

String (19656212) “sucre”

···

“Anubis” philippe.deschamps@sympatico.ca wrote:

Proc (19656176) #Proc:0x0257dc10@C:/TEMP/rbF2A0.TMP:12

#----------------------------------------

String (19656212) “sucre”

String (19656212) “sucre”

Proc (19656176) #Proc:0x0257dc10@C:/TEMP/rbF2A0.TMP:12

#----------------------------------------
#grand bien vous fasse!

(“Much good it will do for you!”)
Ruby will do you much good, though.

Bon chance,

daz