ruby sort hash by value

lets say you have a hash
hh = {1=>'y', 2=>'a'}
if you need to sort a hash by its value this works like this
Hash[hh.sort_by{|k, v| v}]

in case your hash looks like this
hh = {1=>'y',2=>'A'}
you should be aware to change your value to lower case like this
Hash[hh.sort_by{|k, v| v.downcase}]

in case your hash looks like this
hh = {1=>0.0,2=>nil,3=>4.0}
you need to replace nil with a sortable value
hh.sort_by{|k, v| v ? v : 0}

sub! problem

I had a problem caused by the sub! function.
What I did:
myobj = Model.create({"field"=>"text"})
myobj.field.sub! "text", "replacedwith"
myobj.save
puts myobj.field
#returns replacedwith
myobj.reload
puts myobj.field
#returns text

using this line it works
myobj.field = myobj.field.sub "text", "replacedwith"

I am using rails 4.1.5 and ruby 2.0.0