2

This is my array and custom method to reverse an array output without using the reverse method. not sure where it broke, tried running it in console, no dice.

numbers = [1, 2, 3, 4, 5, 6]

def reversal(array)
  do |item1, item2| item2 <=> item1
end

p reversal(numbers)
17
  • 1
    Before one can consider it "broken", it has to have been in a working state, first. Do you understand what <=> does? Commented Oct 15, 2016 at 2:11
  • 2
    Are you trying to make the array reverse-sorted, or just reversed? Like, if you had the array [1,3,2], is the correct output [3,2,1] or [2,3,1]? Commented Oct 15, 2016 at 2:28
  • 3
    Also, there's a pretty significant syntactical error going on here as well... do and the do |stuff| construct are part of a method call, not a method definition. Of course, you can call a method from within the body of another method, but you're not doing that. Also, every do needs a corresponding end regardless. Commented Oct 15, 2016 at 2:30
  • 2
    @WestCoastCharlie Your example doesn't clarify the question, because [1,2,3,4,5,6] is already in order, so putting it backwards from what it is now and backwards from 'in order' is the same thing. What if you had a list that was [1,2,4,5,6,3]? Would you want the result to be [6,5,4,3,2,1] or [3,6,5,4,2,1]? Commented Oct 15, 2016 at 2:49
  • 2
    In any case, a method definition is something that begins with def and ends with end, whereas a method invocation is where you call the method. From def reversal(array) to end is a method definition (though not a valid one). The last line is a method call (actually two method calls, one call to reversal and one call to p). Method calls can include a 'block' (though many don't), which is a bunch of code between do and end or between { and }. Like in @roychri's answer below, he passes a block to the method loop Commented Oct 15, 2016 at 2:54

5 Answers 5

3

Here's one way to handle this. This is not very efficient but works.

def reversal(array)
    reversed = []
    loop do
        reversed << array.pop
        break if array.empty?
    end
    reversed
end

Here is another implementation that does the same thing:

def reversal(array)
    array.each_with_index.map do |value, index|
        array[array.count-index-1]
    end
end
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks roychri. I'm still confused as how a combined comparison operator fits into all this if it doesn't reverse the order of objects.
The comparaison operator is used to sort. What you want to do is reverse the array so that if the array contains [4,2,8] it becomes [8,2,4]. You cannot sort it in that case. just you take the last one and make it first, and so on.
@WestCoastCharlie The combined comparison operator doesn't fit into all of this, it's completely unrelated. That operator is just a combination of <, ==, and > into one operator; so, instead of returning true or false it returns -1 (for 'a is less than b), 0` (for 'a is equal to b') or +1 (for 'a is greater than b').
While both methods return the reverse array, the first empties array: array = [1,2,3]; reversal(array) #=> [3,2,1]; array = []. I think you should either leave arr unchanged or replace it with the arr reversed (depending on West Coast Charlie's needs), but certainly not replace it with an empty array.
3

So many ways... Here are three (#1 being my preference).

numbers6 = [1, 2, 3, 4, 5, 6]
numbers5 = [1, 2, 3, 4, 5]

For all methods my_rev below,

my_rev(numbers6)
  #=> [6, 5, 4, 3, 2, 1]
my_rev(numbers5)
  #=> [5, 4, 3, 2, 1] 

#1

def my_rev(numbers)
  numbers.reverse_each.to_a
end

#2

def my_rev(numbers)
  numbers.each_index.map { |i| numbers[-1-i] }
end

#3

def my_rev(numbers)
  (numbers.size/2).times.with_object(numbers.dup) do |i,a| 
    a[i], a[-1-i] = a[-1-i] , a[i]
  end
end

Comments

1

there are so many ways to do this

1 Conventional way

a=[1,2,3,4,5,6,7,8]
i=1
while i <= a.length/2 do
  temp = a[i-1]
  a[i-1] =  a[a.length-i]
  a[a.length-i] = temp
  i+=1
end

2 Using pop

a=[1,2,3,4,5,6]
i=0
b=[]
t=a.length
while i< t do
  b << a.pop
  i+=1
end

3 Using pop and loop

a=[1,2,3,4,5,6]
b=[]
loop do
  b << a.pop
  break if a.empty?
end

Comments

0
a = [1,2,3,4,5]
b = []
a.length.times { |i| b << a[(i+1)*-1] }
b
=> [5,4,3,2,1]

Comments

0

We can implement the array reversal functionality without using built-in ruby methods.

arr = [1,2,3,4,5]
counter = 0
size = (arr.size/2.0).round(0)

while counter <= size - 1
    first_el = arr[counter]
    arr[counter] = arr[-(counter+1)]
    arr[-(counter+1)] = first_el
    counter += 1
end
p arr

This sample code will shift the array elements without creating a new array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.