E
E
eldar_web2015-08-14 15:43:07
htaccess
eldar_web, 2015-08-14 15:43:07

How to sort and output unique hash content in Ruby?

For example, there is a hash:
list['first'] = 'BMW';
list['second'] = 'BMW';
list['third'] = 'MERCEDES';
How to sort this hash by uniqueness???

Answer the question

In order to leave comments, you need to log in

5 answer(s)
D
dodo512, 2019-02-07
@Huf

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{THE_REQUEST} " /(index\.php|([^?]*)\.php)"
RewriteRule ^ http://www.site.ru/%2 [R=301,L,NE]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ http://www.site.ru/$1 [R=301,L]

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.site.ru/$1 [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

V
Viktor Taran, 2019-02-13
@shambler81

Just removing the slash is not the best idea, sometimes it should be closed, and most importantly, these two rules work together
https://klondike-studio.ru/standards/standartnyy-h...
Here I described in more detail

T
thepry, 2015-08-14
@eldar_web

UPDATE:
list = {'first' => 'BMW', 'second' => 'BMW', 'third' = 'MERCEDES'; }
And I need:
list = {'first' => 'BMW', 'third' = 'MERCEDES'; }
If you don't care which key will be chosen, then

list.invert.invert => {"second"=>"BMW", "third"=>"MERCEDES"}

If you don't give a damn, then
Hash[list.to_a.uniq{ |v| v.last}] => {"first"=>"BMW", "third"=>"MERCEDES"}

list['first'] = 'BMW';
list['second'] = 'BMW';
list['third'] = 'MERCEDES';
How to sort this hash by uniqueness???

A hash is an associative array. It doesn't have sorting.
Did I understand correctly that you need to select only those key, value pairs whose value is unique in the entire hash? If yes, then like this:
h = { a: 'bmw', b: 'bmw', c: 'lada kalina' }
count = h.values.group_by(&:to_s)
h.select{ |_, v| count[v].size == 1 }  => {:c=>"lada kalina"}

You can also sort, but you get an array:
h.sort_by{ |_, v| count[v].size } =>  

N
Nikolai Markov, 2015-08-14
@manameiz

values ​​= []
list.keys.each do |key|
if values.include?(list[key])
list.delete(key)
else
values ​​<< list[key]
end
end

T
TyzhSysAdmin, 2015-08-14
@POS_troi

a = [ "a", "a", "b", "b", "c" ]
a.uniq   # => ["a", "b", "c"]

b = 
b.uniq { |s| s.first } # => 

ruby-doc.org/core-2.2.0/Array.html#method-i-uniq
Do you really write down the hash of arrays this way?
list = {first:'BMW', seccond:'BMW', third:'MERCEDES'}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question