Forum: Ruby Is there a method for incrementing string names?!?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7595ff5e64d9a309c29932d106959973?d=identicon&s=25 Human Dunnil (Guest)
on 2006-05-12 16:44
(Received via mailing list)
Hello,
I have model which has a property named "name", I want to append a
number to the end of the name when there is similar name in db.

name, name1, name2...

Is there a ready to use method in ruby and/or rails for doing so?

Thanks in advance,
- Dunnil
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (Guest)
on 2006-05-12 16:48
(Received via mailing list)
Human Dunnil wrote:
> Hello,
> I have model which has a property named "name", I want to append a
> number to the end of the name when there is similar name in db.
>
> name, name1, name2...
>
> Is there a ready to use method in ruby and/or rails for doing so?
Nearly...
irb(main):001:0> s = "aaa"
=> "aaa"
irb(main):002:0> s = s.succ
=> "aab"
irb(main):003:0> s = s.succ
=> "aac"

Or...

irb(main):001:0> s = "aaa1"
=> "aaa1"
irb(main):002:0> s = s.succ
=> "aaa2"
irb(main):003:0> s = s.succ
=> "aaa3"
23172b6630dc631a134c9bad2fec2a39?d=identicon&s=25 Chris (Guest)
on 2006-05-12 16:51
Alex Young wrote:

> irb(main):003:0> s = s.succ
> => "aaa3"

Note the "aaa9".succ => "aab1"

So if you only want the number incremented be sure to append
enough 0s
3a3fe02be09e3103aea5e22d62478f38?d=identicon&s=25 Pedro Côrte-Real (Guest)
on 2006-05-12 16:54
(Received via mailing list)
On 5/12/06, Alex Young <alex@blackkettle.org> wrote:
> irb(main):001:0> s = "aaa1"
> => "aaa1"
> irb(main):002:0> s = s.succ
> => "aaa2"
> irb(main):003:0> s = s.succ
> => "aaa3"

But don't forget:

irb(main):001:0> 'aa9'.succ
=> "ab0"

So it won't do what's needed.

Cheers,

Pedro.
89d967359903c639d31e4cad4569f537?d=identicon&s=25 Charlie Bowman (Guest)
on 2006-05-12 17:04
(Received via mailing list)
Why not just create an array called name and access with the numbers?

irb(main):003:0> name << 'charlie' << 'moir' << 'bowman'
=> ["charlie", "moir", "bowman"]
irb(main):006:0> puts name[0]
charlie
=> nil
irb(main):007:0> puts name[1]
moir
=> nil
irb(main):008:0> puts name[2]
bowman
=> nil


Charlie Bowman
http://www.recentrambles.com
51a34236538906ab994cf9f2e533d14d?d=identicon&s=25 Lou Scoras (ljscoras)
on 2006-05-12 17:10
(Received via mailing list)
>
*agrees*

How about something like this?

    def next_name(s)
      s.reverse =~ /(\d*)(.*)/
      digits_r, word_r = $~[1,2]
      digits = digits_r ? digits_r.reverse.to_i : 0
      digits += 1
      word_r.reverse + digits.to_s
    end

    next_name "var"      # => "var1"
    next_name "var32" # => "var33"
    next_name "var99" # => "var100"

String.succ will blow up for cases 1 and 3.
E2a9d30a2487a924165aaaa016b54f87?d=identicon&s=25 C Erler (Guest)
on 2006-05-12 17:14
(Received via mailing list)
Here's a method to do it :

def inc name
  parts = name.scan(/^(.*\D)(\d+)$/).first
  if parts.nil?
    "#{name}1"
  else
    "#{parts.first}#{parts.last.to_i + 1}"
  end
end
51a34236538906ab994cf9f2e533d14d?d=identicon&s=25 Lou Scoras (ljscoras)
on 2006-05-12 17:23
(Received via mailing list)
On 5/12/06, C Erler <erlercw@gmail.com> wrote:
>
Quite right.  My regex should have been anchored:

/\A(\d*)(.*)/
E2a9d30a2487a924165aaaa016b54f87?d=identicon&s=25 C Erler (Guest)
on 2006-05-12 17:36
(Received via mailing list)
Once again, I come up with something better two minutes after I hit
send.

class String
  def inc
    parts = scan(/^(.*\D)?(\d+)?$/).first
    "#{parts.first}#{parts.last.to_i + 1}"
  end
end
7595ff5e64d9a309c29932d106959973?d=identicon&s=25 Human Dunnil (Guest)
on 2006-05-12 17:39
(Received via mailing list)
My friends ask me why you love Ruby! that's becasue of you guys. ; )
Thanks
This topic is locked and can not be replied to.