From: Rubist Rohit Date: 2011-04-22T14:07:14+09:00 Subject: Re: Reversing a string without using array, classes and reverse function I attempted below given code, but it is neither displaying result nor error: ================CODE========================== s = "This is to test reverse of a string" len = s.length for j in len..1 do mycommand = "s.scan(/.$/) {|x| puts x}" mycommand = mycommand.insert 7,"." end ============================================== What I am doing is to insert a period (.) in the seventh or eighth position on each loop. spiralofhope wrote in post #994433: > On Fri, 22 Apr 2011 12:54:16 +0900 >> I am trying this: >> >> mystring = gets >> mystring.scan(/..$/) {|x| puts x} >> >> It returns only the last character. Is it possible to add the above >> line in loop? > > Here's something I stumbled through which seems to work. > > - Using a regex of /.$/ > - Slowly chomping away at the original string. > - Using another variable to build my result. > > mystring = 'Hello, World!' > result = '' > > fail = 0 > until fail == "100" or mystring == '' do > fail += 1 > mystring.match( %r{(.$)} ) > break if $~ == nil > result += $~[1] > mystring = mystring.chomp( $~[1] ) > end > > puts result -- Posted via http://www.ruby-forum.com/.