sketchucation logo sketchucation
    • Login
    πŸ›£οΈ Road Profile Builder | Generate roads, curbs and pavements easily Download

    Auto add numbers in Ruby?

    Scheduled Pinned Locked Moved Developers' Forum
    12 Posts 5 Posters 563 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.
    • Chris FullmerC Offline
      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 Offline
        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 Offline
          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 Offline
            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 Offline
              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 Offline
                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 Offline
                  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 Offline
                    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 Offline
                      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 Offline
                        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
                        • 1 / 1
                        • First post
                          Last post
                        Buy SketchPlus
                        Buy SUbD
                        Buy WrapR
                        Buy eBook
                        Buy Modelur
                        Buy Vertex Tools
                        Buy SketchCuisine
                        Buy FormFonts

                        Advertisement