0 Replies - 1455 Views - Last Post: 15 November 2013 - 02:52 PM

#1 Lemur   User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1453
  • View blog
  • Posts: 3,633
  • Joined: 28-November 09

Character Counter

Posted 15 November 2013 - 02:52 PM

Description: Example in Pry (IRB Alternative)

[1] pry(main)> str = 'lorem ipsum dolor sit amet'
=> "lorem ipsum dolor sit amet"
[2] pry(main)> str.each_char.each_with_object(Hash.new(0)) { |ch,hash| hash[ch] += 1 }
=> {"l"=>2,
"o"=>3,
"r"=>2,
"e"=>2,
"m"=>3,
" "=>4,
"i"=>2,
"p"=>1,
"s"=>2,
"u"=>1,
"d"=>1,
"t"=>2,
"a"=>1}
Counts occurrences of characters
str.each_char.each_with_object(Hash.new(0)) { |ch,hash| hash[ch] += 1 }


Is This A Good Question/Topic? 0
  • +

Page 1 of 1