Last active
August 5, 2017 04:20
-
-
Save subratrout/58848394af50c7db7ccd75a910174a32 to your computer and use it in GitHub Desktop.
RubyMonk Snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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