# Use Singleton Error

**URL:** <https://forums.sketchup.com/t/use-singleton-error/230404>\
**Category:** Ruby API\
**Tags:** import\
**Created:** [May 18, 2023, 4:09am UTC](https://forums.sketchup.com/t/use-singleton-error/230404 "2023-05-18T04:09:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ljs](https://avatars.discourse-cdn.com/v4/letter/l/49beb7/32.png) [@ljs](https://forums.sketchup.com/u/ljs)\
**Post date:** [May 18, 2023, 4:09am UTC](https://forums.sketchup.com/t/use-singleton-error/230404/1 "2023-05-18T04:09:59Z")

</div>

When I updated to SketchUp Version 23.0.419, an error occurred in the extension that was being developed. Before the update, running SketchUp didn’t give me any errors. The contents of the error are as follows.

Error: #\<NameError: undefined method `extend_object' for class `Singleton’  
Did you mean? extended\>  
C:/Program Files/SketchUp/SketchUp 2023/Tools/RubyStdLib/Singleton.rb:150:in `undef_method' C:/Program Files/SketchUp/SketchUp 2023/Tools/RubyStdLib/Singleton.rb:150:in `singleton class’  
C:/Program Files/SketchUp/SketchUp 2023/Tools/RubyStdLib/Singleton.rb:138:in module:Singleton’  
C:/Program Files/SketchUp/SketchUp 2023/Tools/RubyStdLib/Singleton.rb:94:in `<top (required)>' C:/Program Files/SketchUp/SketchUp 2023/Tools/RubyStdLib/rubygems/core_ext/kernel_require.rb:83:in `require’  
C:/Program Files/SketchUp/SketchUp 2023/Tools/RubyStdLib/rubygems/core\_ext/kernel\_require.rb:83:in `require’

I wonder what the reason is.

---

<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:** [May 18, 2023, 6:17pm UTC](https://forums.sketchup.com/t/use-singleton-error/230404/2 "2023-05-18T18:17:25Z")

</div>

When I do:

```ruby
require 'singleton'

```

… at the Ruby console, I get **no error** in the latest version of SketchUp which was updated to use Ruby 2.7.7.

Note, that I do **not** see any changes in the `"Tools/RubyStdLib/singleton.rb"` file between Ruby 2.7.2 and 2.7.7. In fact, both modules are version `"0.1.0"`.

* * *

When you updated, did you download the installer and right-click it and choose “Run as administrator” ? … or did you do the web update from within a running SketchUp ?

If the latter, then shut down all applications, reboot the computer, then go download the full installer, and right-click it and “Run as administrator”. Afterward, reboot the computer again.

Then see if you can reproduce the Ruby error.

* * *

**When is the error occurring for you ?** (From the backtrace you posted, it looks like just requiring the `Singleton` library has caused the error.)

Are you modifying the [`Singleton`](https://ruby-doc.org/stdlib-2.7.2/libdoc/singleton/rdoc/Singleton.html) module in any way ? directly, or indirectly with `include` or `append` ?

Is your extension (or another) trying to `extend` the custom class that has [`Singleton`](https://ruby-doc.org/stdlib-2.7.2/libdoc/singleton/rdoc/Singleton.html) mixed in ?

---

<div class="post-metadata">

**Author:** ![MSP\_Greg](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/msp_greg/32/206066_2.png) [@MSP\_Greg](https://forums.sketchup.com/u/MSP_Greg)\
**Post date:** [May 24, 2023, 2:35pm UTC](https://forums.sketchup.com/t/use-singleton-error/230404/3 "2023-05-24T14:35:53Z")

</div>

Case insensitive file systems can be a PITA. The file in question is `singleton.rb`, not `Singleton.rb`.

Somewhere in your code you probably have

```ruby
require "Singleton"

```

but elsewhere you have

```ruby
require "singleton"

```

Or, your error log shows:

```auto
C:/Program Files/SketchUp/SketchUp 2023/Tools/RubyStdLib/Singleton.rb

```

From a console:

```auto
ruby -rsingleton -rSingleton -e "puts 'case issue'"

```

throws your error, but the following does not:

```auto
ruby -rsingleton -rsingleton -e "puts 'case issue'"

```

Ruby is loading the file twice because of the ‘casing’ change.

---

<div class="post-metadata">

**Author:** ![MSP\_Greg](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/msp_greg/32/206066_2.png) [@MSP\_Greg](https://forums.sketchup.com/u/MSP_Greg)\
**Post date:** [May 24, 2023, 3:59pm UTC](https://forums.sketchup.com/t/use-singleton-error/230404/4 "2023-05-24T15:59:44Z")

</div>

JFYI, I’m not sure what I think of this, but considering that most desktop OS’s do have case insensitive file systems, Ruby could handle this type of error in a different manner.

It may be considered ‘expected’ behavior.

I filed an issue on the Ruby bug tracker:

See [Bug #19691: Case insensitive file systems, require filename casing - Ruby master - Ruby Issue Tracking System](https://bugs.ruby-lang.org/issues/19691)

---

<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:** [May 24, 2023, 9:58pm UTC](https://forums.sketchup.com/t/use-singleton-error/230404/5 "2023-05-24T21:58:47Z")

</div>

I think it is expected behavior for case-sensitive file systems.

But I **agree** , as I believe that a great majority of the Ruby world is on case-insensitive file systems, and that there should be a default `ARGV` switch for the `Kernel` require suite of methods to do insensitive path matching into the `$LOADED_FEATURES` array. See C Ruby API `rb_require_string()`

The SketchUp world has dealt with this by down casing the path string arguments to [`Sketchup::require`](https://ruby.sketchup.com/Sketchup.html#require-class_method) so that the entire path string pushed into the `$LOADED_FEATURES` array is lower cased both for it’s internal decryption condition and when passed on to a global [`require`](https://ruby-doc.org/core-2.7.2/Kernel.html#method-i-require) call for unencrypted `rb` files. (This API feature then can cause loading issues on case-sensitive file systems. I’m not even sure if SketchUp is supported on case-sensitive file system installs.)

---

<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:** [May 24, 2023, 10:32pm UTC](https://forums.sketchup.com/t/use-singleton-error/230404/6 "2023-05-24T22:32:49Z")

</div>

Good catch on the double loading.

The issue arises because of the [`Singleton`](https://ruby-doc.org/stdlib-2.7.2/libdoc/singleton/rdoc/Singleton.html) library’s switching off object extending for classes that mix in this library.

It does this (in part) by calling `undef_method(:extend_object)` (at line `150` in `"singleton.rb"`).

The first time loading the file is fine, the class callback method is undefined. This is proven by the second inadvertent loading where the [`extend_object`](https://ruby-doc.org/core-2.7.2/Module.html#method-i-extend_object) class method cannot be found to be undefined, hence the [`NameError`](https://ruby-doc.org/core-2.7.2/NameError.html) seen in the first post.

* * *

The `"singleton.rb"` file says at line `149` says …

```ruby
# extending an object with Singleton is a bad idea

```

… meaning that the Ruby team does not want us to `include` the `Singleton` module into a custom mixin module and then try to use this mixin as an argument to `extend` another object, class or module.

In fact, the [`Singleton`](https://ruby-doc.org/stdlib-2.7.2/libdoc/singleton/rdoc/Singleton.html) module does not allow itself to be included into a module at all, as just after undefining [`extend_object`](https://ruby-doc.org/core-2.7.2/Module.html#method-i-extend_object) in the file it overrides [`append_features`](https://ruby-doc.org/core-2.7.2/Module.html#method-i-append_features) to raise a [`TypeError`](https://ruby-doc.org/core-2.7.2/TypeError.html) if this is attempted by code.

What is a bit weird, perhaps an oversight as the [`Singleton`](https://ruby-doc.org/stdlib-2.7.2/libdoc/singleton/rdoc/Singleton.html) module likely predated the [`prepend`](https://ruby-doc.org/core-2.7.2/Module.html#method-i-prepend) feature, is that it does not override [`prepend_features`](https://ruby-doc.org/core-2.7.2/Module.html#method-i-prepend_features) to prevent it from prepending a module.  
**Should a Ruby library issue be opened with regard to this?**

* * *

Note however, that nothing has been done to prevent the actual singleton instance object from being extended **after creation** via the [`Object#extend`](https://ruby-doc.org/core-2.7.2/Object.html#method-i-extend) method. This seems to be okay to do.

---

<div class="post-metadata">

**Author:** ![MSP\_Greg](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/msp_greg/32/206066_2.png) [@MSP\_Greg](https://forums.sketchup.com/u/MSP_Greg)\
**Post date:** [May 24, 2023, 11:41pm UTC](https://forums.sketchup.com/t/use-singleton-error/230404/7 "2023-05-24T23:41:06Z")

</div>

> [@DanRathbun](#):
>
> I think it is expected behavior for case-sensitive file systems.

That hasn’t really been mentioned. Using the above on Ubuntu, if the file `Singleton.*` exists on the `$LOAD_PATH`, it will be loaded, otherwise it will raise `LoadError`.

From the above linked Ruby issue, it seems that macOS is functioning as expected in Ruby 3.1 and later, and it’s broken in Windows, as shown here (I tested back to Ruby 2.5). It is considered a bug, as one of the Ruby committers found that `File.realpath` is probably the culprit.

> But I **agree** , as I believe that a great majority of the Ruby world is on case-insensitive file systems

I might disagree. The majority of desktops are case insensitive, but most Ruby projects run on servers. An example might be a popular Ruby web/app server, which sees over 200k daily downloads.

GitHub, Shopify and other wellknown companies run very large RoR (Ruby on Rails) apps.

Both Discourse and Mastodon are also RoR apps.

---

<div class="post-metadata">

**Author:** ![ljs](https://avatars.discourse-cdn.com/v4/letter/l/49beb7/32.png) [@ljs](https://forums.sketchup.com/u/ljs)\
**Post date:** [May 25, 2023, 4:30am UTC](https://forums.sketchup.com/t/use-singleton-error/230404/8 "2023-05-25T04:30:47Z")

</div>

An error occurred right after the update, but after some time it worked. However, I have already modified it in a way that does not use singleton. Thank you for your reply.

---

<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:** [May 25, 2023, 4:38am UTC](https://forums.sketchup.com/t/use-singleton-error/230404/9 "2023-05-25T04:38:20Z")

</div>

> [@ljs](#):
>
> An error occurred right after the update, …

As we noted above, some other extension had already loaded it as `"singleton.rb"` but your code loaded it a second time as `"Singleton.rb"`.

The lesson is always use the exact case of the filename when making `require` calls.

> [@ljs](#):
>
> …, but after some time it worked.

Most probably the error on the second load attempt would not hurt how the [`Singleton`](https://ruby-doc.org/stdlib-2.7.2/libdoc/singleton/rdoc/Singleton.html) class works.
