rub 1.8.1 and $=

  • Thread starter Patrick Gundlach
  • Start date
P

Patrick Gundlach

Hello out there,

I have the following code to sort an array:

def Myclass
def sort
# $= =false => case sensitive
oldequal=$=
$= =1
@allcmds.sort!
$= =oldequal
end
end

And ruby 1.8.1 gives me a warning, that setting $= is deprecated. How
should I rather do the case insensitive sort?

Patrick
 
R

Robert Klemme

Patrick Gundlach said:
Hello out there,

I have the following code to sort an array:

def Myclass
def sort
# $= =false => case sensitive
oldequal=$=
$= =1
@allcmds.sort!
$= =oldequal
end
end

And ruby 1.8.1 gives me a warning, that setting $= is deprecated. How
should I rather do the case insensitive sort?

Patrick

class Myclass
def sort
@allcmds.sort! {|a,b| a.casecmp b}
end
end

robert
 
P

Patrick Gundlach

Hello Robert,
class Myclass
def sort
@allcmds.sort! {|a,b| a.casecmp b}
end
end

Oh, that's new in 1.8.0. I'll read the news section more carefully
next time. Thank you for pointing out casecmp.

Patrick
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: rub 1.8.1 and $="

|> class Myclass
|> def sort
|> @allcmds.sort! {|a,b| a.casecmp b}
|> end
|> end
|
|Oh, that's new in 1.8.0. I'll read the news section more carefully
|next time. Thank you for pointing out casecmp.

FYI, this also work

class Myclass
def sort
@allcmds.sort_by{|x| x.upcase}
end
end

Note "sort_by" do not modify @allcmds.

matz.
 
R

Robert Klemme

Patrick Gundlach said:
Hello Robert,


Oh, that's new in 1.8.0. I'll read the news section more carefully
next time. Thank you for pointing out casecmp.

With older versions you can use

@allcmds.sort! {|a,b| a.upcase <=> b.upcase}

Regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top