sketchucation logo sketchucation
    • Login
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    ⚠️ Update | Sketchucation Tools 5.0.8 released with licensing improvements and bugfixes Download

    Auto add numbers in Ruby?

    Scheduled Pinned Locked Moved Developers' Forum
    12 Posts 5 Posters 986 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.
    • J
      Jim
      last edited by

      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
      • Chris FullmerC
        Chris Fullmer
        last edited by

        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
        • thomthomT
          thomthom
          last edited by

          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
          • thomthomT
            thomthom
            last edited by

            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
            • Chris FullmerC
              Chris Fullmer
              last edited by

              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
                todd burch
                last edited by

                @unknownuser said:

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

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

                1 Reply Last reply
                Reply Quote 0
                • Chris FullmerC
                  Chris Fullmer
                  last edited by

                  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
                    Dan Rathbun
                    last edited by

                    @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
                      Dan Rathbun
                      last edited by

                      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
                        todd burch
                        last edited by

                        @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
                          Jim
                          last edited by

                          @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

                          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                          With your input, this post could be even better πŸ’—

                          Register Login
                          • 1 / 1
                          • First post
                            Last post
                          Buy SketchPlus
                          Buy SUbD
                          Buy WrapR
                          Buy eBook
                          Buy Modelur
                          Buy Vertex Tools
                          Buy SketchCuisine
                          Buy FormFonts

                          Advertisement