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

    Model in model sharing methods

    Scheduled Pinned Locked Moved Developers' Forum
    7 Posts 4 Posters 283 Views 4 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.
    • onidarbeO Offline
      onidarbe
      last edited by

      Hi,
      I've written my own method p(msg) which adds information above and a line-number before the printed values, as in:
      4 p "hello" 5 p "world"
      ┌─┬─ filename ─ module::method ─ time.msec ─
      │ 4│ hello
      │ 5│ world

      I placed this in a separate plugin-file but under the same module name. So all p ... are printed now with more information, though when not having this plugin it still has normal p ... -puts

      Now I was wondering if I could have the p-method also in other modules of mine without making it global by not putting it in a module at all?

      #mr_main.rb;
      module MR
        def self.p(msg=nil)
          # Organises puts with line-numbers in a frame; ┌─┬─ filename ─ module;;method ─ time ─
          puts "┌───┬─ " # ... full version attached to this post ...
          puts msg
        end
      end
      
      #mr_test.rb;
      module MR
        module MR_test
          def self.test
            MR;;p "working"
            p "not working as I like it to call MR;;p"
          end
          test
        end
      end
      
      1 Reply Last reply Reply Quote 0
      • tt_suT Offline
        tt_su
        last edited by

        You could make it a mix-in module and include it into the modules you need.

        1 Reply Last reply Reply Quote 0
        • onidarbeO Offline
          onidarbe
          last edited by

          mix-in module? How do I do that?

          1 Reply Last reply Reply Quote 0
          • tt_suT Offline
            tt_su
            last edited by

            http://rubylearning.com/satishtalim/modules_mixins.html

            1 Reply Last reply Reply Quote 0
            • onidarbeO Offline
              onidarbe
              last edited by

              Thank you, but even reading that twice and playing with it, I still don't see how to do it. I don't know if it's even possible what I'm trying.

              module MR
                def self.p(msg=nil)
                  puts msg
                end
              end
              module MR_test
                include MR
                MR;;p "hello" #working
                p "hello" #still not working ;(
              end
              
              1 Reply Last reply Reply Quote 0
              • S Offline
                slbaumgartner
                last edited by

                Seems to me this depends on what you are really trying to achieve. Are these printouts for your own use during development and testing (most likely) or for your end users (seems unlikely as they probably won't have Ruby Console open and won't have any use for file and line info)? If the former, what is wrong with coding MR::p? You can then turn off the effect globally by editing just the MR module so that p does nothing. Also, you really don't want to pollute the global namespace with a variant method. That is reserved for the Trimble team 😲

                Steve

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

                  try something like:

                  file "MR/MIX/P.rb"

                  module MR
                    module P
                      def p(msg="")
                        puts "from MR;;P; '#{msg}'"
                      end
                    end
                  
                    # create a module function within module MR;
                    extend(P) # creates a singleton method
                  
                  end
                  
                  

                  other files:

                  
                  require("MR/MIX/P.rb")
                  module MR
                    module Test
                  
                      p "Called p inherited down from Kernel"
                  
                      include(MR;;P) # overrides the p() inherited from Object, as mixed in from Kernel
                  
                      MR;;p "Called MR;;p"
                  
                      p "Called p" # should call overridden p()
                  
                      def self.pedigree()
                        puts "#{Module.nesting[0].name} ancestors; #{self.ancestors.inspect}"
                      end
                  
                    end
                  end
                  

                  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