Skip to content

Instantly share code, notes, and snippets.

@rskelley9
Last active February 21, 2019 08:19
Show Gist options
  • Save rskelley9/a5639ed47aca2d3ba9ebbff87fd6e3c8 to your computer and use it in GitHub Desktop.
Save rskelley9/a5639ed47aca2d3ba9ebbff87fd6e3c8 to your computer and use it in GitHub Desktop.
Demonstrate concepts of recursion and blocks
class Array
def my_map
new_arr = []
for el in self
new_arr.push(yield(el))
end
new_arr
end
def my_map!
ix = 0
for el in self
self[ix] = yield(el)
ix += 1
end
self
end
end
class Hash
def my_map
new_hash = {}
self.each do |k, v|
new_hash[k] = yield(k, v)
end
new_hash
end
def my_map!
self.each do |k,v|
self[k] = yield(k, v)
end
self
end
end
def is_pal?(str)
return true if str.strip.length <= 1
str_array = str.scan(/\w/)
str_next = str_array[1..-2].join
return false unless str_array.first == str_array.last
is_pal?(str_next)
end
is_pal?('racecar')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment