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

    [Code] Creating attr like methods

    Scheduled Pinned Locked Moved Developers' Forum
    8 Posts 3 Posters 1.3k 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.
    • thomthomT Offline
      thomthom
      last edited by

      I have a set of classes where I'd like to use attr like syntax to define some properties for each class. Each of these classes inherit from a base class. I was wondering if it was possible to define your own attr like syntax?

      <span class="syntaxdefault">class Hello </span><span class="syntaxkeyword"><</span><span class="syntaxdefault"> MyBaseClass<br />  foo</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">;</span><span class="syntaxdefault">lorem</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">;</span><span class="syntaxdefault">ipsum </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">end<br /></span>
      

      The simple solution:

      <span class="syntaxdefault">class MyBaseClass<br />  private </span><span class="syntaxcomment"># Not required, but I like to scope'em.<br /></span><span class="syntaxdefault">  def self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">foo</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">*</span><span class="syntaxdefault">args </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">    </span><span class="syntaxcomment"># Do magic stuff!<br /></span><span class="syntaxdefault">  end<br />end<br /><br />class Hello </span><span class="syntaxkeyword"><</span><span class="syntaxdefault"> MyBaseClass<br />  foo</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">;</span><span class="syntaxdefault">lorem</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">;</span><span class="syntaxdefault">ipsum </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">end<br /></span>
      

      It was pretty much what I had tried before posting the question, but I'd forgotten to add self. ... 😳

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

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

        Found the solution here: http://www.vitarara.org/cms/ruby_metaprogamming_declaratively_adding_methods_to_a_class

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

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

          @thomthom said:

          I was wondering if it was possible to define your own attr like syntax?

          I really think they missed an attr-like method for modules and classes that creates and inits class @@ vars.

          I'm not here much anymore.

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

            @dan rathbun said:

            @thomthom said:

            I was wondering if it was possible to define your own attr like syntax?

            I really think they missed an attr-like method for modules and classes that creates and inits class @@ vars.

            Aye!

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

            1 Reply Last reply Reply Quote 0
            • tbdT Offline
              tbd
              last edited by

              maybe add an example in this thread (maybe original post) to have it for further searches. thanks.

              SketchUp Ruby Consultant | Podium 1.x developer
              http://plugins.ro

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

                @unknownuser said:

                maybe add an example in this thread (maybe original post) to have it for further searches. thanks.

                Done!

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

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

                  Need to prepend the subject with "[Code]" so Jim's indexer will 'snag' it ...

                  I'm not here much anymore.

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

                    Doubledone!

                    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