sketchucation logo sketchucation
    • Login
    1. Home
    2. Cleverbeans
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info
    C
    Offline
    • Profile
    • Following 0
    • Followers 0
    • Topics 0
    • Posts 41
    • Groups 1

    Cleverbeans

    @Cleverbeans

    10
    Reputation
    1
    Profile views
    41
    Posts
    0
    Followers
    0
    Following
    Joined
    Last Online

    Cleverbeans Unfollow Follow
    registered-users

    Latest posts made by Cleverbeans

    • RE: Inherit from Sketchup Classes

      Based on your description it seems unclear as to why you'd want to use inheritance here when the goal is to simply store the weights associated with the vector. I can envision either a class with a .vector and .weight attribute and some supporting methods. Here are some examples of what I think would be cleaner implementations of your idea.

      
      class MyClass
         attr_accessor ;weight
         def initialize(x,y,z,weight)
            @vector = Geom;;Vector3d.new(x,y,z)
            @weight = weight
         end
         def vector
            return @vector.transform(@weight)
         end
      end
      
      

      This would cleanly differentiate between the underlying Vector3d and the weight you want to store and allows you to update the weight while encapsulating the scaling behavior in the .vector method. The only downside of this approach is that occasionally you'll have to type my_class.vector.method instead of my_class.method but it avoids the semantic muddling of the underlying vector defined by the x,y,z values provided to initialize and the weighted version of it.

      posted in Developers' Forum
      C
      Cleverbeans
    • RE: Module class method defining confusion

      Considering the following code, we have MyClass defined in MyModule, which can be accessed with MyModule::MyClass. You do not require the prefix, and note that class methods are differentiated by the 'self.' prefix.

      
      module MyModule
        class MyClass
          def self.class_method
            return "I am a class method"
          end
          def instance_method
            return "I am an instance method"
          end
        end
      end
      
      

      Accessing another classes variables is simply a matter of creating getter methods for them. Since you need access to a class variable, you'll want a class method like the following. Note that I've changed it to @@value instead of @@data for the reasons Dan gave.

      
      class MyClass
        def self.value
          return @@value
        end
      end
      
      

      With that being said, It begs the question as to why you require access to a variable across multiple classes because global variables are really bad mojo. One of the great things about namespaces is localization, as it allows you to isolate bugs to a specific region of your code or to reuse your code without having to untangle it from other portions of your application. It's interesting to note that you can actually write every conceivable program without modifying any variables at all. This style of programming is generally called functional programming and it avoids the side effects that can crop up when you're modifying a variable.

      If you absolutely much have a global, then there are a few ways you can implement this. You can simply have a getter like the example above, or you can have a superclass which handles interaction with that global then have the classes which require access to that variable inherit from it. Note that if you want a class level variable which is isolated to a class and not accessible by it's subclasses/superclasses then you'd want a class instance variable. But once again I'd encourage you to avoid doing this at all costs, as you can likely avoid the global entirely and avoid a lot of headaches in the process.

      posted in Developers' Forum
      C
      Cleverbeans
    • RE: Ruby "split" in file line reading

      @shirazbj said:

      Heard of regular expressions before, This time I tried to understand it. Thanks.

      Regular expression are simultaneously very useful and painful to work with. There is a great quote by Jamie Zawinski which goes "Some people, when confronted with a problem, think “I know, I’ll use regular expressions.” Now they have two problems." I first ran into them at Dive into Python 3 which gives a couple simple case studies and although the language is slightly different the regular expressions are basically the same.

      Looking at the regular expression given by /[-|+]?\d.\dE[-|+]?\d\d\d/ we have the / on each end marking the start and end of the expression. There are two blocks of [-|+]? which matches an optional + or - sign. I would read this is "0 or 1 instances of + or -". Then we have \d.\dE which matches a single digit, then some arbitrary number of digits until it find an E, then we again have an option sign followed by exactly 3 digits. So this pattern will match a single numerical value in the string given. The .scan method then constructs an array out of every match. From here you can wrap blocks in parenthesis to create groups so you can break the matches up into their own arrays which is what I had done originally until I realized Ruby would parse scientific notation with the .to_f method.

      posted in Developers' Forum
      C
      Cleverbeans
    • RE: Drawing an array of boxes

      I would suggest breaking the code out into logical steps, and give each step a method. Normally I'd start with a blank file and write out what I want to happen in natural language as comments. Once I get it into a step-by-step form that makes sense I will declare method names around each step. Once I've done that I figure out what the inputs and outputs of each method should be and document them in a comment. Once that's done I pick the easiest one to test, write a simple test for it, then implement the method and see if it passes the test. Repeat until finished. It's hard to say why it's failing here because of all the variable kicking around, so maybe just try and split the methods into more bit sized portions then put it together at the end.

      posted in Developers' Forum
      C
      Cleverbeans
    • RE: Drawing an array of boxes

      Each kind of variable is referenced differently to indicate it's scope. @@var is a class variable, @var is an instance variable, and var without a modifier is a method variable. method variables can only be referenced within that method, instance variables have unique values for each instance, and class variables are shared by all instances of a class including super/sub classes. Class instance variables exist as well, and use the @var syntax, but are declared outside of a method which can trip up the unaware coder. They act like class variables but without super/sub classes having access to them. This gives you granular control over the scope for the reasons mentioned above - variables with limited scope are easier to debug. Using that as a rule of thumb then a best -> worst listing would be method -> instance -> class instance -> class -> global. In your case I believe simply changing the $ to @ would give you the behavior you're looking for assuming you want distinct values every time you call Building.new

      posted in Developers' Forum
      C
      Cleverbeans
    • RE: Drawing an array of boxes

      There are only four points, so the only condition under which there would be repeated points are $start_of_slab == $slabwidth. If $slabwidth is 0 initially then when it's incremented it will take on teh value of $start_of_slab. Are you sure you didn't mean to type it $start_of_slab += $slabwidth? If you're using $start_of_slab as the initial position I expect that's the trouble.

      As for the variables there are more than one level of scope a variable can have. If you're not calling the Building.new method anywhere you're probably wanting class methods, which should read "def self.method_name" rather than just "def method_name", which will have access to class variables rather then using globals. Also, you cannot declare variables anywhere, if you're going to declare an instance variable it must be done within an instance method. There is some subtlety here between a class instance variable (not to be confused with a class variable) and a object instance variable which requires some care, since it's easy to declare it in the wrong location and get unintended behavior.

      Even better though, would be to get rid of the variables all together and use parameters and return values for your methods instead. This really helps when trying to track down where a bug is introduced since if the problem is with a global variable, every method that interacts with that variable has to be checked and it requires tracing all the method calls. If you've got parameters and return values you can verify each block independently which can save a lot of headaches. You can always put in useful default values, and array unpacking in Ruby is a really nice feature when you've got multiple return values like depth, width,height triplets. I don't see any explicit need to store the values as variables with the provided code, so maybe consider that approach if you can swing it.

      posted in Developers' Forum
      C
      Cleverbeans
    • RE: Ruby "split" in file line reading

      After looking at this again, it seems ruby understands scientific notation natively, so there is no need to split the block into num and exp, it can just be grouped as a single value. Here is the modification which gives a noticeable increase in speed as well. I would expect in general the regular expression will be slower than index parsing too.

      def parse_line(line)
         #accepts a string of numbers in scientific notation
         #returns an array of [float,int] pairs
         pat = /[-|\+]?\d\.\d*E[-|\+]?\d\d\d/
         return line.scan(pat).map{|num| num.to_f}
      end
      
      posted in Developers' Forum
      C
      Cleverbeans
    • RE: Ruby "split" in file line reading

      @dan rathbun said:

      CB.. how about doing the conversion within the method, and returning an [x,y,z] array ?

      Certainly can be done, I just didn't presume that the three numbers were coordinate values, or that a line would only have three values, but under these assumptions. I would probably do it a little differently however by including the construction point within the map block which tightens up the code a bit more than even my original.

      
      def parse_line(line)
         #accepts a string of numbers in scientific notation
         #returns an array of [float,int] pairs
         pat = /([-|\+]?\d\.\d*)E([-|\+]?\d\d\d)/
         return line.scan(pat).map{|num,exp| num.to_f**exp.to_i}
      end 
      

      You could also convert to numerical values and then stride the array if the values are not neatly separated by line. I figured there are times when scientific notation may be the preferred output for whatever reason, so I chose that route for the example code. In any event, the regular expression is at the heart of the function and the rest is easily tailored to various needs.

      posted in Developers' Forum
      C
      Cleverbeans
    • RE: Ruby "split" in file line reading

      For something like this I like to use regular expressions. It has the advantage of allowing you to explicitly capture the pattern you're looking for, regardless of slight variations in the files. Here is how I would parse the line into [float,int] pairs, and note that even if the values are separated by white space this will still work. The pattern given matches an optional + or -, then a digit, a decimal place, then more digits till it finds then E, then again an optional + or -, and exactly 3 digits. the string.scan method finds every match in the string and puts it into an array, and the pattern has groups delimited by the parenthesis which split the matching string natural into pair as [num,exp], all that's left is to convert to numerical quantities. Here is the code.

      
      def parse_line(line)
         #accepts a string of numbers in scientific notation
         #returns an array of [float,int] pairs
         pat = /([-|\+]?\d\.\d*)E([-|\+]?\d\d\d)/
         return line.scan(pat).map{|num,exp| [num.to_f, exp.to_i]}
      end
      
      
      posted in Developers' Forum
      C
      Cleverbeans
    • RE: Integer vs string test time

      It seems to be slower to do the conversion then do the comparison. To test something like this you can use the Time.now() method to make a simple stopwatch. Here is how I tested it.

      
      def str_test()
         t = Time.now()
         (1..10000000).each{|i| "0" == "0"}
         puts(Time.now() - t)
      end
      
      def int_test()
         t = Time.now()
         (1..10000000).each{|i| "0".to_i == 0}
         puts(Time.now() - t)
      end
      
      

      str_test ran in 6.922 and int_test ran in 8.406, feel free to run the test yourself.

      posted in Developers' Forum
      C
      Cleverbeans