I have members with usernames. In the event that a new member requests an already-existing username, I''d like to automatically "increment" a next-best string: johnny johnny1 johnny2 Knowing RoR, my gut tells me there''s some elegant, concise way to do this, but I can''t think of it. Any advice? -- Posted via http://www.ruby-forum.com/.
On 7/11/06, Max Rockatansky <shinji@kuwayama.com> wrote:> I have members with usernames. In the event that a new member requests > an already-existing username, I''d like to automatically "increment" a > next-best string: > > johnny > johnny1 > johnny2 > > Knowing RoR, my gut tells me there''s some elegant, concise way to do > this, but I can''t think of it. Any advice?String#succ From: http://www.ruby-doc.org/core/ http://www.ruby-doc.org/core/classes/String.html#M001851 - Rob - http://www.robsanheim.com http://www.seekingalpha.com http://www.ajaxian.com
On Wednesday, July 12, 2006, at 4:46 AM, Max Rockatansky wrote:>I have members with usernames. In the event that a new member requests >an already-existing username, I''d like to automatically "increment" a >next-best string: > >johnny >johnny1 >johnny2 > >Knowing RoR, my gut tells me there''s some elegant, concise way to do >this, but I can''t think of it. Any advice? > >-- >Posted via http://www.ruby-forum.com/. >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/railsActually, It''s a ruby thing. "johnny1".succ => "johnny2" but watch out, it has some quirks. "johnny".succ => "johnnz" "johnny9".succ => "johnnz0" _Kevin -- Posted with http://DevLists.com. Sign up and save your mailbox.
> but watch out, it has some quirks. > > "johnny".succ => "johnnz" > "johnny9".succ => "johnnz0"What about String#sub(/(\d*)\Z/)? "jonny".sub(/(\d*)\Z/){ $1.to_i.succ } # => "jonny1" "jonny1".sub(/(\d*)\Z/){ $1.to_i.succ } # => "jonny2" "jonny9".sub(/(\d*)\Z/){ $1.to_i.succ } # => "jonny10" -- Yugui yugui.ps@gmail.com http://idm.s9.xrea.com