Dynamic Constant Assignment

Dynamic constant assignment

Your problem is that each time you run the method you are assigning a new value to the constant. This is not allowed, as it makes the constant non-constant; even though the contents of the string are the same (for the moment, anyhow), the actual string object itself is different each time the method is called. For example:

def foo
p "bar".object_id
end

foo #=> 15779172
foo #=> 15779112

Perhaps if you explained your use case—why you want to change the value of a constant in a method—we could help you with a better implementation.

Perhaps you'd rather have an instance variable on the class?

class MyClass
class << self
attr_accessor :my_constant
end
def my_method
self.class.my_constant = "blah"
end
end

p MyClass.my_constant #=> nil
MyClass.new.my_method

p MyClass.my_constant #=> "blah"

If you really want to change the value of a constant in a method, and your constant is a String or an Array, you can 'cheat' and use the #replace method to cause the object to take on a new value without actually changing the object:

class MyClass
BAR = "blah"

def cheat(new_bar)
BAR.replace new_bar
end
end

p MyClass::BAR #=> "blah"
MyClass.new.cheat "whee"
p MyClass::BAR #=> "whee"

Dynamic constant assignment main.rb:6: Ruby

When you define a Capitalized variable in Ruby, that is a constant - it is a special kind of variable that is not allowed to change value (well, technically you can change it with const_set, but that's not really relevant here).

Because of this limitation, Ruby won't allow you to change constants from within functions. It assumes the function will be called many times, which would cause the constant to change value, which as I just mentioned is illegal.

So, quick fix, just replace your Odd and Even with the lowercase versions odd and even. That way they're regular variables and not constants.

Why there is an error in the code dynamic constant assignment Ruby

You reassign WAYS_TO_WIN every time win_play is called

def win_play(board)

WAYS_TO_WIN =

define WAYS_TO_WIN outside of the method, e.g. with all the other constants

NUMBER_SQUARES = 9  # number of fields on the board
WAYS_TO_WIN = ...

Dynamic Constant Assignment error in Ruby

You get that error because you are trying to make a constant, non-constant. Once it's been assigned you can't go back and change that. You could refactor this to use only local variables rather than constants:

def calculateGST(price)
price * 0.15
end

puts "Enter a value: "
cost = gets.chomp.to_f

gst_paid = calculateGST(cost)
cost_no_gst = cost - gst_paid

puts """
Cost: #{cost}
GST included: #{gst_paid}
cost no GST: #{cost_no_gst}
"""

Dynamic constant assignment Ruby

Names that start with upper-case letter are constants. In your code you assign a non-constant (dynamic) value to a name that represents a constant. Hence the error.

Console_Screen = Screen.new

Use local variable name convention (snake_case)

console_screen = Screen.new

Dynamic Constant Assignment Error

First, you should sort out the syntax errors, starting from the first reported one - often the rest will disappear.

In the line 14 change the return to return an array:

  return [data, labels]
end

Ruby thinks that you should not set a constant from a method. Setting it from a class definition (outside any 'def' block) would not be an error. If you really want to create a constant from a method, use this:

self.class.const_set(:FAILED_BUILDS, `mysql -h ....

But my question is: why do you want to set a constant? An instance variable may be a better solution.

Dynamic constant assignment when using Graphql for Shopify API

And here is the solution. Super hard to believe this is a "new" way of accessing data via an API. Its slower, more complicated and much more verbose. I don't get the benefit at all.

  def run_first_query
query = <<-'GRAPHQL'
query($first: Int){
products(first: $first) {
pageInfo {
hasNextPage
}
edges {
cursor
node {
id
title
featuredImage {
originalSrc
}
}
}
}
}
GRAPHQL
first = {
"first": number_of_products_to_return,
}
Kernel.const_set(:ProductQuery, graphql_client.parse(query))
@query_result = graphql_client.query(ProductQuery, variables: first)
end

Assigning a variable the length of a string or array causes a dynamic constant assignment error in Ruby

Turns out that if the 1st letter of a variable is capitalized, it's treated as a constant. "L" should be changed to "l".

Using structs in Ruby on Rails gives dynamic constant assignment (SyntaxError)

The error explains what the problem is - you have a constant being assigned in a context that's too dynamic - i.e. inside the index method.

The solution is to define it outside:

DashItem = Struct.new(:name, :amount, :moderated)
def index
@dashboard_items = []
...


Related Topics



Leave a reply



Submit