sketchucation logo sketchucation
    • Login
    โ„น๏ธ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Concatenate a string and an integer?

    Scheduled Pinned Locked Moved Developers' Forum
    4 Posts 3 Posters 564 Views 3 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.
    • T Offline
      tomot
      last edited by

      Does Ruby have this ability? It appear to me it does not, unless my search and coding is in error.

      For example:

      opposite=4; adjacent=3; angle=Math.atan(4/3).radians
      45.0
      string=".degrees"
      .degrees
      angle1=angle+string
      Error: #<TypeError: (eval):4542:in `+': String can't be coerced into Float>
      (eval):4542
      (eval):4542

      FYI: Autolisp: can concatenate a string and an integer: such that

      angle1=angle+string
      45.degrees

      [my plugins](http://thingsvirtual.blogspot.ca/)
      tomot

      1 Reply Last reply Reply Quote 0
      • A Offline
        Aerilius
        last edited by

        In ruby objects have a certain type and you can rely on that the type does not magically change on its own.
        For a number, the + method is defined to accept another number as argument and returns a number (that you don't want).
        For a string, the + concatenates strings and returns the resulting string, so you have to convert any object into a string first.

        There are many ways to create strings:
        string concatenation:
        90.to_s + " degrees" # slow because it creates a new third string object
        string appending:
        90.to_s << " degrees" # faster because it adds the second string into the first
        string interpolation:
        "#{90} degrees"

        1 Reply Last reply Reply Quote 0
        • T Offline
          tomot
          last edited by

          thanks for the explanation Aerilius: ๐ŸŽ‰

          opposite=4; adjacent=3; angle=Math.atan(4/3).radians
          45.0
          string=".degrees"
          .degrees
          angle1=angle.to_s << ".degrees"
          45.0.degrees

          Its also great news because it finally allows me to make the acceptable 45.0.degrees substitution into the Sine Law that TIG was so kind to put into Ruby form.
          http://sketchucation.com/forums/viewtopic.php?f=180&t=53118

          [my plugins](http://thingsvirtual.blogspot.ca/)
          tomot

          1 Reply Last reply Reply Quote 0
          • thomthomT Offline
            thomthom
            last edited by

            @aerilius said:

            There are many ways to create strings:
            string concatenation:
            90.to_s + " degrees" # slow because it creates a new third string object
            string appending:
            90.to_s << " degrees" # faster because it adds the second string into the first
            string interpolation:
            "#{90} degrees"

            Note that string interpolation is the fastest. (And reads easier IMO)

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

            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