• Login
sketchucation logo sketchucation
  • Login
ℹ️ GoFundMe | Our friend Gus Robatto needs some help in a challenging time Learn More

Concatenate a string and an integer?

Scheduled Pinned Locked Moved Developers' Forum
4 Posts 3 Posters 564 Views
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 13 Jul 2013, 17:08

    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 13 Jul 2013, 17:32

      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 13 Jul 2013, 19:28

        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
        • T Offline
          thomthom
          last edited by 13 Jul 2013, 21:38

          @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
          1 / 1
          • First post
            1/4
            Last post
          Buy SketchPlus
          Buy SUbD
          Buy WrapR
          Buy eBook
          Buy Modelur
          Buy Vertex Tools
          Buy SketchCuisine
          Buy FormFonts

          Advertisement