Creating global objects with optional params

Is this possible? If I have something like:

class Sample    attr_reader 😮bj_info       def initialize(obj_info = nil)        if !obj_info.nil?            @obj_info = obj_info        else            @obj_info = 0 end    endendand then some where I would initialize this into something like:

Code:
$sample_object_demonstration = Sample.new
 Is it ever possible for me to use $sample_object_demonstration to pass obj_info param to the init? in say a script call?
$sample_object_demonstration to pass obj_info param to the init?
\
If you want to initialize it with obj_info just pass that in during instantiation.


If you want to assign a value afterwards you just need to set the variable.


You don't call the constructor after constructing the object.
I figured as much. this just means I have to come up with some other way to deal with creating currencies. Thanks
Tsukihime's right, assuming I'm understanding your question correctly. Also, this is a simpler way to write what you used as an example (just because I saw the opportunity to improve it):

Code:
class Sample  attr_accessor :obj_info    def initialize(obj_info = nil)    @obj_info = obj_info || 0  endend
this is going to such. So much for my idea of allowing you to create currencies on the fly. thanks guys
Creating currencies "on the fly" should be quite simple. Contact me through PMs if you need some specific help with your idea.
this is going to such. So much for my idea of allowing you to create currencies on the fly. thanks guys
Ruby is a great language for creating things on the fly.
Ruby is a great language for creating things on the fly.
I am not sure how but that sounded snide in my head....Maybe it wasn't intended.

Any ways, Can some one tell me how this doesn't work?

module CURR CURR_HASH = { 'GC' => { :full_name => 'Guild Currency', :max => 9999, :description => 'Example Stuffs', }, }endclass Create_CurrencyObject attr_reader :currencies def intitialize @currencies = [] end def create CURR::CURR_HASH.each do |key, value| hash_array = [key, value[:full_name], value[:max], value[:description], 0] @currencies.push(hash_array) end end endI get an error saying that .push() doesn't exist for a nil:NilClass no method error.
The code you posted works in IRB unless I subconsciously bug-fixed something, so my assumption is that you have a typo.


What you're doing with that code really doesn't make any sense to me, though -- I don't see what the benefit is, and classes are really ill-suited to being used the way you've designed your Create_CurrencyObject class.


Edit: You wrote intitialize instead of initialize.
I figured it was a spelling mistake. Thanks. You don't see the benefit because I haven't given you the whole class or the whole concept for what I am doing. thanks though 😀
This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/creating-global-objects-with-optional-params.15432/