# Global Variables

**URL:** <https://forums.sketchup.com/t/global-variables/147071>\
**Category:** Ruby API\
**Created:** [December 25, 2020, 4:47am UTC](https://forums.sketchup.com/t/global-variables/147071 "2020-12-25T04:47:39Z")\
**Posts on this page:** 11\
**Page:** 3

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [December 28, 2020, 6:12pm UTC](https://forums.sketchup.com/t/global-variables/147071/42 "2020-12-28T18:12:46Z")

</div>

> [@majid866](#):
>
> ```ruby
> loop do
> # ... code ...
> end #loop
> 
> ```

“loop” is **not** a Ruby keyword (like it is in Pascal.)

The construct you show is simply a Ruby **`do`** … **`end`** block that evaluates only **once**.

---

<div class="post-metadata">

**Author:** ![majid866](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/majid866/32/210257_2.png) [@majid866](https://forums.sketchup.com/u/majid866)\
**Post date:** [December 29, 2020, 4:18am UTC](https://forums.sketchup.com/t/global-variables/147071/43 "2020-12-29T04:18:46Z")

</div>

Do you know any dialog that can keep my data and return it when I call dialog?

---

<div class="post-metadata">

**Author:** ![majid866](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/majid866/32/210257_2.png) [@majid866](https://forums.sketchup.com/u/majid866)\
**Post date:** [December 29, 2020, 4:57am UTC](https://forums.sketchup.com/t/global-variables/147071/44 "2020-12-29T04:57:01Z")

</div>

Dear Dan, As you know your words are so important for me and I started this topic because of your advice but if you let me this time I disagree you because of following reasons.  
1- As Rumi says, the sun came because of the sun. In the following video I enter the wrong information and as you can see loop works well.  
2- If you simply enter loop do and end (Endless loop) and run it, your SU will freezes.  
3- I learn it from a ruby course.  
4- Ruby’s `loop` is basically the same thing as the `while( true )` - in fact, you can use `while( true )` almost interchangeably with it.

[https://global.discourse-cdn.com/sketchup/original/3X/3/0/3027ee50bffe6db787d0a18327e442cdc41d2fc5.mp4](https://global.discourse-cdn.com/sketchup/original/3X/3/0/3027ee50bffe6db787d0a18327e442cdc41d2fc5.mp4)

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [December 29, 2020, 6:19am UTC](https://forums.sketchup.com/t/global-variables/147071/45 "2020-12-29T06:19:22Z")

</div>

> [@majid866](#):
>
> 4- Ruby’s `loop` is basically the same thing as the `while( true )` - in fact, you can use `while( true )` almost interchangeably with it.

Actually it is **global method** that I never noticed before.  
[Module: Kernel (Ruby 2.7.1)](https://ruby-doc.org/core-2.7.1/Kernel.html#method-i-loop)

I would say that using it is poor programming practice. Use `while` or `until` instead.

---

<div class="post-metadata">

**Author:** ![eneroth3](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/eneroth3/32/1222_2.png) [@eneroth3](https://forums.sketchup.com/u/eneroth3)\
**Post date:** [December 29, 2020, 11:25am UTC](https://forums.sketchup.com/t/global-variables/147071/46 "2020-12-29T11:25:12Z")

</div>

As I said in my first respone, you can use instance variables at your extension namespace module.

```ruby
module Me
  module MyPlugin
    @some_variable = 1234

      def self.some_variable
        @some_variable
      end

      def self.some_variable=(some_variable)
        @some_variable = some_variable
      end
  end
end

```

In the rest of the code you can use `MyPlugin.some_variable` and `MyPlugin.some_variable=` to get and set the value.

This persists throughout the SketchUp session but not between sessions (just like the number of segments for native Circle tool).

---

<div class="post-metadata">

**Author:** ![majid866](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/majid866/32/210257_2.png) [@majid866](https://forums.sketchup.com/u/majid866)\
**Post date:** [December 29, 2020, 12:19pm UTC](https://forums.sketchup.com/t/global-variables/147071/47 "2020-12-29T12:19:29Z")

</div>

Please forgive me if sometimes I understand something late. You are completely right. Following codes work exactly the same as Singleton and till we restart SU keep data. Thank you so much. I know also dialogs have the capability to keep data. Do you know any dialog that can keep data for us? dialog.show\_modal cause SU freeze completely but if we run dialog.add\_action\_callback before it, dialog\_callback is active even when SU is freeze. I believe dialogs can be very useful for us this is why I ask you again for it.

```ruby
module MajidMahmoudi
  module MyPlugin
    @some_variable = 1234

      def self.some_variable
        @some_variable
      end

      def self.some_variable=(some_variable)
        @some_variable = some_variable
      end
  end
  module MajTest
    class MajTest
      def activate
        test = MyPlugin.some_variable
        p test
        MyPlugin.some_variable = "4321"
      end

    end #class MAJTest
    cmd1 = UI::Command.new("MAJ Test") {
      Sketchup.active_model.select_tool MajTest.new
    }
    cmd1.set_validation_proc { true ? MF_ENABLED : MF_GRAYED }
    cmd1.small_icon = "./images/small_icon_wall.png"
    cmd1.large_icon = "./images/large_icon_wall.png"
    cmd1.tooltip = "Wall Maker"
    maj_toolbar = UI::Toolbar.new "MAJ Test"
    maj_toolbar.add_item cmd1
    maj_toolbar.show
  end #MajWall
end #MajidMahmoudi#

```

---

<div class="post-metadata">

**Author:** ![eneroth3](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/eneroth3/32/1222_2.png) [@eneroth3](https://forums.sketchup.com/u/eneroth3)\
**Post date:** [December 29, 2020, 2:02pm UTC](https://forums.sketchup.com/t/global-variables/147071/48 "2020-12-29T14:02:25Z")

</div>

I don’t know what you mean by tracking data by a dialog. Dialogs are user interfaces, not places for data storage.

---

<div class="post-metadata">

**Author:** ![dpc](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/dpc/32/59801_2.png) [@dpc](https://forums.sketchup.com/u/dpc)\
**Post date:** [January 1, 2021, 4:36am UTC](https://forums.sketchup.com/t/global-variables/147071/49 "2021-01-01T04:36:41Z")

</div>

Hi Majid,

Sounds to me like you are getting into the realm of Preferences.

In my plugin FlatText, I ended up keeping three sets of user preferences:

1. The “factory defaults” that are hard coded into the plugin itself,
2. The “model preferences” that are stored as attributes on a model, and
3. The “default preferences” which are stored in an external file. I use a simple text file for this.

When FlatText needs to refer to the preferences, it first looks at the model to see if there is a preferences attributes dictionary on the model (“model preferences”). If there is none, it then looks for the external file of default prefs. If that does not exist, it then falls back to the factory default prefs.

I used to store the default preferences text file in the Plugins folder. The problem with this is that when the User upgrades SketchUp, they are in effect moving to a new Plugins folder, and the prefs file does not move with them. I solved this (with help from this forum) by putting my prefs files in a different directory than Plugins. Now it is available to any version of SketchUp that can run FlatText.

This all sounds complicated, and it is, but it is the sort of trouble we should go to as developers to make things simple and easy for our Users.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![majid866](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/majid866/32/210257_2.png) [@majid866](https://forums.sketchup.com/u/majid866)\
**Post date:** [January 1, 2021, 6:10am UTC](https://forums.sketchup.com/t/global-variables/147071/50 "2021-01-01T06:10:40Z")

</div>

Dear friend,  
Thank you so much for your information and for sure it will be very useful for my plugins. As I learned from Dezmo we can save our data in a special file by using the following codes. File address is “c:\Users\USER\_NAME\AppData\Local\SketchUp\SketchUp2020\SketchUp\PrivatePreferences.json”  
and data saved like this…  
“MajWall”: {  
“0”: “"7.0,125.0,10.0,3.0,0.0,0.0,0.4,0.0,0.0"”,  
“3”: “"20.0,315.0,0.0,5.0,0.0,0.0,1.0,0.0,0.0"”,  
“4”: “"0.2,3.2,0.0,0.05,0.0,0.0,0.01,0.0,0.0"”  
},  
As you said this file will clear when we upgrade SU and it is possible to use a special file and save our information there. Would you please let me know by which code we can save data and how to read data? If your solution is JSON codes, please let us know (I think JSON also can solve this problem). Thank you in advance.

> [@majid866](#):
>
> ```auto
> result = Sketchup.write_default("section", "key", "my_value")
> result = Sketchup.read_default("section", "variable", "default")
> 
> ```

---

<div class="post-metadata">

**Author:** ![dpc](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/dpc/32/59801_2.png) [@dpc](https://forums.sketchup.com/u/dpc)\
**Post date:** [January 1, 2021, 7:38pm UTC](https://forums.sketchup.com/t/global-variables/147071/51 "2021-01-01T19:38:48Z")

</div>

I’m sure JSON will work just fine. I’m just partial to .txt files.  
As for the file location, I make a “company” directory and put my prefs files in a subdirectory named for the plugin. (My company directory contains prefs directories for several of my plugins.) The company directory is in the same directory that the SketchUp app uses for its own prefs (including Plugins).  
Here’s the code I use:

```
    appDataStr = ''
    if Sketchup.platform == :platform_win # Windows
      appDataStr = ENV['APPDATA'].tr File::ALT_SEPARATOR, File::SEPARATOR
    else # Mac
      appDataStr = File.expand_path('~/Library/Application Support')
    end
    COMPANY_DIR = File.join( appDataStr, COMPANY_APP_DIR )
    PATH_PREFS = File.join( COMPANY_DIR, PLUGIN_NAME )

```

This code is in the unencrypted .rb file named for the plugin (the one that’s NOT in the plugin’s folder of the same name) so that a sophisticated user can edit the paths, to something like a central prefs directory on a shared server, for example.  
In my plugin’s (encrypted) code, I use the COMPANY\_DIR and PATH\_PREFS constants to look for, and if necessary create, my company directory and my prefs directory.

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [January 1, 2021, 7:44pm UTC](https://forums.sketchup.com/t/global-variables/147071/52 "2021-01-01T19:44:19Z")

</div>

[quote=“majid866, post:50, topic:147071”]  
Thank you in advance.

> [@majid866](#):
>
> ```ruby
> result = Sketchup.write_default("section", "key", "my_value")
> result = Sketchup.read_default("section", "variable", "default")
> 
> ```

The above is wrong because the **key** name is not the same in both method calls.

You would use **either** `"key"` or `"variable"` but **not** both.

[Previous page](https://forums.sketchup.com/t/global-variables/147071.md?page=2)
