Calling a singleton method within the singleton class in Ruby?

NickJaremek

Consider the following Ruby code:

module MyModule
  class << self

    def process_item(item)
      item.capitalize
    end

    def foo=(item)
      @foo_ref=process_item(item)
    end

    def foo
      @foo_ref
    end

    self.foo = "initial foo"
  end
end

I want to set a default foo_ref variable inside the singleton class definition. However, the Ruby interpreter raises the following error:

singleton class': undefined method `foo=' for #(NoMethodError)

If I change self.foo = "Initial foo" to foo = "Initial foo" then I am creating a local variable within the singleton class, instead of calling the setter method.

Also, I do realize that I may have to put the process_item method in the module definition, outside the singleton class definition, so that it does not become a singleton method, but rather a helper method.

What would be the proper approach to this code (being able to call a singleton method within the singleton class definition, and having helper methods available within the singleton class definition)?

Fede Bonisconti

You can define the default value as:

module MyModule
  class << self

    def process_item(item)
      item.capitalize
    end

    def foo=(item)
      @foo_ref=process_item(item)
    end

    def foo
      @foo_ref ||= "initial foo"
    end
  end
end

What you were trying to do was to set foo to the singleton class, instead of setting foo to the class.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling a singleton method within an instance method in a module that extends itself

From Java

Calling a method of a @Singleton bean

From Dev

Is a static class within a singleton class, also singleton?

From Dev

Singleton method chaining in ruby

From Dev

Ruby: DRY class methods calling Singleton instance methods

From Dev

Calling a Fragment method from a singleton

From Dev

Difference between Singleton class and singleton method?

From Dev

Singleton method and class method Java

From Dev

Ruby Singleton, module vs class

From Dev

Get the name of the class in a Singleton method

From Dev

Calling Async and Sync Method on EJB Singleton

From Dev

Calling method on singleton before initialization finished

From Dev

Calling custom method from within a Ruby core class

From Dev

Singleton method vs. class method

From Java

Is this class a singleton?

From Dev

What are the scenarios when ruby creates a singleton class

From Dev

Ruby on Rails: Reference to a singleton class action in routes

From Dev

Retrieve a Ruby object from its singleton class?

From Dev

How do I access class methods in a define_singleton_method block in Ruby

From Java

Making a class singleton without using static method

From Dev

Cannot Initialize a class using the Singleton Method

From Dev

Init Method in Singleton Class (Objective-C)

From Dev

shim a sealed class singleton method and with MS Fakes

From Dev

React native export class and singleton method

From Dev

Invoking a singleton class in a abstract factory method

From Dev

Calling a method within the same class

From Dev

How can a class call a class method that is defined on a singleton class?

From Java

How to solve error with calling kotlin activity method from java singleton?

From Dev

Syntax for Calling Method with Completion in Swift using Singleton Pattern

Related Related

  1. 1

    Calling a singleton method within an instance method in a module that extends itself

  2. 2

    Calling a method of a @Singleton bean

  3. 3

    Is a static class within a singleton class, also singleton?

  4. 4

    Singleton method chaining in ruby

  5. 5

    Ruby: DRY class methods calling Singleton instance methods

  6. 6

    Calling a Fragment method from a singleton

  7. 7

    Difference between Singleton class and singleton method?

  8. 8

    Singleton method and class method Java

  9. 9

    Ruby Singleton, module vs class

  10. 10

    Get the name of the class in a Singleton method

  11. 11

    Calling Async and Sync Method on EJB Singleton

  12. 12

    Calling method on singleton before initialization finished

  13. 13

    Calling custom method from within a Ruby core class

  14. 14

    Singleton method vs. class method

  15. 15

    Is this class a singleton?

  16. 16

    What are the scenarios when ruby creates a singleton class

  17. 17

    Ruby on Rails: Reference to a singleton class action in routes

  18. 18

    Retrieve a Ruby object from its singleton class?

  19. 19

    How do I access class methods in a define_singleton_method block in Ruby

  20. 20

    Making a class singleton without using static method

  21. 21

    Cannot Initialize a class using the Singleton Method

  22. 22

    Init Method in Singleton Class (Objective-C)

  23. 23

    shim a sealed class singleton method and with MS Fakes

  24. 24

    React native export class and singleton method

  25. 25

    Invoking a singleton class in a abstract factory method

  26. 26

    Calling a method within the same class

  27. 27

    How can a class call a class method that is defined on a singleton class?

  28. 28

    How to solve error with calling kotlin activity method from java singleton?

  29. 29

    Syntax for Calling Method with Completion in Swift using Singleton Pattern

HotTag

Archive