• Login
sketchucation logo sketchucation
  • Login
ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

Auto add numbers in Ruby?

Scheduled Pinned Locked Moved Developers' Forum
12 Posts 5 Posters 503 Views 5 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    Chris Fullmer
    last edited by 28 Jun 2010, 22:57

    I've forgotten how to do this the quick way (seem like there is a method already set up nicely for what I want).

    I want to take a string and add numbers to the beginning of it.

    Chris
    Dan
    Jim
    Martin
    Thom

    becomes

    0001Chris
    0002Dan
    0003Jim
    0004Martin
    0005Thom

    Seems like there is a method I can use that does this automatically and smoothly for me, where I should be able to specify the amount of placeholders to use (in the example I show 4 10's places being held). Anyone recall what I'm thinking of? Just hte method is enough, I can look up its syntax again if you could kindly point me in the right direction. Thanks,

    Chris

    Lately you've been tan, suspicious for the winter.
    All my Plugins I've written

    1 Reply Last reply Reply Quote 0
    • J Offline
      Jim
      last edited by 28 Jun 2010, 23:09

      The easiest is to use strings and .next.

      "0000".next => "0001"

      Otherwise, if you use numbers you need to use printf or sprintf (aka format).

      10.times { |i|
        printf "%04d\n", i
      }
      
      
      names = %w( Joe Mike Tim )
      names.each_with_index do |name, i|
        printf "%04d%s\n" % [i, name]
      end
      
      
      

      See sprintf for details on formatting.

      Hi

      1 Reply Last reply Reply Quote 0
      • C Offline
        Chris Fullmer
        last edited by 28 Jun 2010, 23:22

        Awesome, thanks Jim. It was string.next that I was trying to think of. That is all I needed, thanks again!

        Chris

        a = "000" 10.times do puts a a = a.next end

        Lately you've been tan, suspicious for the winter.
        All my Plugins I've written

        1 Reply Last reply Reply Quote 0
        • T Offline
          thomthom
          last edited by 29 Jun 2010, 07:44

          Another way is: '1'.rjust(4, '0')

          Thomas Thomassen — SketchUp Monkey & Coding addict
          List of my plugins and link to the CookieWare fund

          1 Reply Last reply Reply Quote 0
          • T Offline
            thomthom
            last edited by 29 Jun 2010, 07:49

            You don't have to do this: a = a.next - you can instead do a.next!. Most probably that will be faster - thought not noticeable until you run many iterations.

            If you did not require '0000' then you could also do:

            a = '0000' 10.times{ puts a.next! }

            Thomas Thomassen — SketchUp Monkey & Coding addict
            List of my plugins and link to the CookieWare fund

            1 Reply Last reply Reply Quote 0
            • C Offline
              Chris Fullmer
              last edited by 29 Jun 2010, 07:58

              Ahh, good idea on the next! method. It didn't cross my mind that it might exist. I also like that rjust method. I'll look into that one too. Thanks Thom,

              Chris

              Lately you've been tan, suspicious for the winter.
              All my Plugins I've written

              1 Reply Last reply Reply Quote 0
              • T Offline
                todd burch
                last edited by 29 Jun 2010, 21:56

                @unknownuser said:

                ...4 10's places....

                HAR HAR HAR HAR HAR!!!!!!!

                1 Reply Last reply Reply Quote 0
                • C Offline
                  Chris Fullmer
                  last edited by 29 Jun 2010, 22:50

                  Laughing at my inability to express myself in functional english there Todd 😄, or is there a big inside joke I missed (both equally likely).

                  Chris

                  Lately you've been tan, suspicious for the winter.
                  All my Plugins I've written

                  1 Reply Last reply Reply Quote 0
                  • Dan RathbunD Offline
                    Dan Rathbun
                    last edited by 29 Jun 2010, 23:41

                    @jim said:

                    
                    > names = %w( Joe Mike Tim )
                    > names.each_with_index do |name, i|
                    >   printf "%04d%s\n" % [i, name]
                    > end
                    > 
                    

                    index starts at 0, you'd need i+1 to start at 1

                    also.. printf "%04d%s\n" % [i, name]
                    is adding an extra operation eval that's not needed.
                    could be either:
                    puts "%04d%s\n" % [i+1, name]
                    or
                    printf("%04d%s\n",i+1,name)

                    I'm not here much anymore.

                    1 Reply Last reply Reply Quote 0
                    • Dan RathbunD Offline
                      Dan Rathbun
                      last edited by 29 Jun 2010, 23:55

                      Of course if your wanting to build up an Array of Hash keys (or Attribute Dictionary keys,) just create an empty array first
                      keys=[]
                      then replace the puts or printf with
                      keys.push

                      I'm not here much anymore.

                      1 Reply Last reply Reply Quote 0
                      • T Offline
                        todd burch
                        last edited by 30 Jun 2010, 14:31

                        @chris fullmer said:

                        Laughing at my inability to express myself in functional english there Todd 😄, or is there a big inside joke I missed (both equally likely).

                        Chris

                        The first.

                        1 Reply Last reply Reply Quote 0
                        • J Offline
                          Jim
                          last edited by 30 Jun 2010, 17:46

                          @dan rathbun said:

                          also.. printf "%04d%s\n" % [i, name]
                          is adding an extra operation eval that's not needed.

                          Note to self: % is a method of Strings. Thanks.

                          Hi

                          1 Reply Last reply Reply Quote 0
                          • 1 / 1
                          1 / 1
                          • First post
                            1/12
                            Last post
                          Buy SketchPlus
                          Buy SUbD
                          Buy WrapR
                          Buy eBook
                          Buy Modelur
                          Buy Vertex Tools
                          Buy SketchCuisine
                          Buy FormFonts

                          Advertisement