-
Website
None -
Original page
http://e168f07.7fff.com/2007/10/24/your-one-liners/ -
Subscribe
All Comments -
Community
-
Top Commenters
-
steveharris
11 comments · 2 points
-
lleahy
1 comment · 1 points
-
Daniel Ha
2 comments · 435 points
-
amynewell
1 comment · 2 points
-
Mhoram
1 comment · 1 points
-
-
Popular Threads
Hash#invert returns a new hash where the keys of the new hash are the values of the old hash, and vice-versa. If the original hash contains multiple keys that map to the same value, the new hash will contain a single entry for that value (now the new key) and which of the original keys becomes the new value is not predictable.
Write a one-liner which inverts a hash, but where each value in the new hash is an array containing all the keys of the original hash.
Now, write a one-liner like the above, but if there is only one (new) value, it should be a simple value, not an array.
- Steve
I think this covers most situations, but "\b" may be a little obscure.
s.split(/\b/).map {|w| d.include?(w) ? w[0].chr + "*" * (w.length - 1) : w}.join
I'll leave the substring-based solution to someone else.
Also, what I posted censors case-sensitively. That probably ought to be corrected, too. Making it:
d.map {|b| b.upcase}.include?(w.upcase)
should take case of that.
Oneliners that try to cover all the bases seem to get quite ornate.