Skip to content

Instantly share code, notes, and snippets.

@kareemgrant
Created April 2, 2015 05:30
Show Gist options
  • Save kareemgrant/a86bdabf20b1371daac7 to your computer and use it in GitHub Desktop.
Save kareemgrant/a86bdabf20b1371daac7 to your computer and use it in GitHub Desktop.
class Scrabble
def score(word)
letters = word.upcase.split('')
total = 0
letters.each do |letter|
total += letter_scores[letter]
end
total
end
def letter_scores
{ "A"=>1, "B"=>3, "C"=>3, "D"=>2,
"E"=>1, "F"=>4, "G"=>2, "H"=>4,
"I"=>1, "J"=>8, "K"=>5, "L"=>1,
"M"=>3, "N"=>1, "O"=>1, "P"=>3,
"Q"=>10, "R"=>1, "S"=>1, "T"=>1,
"U"=>1, "V"=>4, "W"=>4, "X"=>8,
"Y"=>4, "Z"=>10
}
end
end
puts Scrabble.new.score("ruBy")
class WordCounter
attr_reader :words
def initialize(content)
@words = content.downcase.gsub(/[.,]/, '').split(' ')
end
def count
word_frequency = Hash.new(0)
words.each do |word|
word_frequency[word] += 1
end
word_frequency.sort_by {|key, value| value }.reverse.to_h
end
end
text = "Initially, Matz looked at other languages to find an ideal syntax. Recalling his search, he said, “I wanted a scripting language that was more powerful than Perl, and more object-oriented than Python.”
In Ruby, everything is an object. Every bit of information and code can be given their own properties and actions. Object-oriented programming calls properties by the name instance variables and actions are known as methods. Ruby’s pure object-oriented approach is most commonly demonstrated by a bit of code which applies an action to a number.
Ruby is a language of careful balance. Its creator, Yukihiro “Matz” Matsumoto, blended parts of his favorite languages (Perl, Smalltalk, Eiffel, Ada, and Lisp) to form a new language that balanced functional programming with imperative programming.
He has often said that he is “trying to make Ruby natural, not simple,” in a way that mirrors life.
Building on this, he adds:
Ruby is simple in appearance, but is very complex inside, just like our human body"
puts WordCounter.new(text).count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment