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

    Attribute dictionary pairs into variables & values?

    Scheduled Pinned Locked Moved Developers' Forum
    8 Posts 4 Posters 455 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.
    • Chris FullmerC Offline
      Chris Fullmer
      last edited by

      Hey, I am using some attribute dictionaries. I am using a case statement to turn all the key/value pairs into variables and values. The key / value pairs look something like this:

      name / "chris age / "young" size / "too big"
      etc. Then later when I go to read those, I want to make them into variables where the key is the variable name and the value is what the variable points to. I am using a system like this:

      my_dict.each_pair do |aa, bb|
      case aa
      when "name" 
        sname = bb
      when "age"
        age = bb
      when "size"
        size = bb
      end
      end
      

      But this feels very slow and unnecessary. And more importanly, I want it to work in such a way that when I add more pairs to my dictionary, it automatically makes those variables without me having to come back and more whens to my case statement.

      So is there a smarter way to be doing this? Thanks!

      Chris

      Lately you've been tan, suspicious for the winter.
      All my Plugins I've written

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

        eval("#{variable_name} = #{value}")
        http://www.ruby-forum.com/topic/21359#5425

        But why not use the hash?

        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

          http://stackoverflow.com/questions/2530112/using-the-value-of-a-variable-as-another-variables-name-in-ruby

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

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

            You don't say what you're really trying to accomplish. An Attribute Dictionary can be used like a Ruby Hash. You have already created the my_dict variable. Why not use it without creating intermediate variables?

            my_dict['name']

            is as clear to me as

            name = my_dict['name']

            Hi

            1 Reply Last reply Reply Quote 0
            • Chris FullmerC Offline
              Chris Fullmer
              last edited by

              Yes, I think this is sort of the point. I am largely unfamiliar with hashes and dictionaries. I've used both before, just never really enough to feel like I know what I'm doing. I just don't know the standard way of using them and what can be done. But I think hat Jim showed there is essentially the answer to my question. Instead of assigning the contents to a variable, just use the dictionary['name'] syntax to call the contents of the name key.

              I also liked what that 2nd link you posted showed Thom. I want to look at that closer today. It looks like something I've tried to do and I had considered using in this case.

              Chris

              Lately you've been tan, suspicious for the winter.
              All my Plugins I've written

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

                When I say "hash" I am usually talking about a Ruby Hash, which is a data structure. Other languages may also have hashes, but I wouldn't know if they behave the same as a Ruby hash. A Ruby Hash is an associative array.

                In Ruby, a Hash is made for storing (key, value) pairs where the key is unique. Keys are typically String or Symbol object in Ruby, but can be any object.

                Particulary in SketchUp, it can be useful to use Entity objects as hash keys in order to create a fast way to look up some pre-calculated values for a particular entity.

                A common use is when you want the global position of all ComponentInstances in the model. You would traverse all the entities and build a hash keyed on the instance, and store its position as the value. You do this once at the start of the script, and then you can just look up the values when you need it.

                More uses include AttributeDictionaries, as representations of .ini files, Windows Registry, and script options ( @opts[:explode_after_extrude] = true ).

                Hi

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

                  Note that strings are very inefficient as hash keys. Symbols are preferred.

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

                  1 Reply Last reply Reply Quote 0
                  • M Offline
                    MartinRinehart
                    last edited by

                    @chris fullmer said:

                    I've used both [hashes and dictionaries] before, just never really enough to feel like I know what I'm doing.

                    Crash course in hashes (a very useful tool to have at hand):

                    A hash is a dictionary and vice-versa. They are sets of key/value pairs. An array is one form of hash: arr[0] = 'fred'. The key is zero and the value is 'fred'. Arrays are commonly restricted to integer keys. The term "dictionary" is commonly used when the keys are single words. dict['name']='fred'.

                    Ruby makes hashes dead simple (though only a devout Rubyist could love the syntax):

                    Ruby-example.jpg

                    In JavaScript, both objects and arrays are hashes:

                    javascript-examples.jpg

                    The last example shows that the key need not be a string. (True in Ruby if you start with a hash: h=Hash.new() or h={}.) The key can be virtually anything. I'm using SketchUp ComponentInstance objects as keys in the stuff I'm doing today (trying to get a lot of things moving all at once).

                    In Ruby, an array must be indexed by number. A hash may be indexed by anything. In JavaScript, an array is really a hash, which wins the flexibility prize but you wouldn't enter them in a race.

                    Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

                    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