Couldn’t find anything about it in the documentation. The code is:
# input
a440hz = 440
octave_division = 12
semitone_offset = [0,0,0,0,0,0,0,0,0,0,0,0]
tuning_stretch = 0
random_offset = 0
def tuning_generator
i = 0; while i < 128
puts frequency(i+1)
i += 1
end
end
tuning_generator
def frequency(note)
a440hz * (2 + tuning_stretch ** ((note - 57) / octave_division))
end
The frequency method is using the variables on top of the file. How to make them global variables so I can access them everywhere?
ps. don’t know for sure if this is the right formula for frequency
In addition to what @straight-shoota said, global vars can be considered a bad practice depending on the context. Not really applicable to this small example, but as a/your application grows bigger and bigger they probably aren’t the best solution anymore.
In Ruby those aren’t called global variables. They are called constants. It’s the same thing in Crystal. I don’t have a computer near y, but that Ruby code should work out of the box in Crystal.
I think both code examples are Crystal translations of a non-disclosed original Ruby code. One translation changed global variables to constants, the other to class variables.
@j.m Without knowing the overall intention of the code, it appears that these values are actually constant values. They’re not going to change during the runtime of the program. So using constants seems like a natural choice (even in Ruby).
Thank you for your reply. I know that those in my codes are not called global variables in crystal/ruby. I want to realize replacement of global variables that have the prefix $ in ruby. I understand that using constants or class variables depends on the context. I think it is better not to introduce an unnecessary class/module.