2

I don't get why reversed_string=string[i] + reversed_string puts the last char first. It seems that string[i] would index the first char and not the last. So if the string was "abc" index 0 would be 'a' and not 'c'. Could someone please explain how ruby gets 'c' from index 0? And then, of course, 'b' from index 1? Etc, etc.

Write a method that will take a string as input, and return a new string with the same letters in reverse order.

Difficulty: easy.

def reverse(string)
  reversed_string = ""

  i = 0
  while i < string.length
    reversed_string = string[i] + reversed_string

    i += 1
  end

  return reversed_string
end
puts("reverse(\"abc\") == \"cba\": #{reverse("abc") == "cba"}")
puts("reverse(\"a\") == \"a\": #{reverse("a") == "a"}")
puts("reverse(\"\") == \"\": #{reverse("") == ""}")
2
  • "abcdefg".reverse! => "gfedcba" Commented Nov 6, 2014 at 2:11
  • 2
    @scottxu I think this question is intended to practice programming by writing a method that reverses a string. Commented Nov 6, 2014 at 2:18

3 Answers 3

5
reversed_string = string[i] + reversed_string

For example, if string is "abc", string[0] is indeed "a", but here it's being put in the beginning of reversed_string, not the end. reversed_string is added up in each iteration as:

"a" + ""     #string[0] + ""  => "a"
"b" + "a"    #string[1] + "a" => "ba"
"c" + "ba"   #string[2] + "ba"=> "cba"
Sign up to request clarification or add additional context in comments.

Comments

0

Assuming you can't use Ruby Class String's built in Reverse method, you could try the following

def reverse_string(string)
  new_string = []
  i = string.length-1
  while i >= 0
    new_string.push(string[i])
    i -= 1
  end
  new_string.join
end

This will create a new string object, but it will reverse the string without using any built-in methods.

1 Comment

You are using built in methods. push and length for example.
0

As you know, there is a method String#reverse to reverse a string. I understand you are not to use that method, but instead write your own, where the method's argument is the string to be reversed. Others will suggest ways you might do that.

As you are new to Ruby, I thought it might be instructive to show you how you could write a new method for the String class, say, String#my_reverse, that behaves exactly the same as String#reverse. Then for the string "three blind mice", we would have:

"three blind mice".reverse    #=> "ecim dnilb eerht"
"three blind mice".my_reverse #=> "ecim dnilb eerht"

To create a method without arguments for the String class, we normally do it like this:

class String
  def my_method
    ...
  end
end

We invoke my_method by sending it a receiver that is an instance of the String class. For example, if write:

"three blind mice".my_method

we are sending the method String#my_method to the receiver "three blind mice". Within the definition of the method, the receiver is referred to as self. Here self would be "three blind mice". Similarly, just as the second character (at offset 1) of that string is "three blind mice"[1] #=> "h", self[1] #=> "h". We can check that:

class String
  def my_method
    puts "I is '#{self}'"
    (0...self.size).each { |i| puts self[i] }
  end
end

"three blind mice".my_method

would print:

I is 'three blind mice'
t
h
r
e
e

b
...
c
e

The method my_reverse is almost the same:

class String
  def my_reverse
    sz = self.size
    str = ''
    (0...sz).each { |i| str << self[sz-1-i] }
    str
  end
end

"three blind mice".my_reverse
  #=> "ecim dnilb eerht"

You can think of self as a variable whose value is the receiver, but unlike a variable, you cannot reassign self to a different object. For example, we can write x = 1; x = 'cat', but we cannot write self = 'cat'. As we have already seen, however, we can change the references self makes to other objects, such as self[1] = 'r'.

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.