From: daz Date: 2003-06-06T14:22:40+09:00 Subject: Re: Array = Hash.sort ... but sometimes not "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 > > 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 ;) 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" # Proc (19656176) # #---------------------------------------- # String (19656212) "sucre" # String (19656212) "sucre" # Proc (19656176) # #---------------------------------------- #grand bien vous fasse! ("Much good it will do for you!") Ruby will do you much good, though. Bon chance, daz