Skip to content

Instantly share code, notes, and snippets.

@foxweb
Last active April 21, 2022 12:39
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 foxweb/a93c6aa40e793a85b775c1febf888258 to your computer and use it in GitHub Desktop.
Save foxweb/a93c6aa40e793a85b775c1febf888258 to your computer and use it in GitHub Desktop.
a = [0, 1, 2, 3, 4, 5]
# example 1
def reverse(array)
array.each_with_index do |val, index|
break if index >= array.count / 2
temp = array[index]
array[index], array[-index-1] = array[-index-1], temp
end
return array
end
reverse(a)
# example 2
def reverse(array)
array.reduce([]) do |memo, item|
memo.unshift(item)
end
end
reverse(a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment