Skip to content

Instantly share code, notes, and snippets.

@subratrout
Last active August 5, 2017 04:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save subratrout/58848394af50c7db7ccd75a910174a32 to your computer and use it in GitHub Desktop.
Save subratrout/58848394af50c7db7ccd75a910174a32 to your computer and use it in GitHub Desktop.
RubyMonk Snippets
Try implementing a method called occurrences that accepts a string argument and uses inject to build a Hash. The keys of this hash should be unique words from that string. The value of those keys should be the number of times this word appears in that string.
def occurrences(str)
word_hash = Hash.new(0)
str.scan(/\w+/).inject(word_hash) do | number, word|
number[word.downcase] += 1
number
end
end
Implement a method superclasses inside Object that returns this class hierarchy information? The method has to return an array that
lists the superclasses of the current object.
class Object
def superclasses(klass = self.superclass)
return [] if klass.nil?
[klass] + superclasses(klass.superclass)
end
end
class Bar
end
class Foo < Bar
end
p Foo.superclasses # should be [Bar, Object, BasicObject]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment