From: Josh Cheek Date: 2011-10-15T10:50:21+09:00 Subject: Re: Ruby functions to split numbers --20cf307812c497767304af4c984b Content-Type: text/plain; charset=ISO-8859-1 On Fri, Oct 14, 2011 at 4:54 PM, ideal one wrote: > Ian M. Asaff wrote in post #1026669: > > ruby-1.9.2-p290 :010 > a = "01234567" > > => "01234567" > > ruby-1.9.2-p290 :011 > var1,var2 = [a[0,3],a[3,5]] > > => ["012", "34567"] > > ruby-1.9.2-p290 :012 > var1 > > => "012" > > ruby-1.9.2-p290 :013 > var2 > > => "34567" > > > > keep in mind that a number with a leading zero makes ruby think the base > > is > > octal: > > > > ruby-1.9.2-p290 :019 > 012 > > => 10 > > > > hope that helps. > > > > ian > > Hello there, > How can i auto increment the number, i tried doing > something like this... > var1 is always constant, but i want to autoincrement var2 > > value = var2 + 1 # iam getting Cannot convert Fixnum to String Error. > is something like this works var2++, not tried this in ruby. > Preserve leading Zero and also autoincrement.. > > Cheers > > -- > Posted via http://www.ruby-forum.com/. > > Strings have implemented on them the method "next" which, for numerical strings, seems to respect numerical iteration n = '0' 15.times do n = n.next # => "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15" end I'm not 100% sure that this is compatible across all versions of Ruby, you should definately have a test to make sure that it increments them as you expect. You should also consider what happens when you have something like "999", is it supposed to go to "1000", which would cause it to gain a digit? If not, you'll need to add special handling for that situation. --20cf307812c497767304af4c984b--