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

    Class similar to Length class

    Scheduled Pinned Locked Moved Developers' Forum
    8 Posts 3 Posters 400 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.
    • V Offline
      Viskiz
      last edited by

      Hello

      I'd like to create Area and Volume classes derived from the Float. These classes should be similar to Length class. How conversion from String to Length is made? Length.new() is not implemented, so it seems it doesn't have constructor? How it should be done?

      1 Reply Last reply Reply Quote 0
      • J Offline
        Jim
        last edited by

        You can get a Length from a String:

        "100cm".to_l

        But since you are implementing your own classes, you can write a "new" constructor.

        
        class Area
          def initialize(area)
            @internal_representation_of_an_area = area
          end
        end
        
        a = Area.new(12.0)
        
        

        And Dan will be along shortly to explain why you need to wrap your class in a module. πŸ˜‰

        Hi

        1 Reply Last reply Reply Quote 0
        • Dan RathbunD Offline
          Dan Rathbun
          last edited by

          @viskiz said:

          I'd like to create Area and Volume classes derived from the Float. These classes should be similar to Length class.


          BUT... Actually ... I'm not sure an Area or Volume class SHOULD ever be implemented.

          What we are talking about, are really "properties" of objects, which are best implemented in ruby attributes, with getter and setter methods. And conversion methods to convert to other units.

          You can still do if it's the best way (for you.)

          A class CAN have attributes, which are instances of some other class.

          So class Square, could have an @area attribute, that is an instance of class Area, which has @x and @y attributes, which are instances of class Length, and so on ...

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • Dan RathbunD Offline
            Dan Rathbun
            last edited by


            Along with this issue, is the question "Should a Length class have been implemented?"

            Yes I understand Sketchup's Length class and why they did it. It was easier (or quicker to implement,) than the "precise" alternative, which is a class for each 'unit of measure.'

            Classes Meter, Inch, Feet, Yard, ... etc. are more intuitive to me.

            When I see Sketchup's Length class, I keep wanting to find it's unit attribute, to see what it is currently set to. But it does not have one! Because really Sketchup's Length class is an alias for an Inch class.

            (I had played a bit defining classes in a General UOM or Units module for global Ruby use. Not finished with it, and it's just an exercise in Ruby programming. I have an interest in metrology as well. There may already be such a module, perhaps in Sketchy Physics?)

            I'm not here much anymore.

            1 Reply Last reply Reply Quote 0
            • Dan RathbunD Offline
              Dan Rathbun
              last edited by

              @jim said:

              And Dan will be along shortly to explain why you need to wrap your class in a module. πŸ˜‰

              🀣

              The rule is:

              IF the class is to be generic (used by,) the entire Ruby Universe, then yes it CAN be defined at the TopLevel, as a Ruby Base class. What is clear, is that you should be VERY certain that your new classname, is not already being used at Ruby's TopLevel, before you define it there.

              IF the class is only to be used with a specific application (Sketchup for example,) it should be in the application's namespace (as most of Sketchup's classes are.)

              If the class is only meant to be use by YOU, (but possibly in several or many of your plugins,) then it should be within the Developer's namespace.

              IF the class is only meant to be used with a specific plugin, then it should be defined within the plugin's namespace (which should defined within the Developer's namespace.)


              But still, even with toplevel base classes, it is often nice to group classes together into a 'catagory' module.

              I'd think this would best in a Geometry or Geometric module. (Not to be confused with the Sketchup specific Geom module.)

              If I were to create a generic Integral class, I would think the best place be within the Math module? (or pehaps within a Calculus submodule of ::Math.)

              I'm not here much anymore.

              1 Reply Last reply Reply Quote 0
              • V Offline
                Viskiz
                last edited by

                @jim said:

                You can get a Length from a String:

                "100cm".to_l

                I'm interested how Length class is made? In String class it should look like this:

                def to_l
                    Length.new(self)
                end
                

                but Length.new throws 'undefined method' error. So how to_l works?

                P.S. Thanks for reminding me about modules. I've completely forgotten about them 😳

                1 Reply Last reply Reply Quote 0
                • Dan RathbunD Offline
                  Dan Rathbun
                  last edited by

                  @viskiz said:

                  In String class it should look like this:

                  def to_l
                  >     Length.new(self)
                  > end
                  

                  but Length.new throws 'undefined method' error. So how to_l works?

                  Length is subclass of Float which also does not have a new method.
                  You create Floats in Ruby using literal expressions:
                  num = 1.2345

                  String.to_l is much more complex than you imagine.
                  It has a string parsing algorithm in it, that separates the numeric characters from the alpha characters, and does conversions from various units to inches.
                  More like:

                  def to_l
                    text=self.strip
                    numpart=text.to_f
                    if numlast = text.rindex(/\d/)
                      unitpart = text.slice(numlast+1..text.length-1)
                    else
                      unitpart=nil
                    end
                    if unitpart
                      case unitpart.downcase
                      when 'cm'
                        return (numpart * 0.3937).to_l
                      # other when statements
                      # for other units
                      end # case
                    else
                      return numpart.to_l
                    end #if
                  end
                  

                  Actually the extendend Numeric conversion methods are used, see:
                  API: Numeric baseclass

                  I'm not here much anymore.

                  1 Reply Last reply Reply Quote 0
                  • Dan RathbunD Offline
                    Dan Rathbun
                    last edited by

                    @viskiz said:

                    I'm interested how Length class is made?

                    It is simply a subclass, which inherits methods from it's superclass.

                    class Distance < Float
                      # add custom methods here
                    end
                    
                    

                    P.S.: DO NOT make changes to the Length class and then publish this in a plugin or script !!

                    I'm not here much anymore.

                    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