Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6703577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:11:16+00:00 2026-05-26T07:11:16+00:00

I have a method that’s used in many places in my app, both in

  • 0

I have a method that’s used in many places in my app, both in some controllers and other models.

class MyClass
  LONG_CONSTANT_1 = "function(x) { some_long_js_function }"
  LONG_CONSTANT_2 = "function(x) { another_really_long_js_function }"

  def self.my_group_method(my_constant)

    count = MyClass.collection.group(
      :keyf => my_constant,
      :reduce => "function(x, y) {y.count += x.count;}"
    )

  end  
end

So even though the method called inside my_group_method is related to MongoDB, the question is itself not related at all, basically I want to be able to call

MyClass.my_group_method(LONG_CONSTANT_2)

or

MyClass.my_group_method(LONG_CONSTANT_1)

(there are actually several more constants needed but the example has only 2)

Unfortunately my implementation here results in the error: NameError: wrong constant name LONG_CONSTANT_1

Any ideas at how to best implement this behavior? I’m likely going to have several long constants (which are actually JS functions as strings that get sent to MongoDB), what am I getting wrong about the design pattern I’m using here?

Any help would be greatly appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:11:17+00:00Added an answer on May 26, 2026 at 7:11 am

    Sounds like you have a scoping problem with your constants. You want to call the class methods as:

    MyClass.my_group_method(MyClass::LONG_CONSTANT_1)
    MyClass.my_group_method(MyClass::LONG_CONSTANT_2)
    

    I can get this NameError:

    uninitialized constant Object::LONG_CONSTANT_1 (NameError)
    

    from something structurally similar to your code but not a “wrong constant name” error.

    If all that namespacing for the constants is too much then you could document the expected inputs (probably as symbols) and then inside your class:

    INPUTS = (
        :long_symbol_1 => LONG_CONSTANT_1,
        :long_symbol_2 => LONG_CONSTANT_2
    )
    
    # Frobnicate the pancakes.
    #
    # The `which` argument should be `:long_symbol_1` or `:long_symbol_2`.
    # etc.
    def self.my_group_method(which)
        str = INPUTS[which]
        raise ArgumentError => "No idea what you're talking about" if(!str)
        #... continue on
    end
    

    That approach would give you some argument safety, your nice constants, and avoid the noise of having to prefix the argument with MyClass:: in the callers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have method that returns module path of given class name def findModulePath(path, className):
I have a method that prints private variable of the class. public class Test
I have method that run many times. I dont want to nest ifs inside
I have some relatively large legacy method that I would like to refactor. It
I have a method that adds some subviews (images/buttons) to my ScrollView. The thing
I have a method that is reading some information from a sqlite db and
I have a method that reflects balls against a wall, and also each other.
i have a method that creates a Process calling a console app. double myProcess()
I have a method that has some data objects passed into it, does some
I have a method that should post some data to a PHP file: -(void)submitForm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.