Creating global objects with optional params

Status
Not open for further replies.

DarknessFalls

Rpg Maker Jesus - JS Dev.
Veteran
Joined
Jun 7, 2013
Messages
1,393
Reaction score
210
First Language
English
Is this possible? If I have something like:

class Sample    attr_reader :obj_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?
 
Last edited by a moderator:

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
$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.
 

DarknessFalls

Rpg Maker Jesus - JS Dev.
Veteran
Joined
Jun 7, 2013
Messages
1,393
Reaction score
210
First Language
English
I figured as much. this just means I have to come up with some other way to deal with creating currencies. Thanks
 

Solistra

Veteran
Veteran
Joined
Aug 15, 2012
Messages
593
Reaction score
247
Primarily Uses
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
 

DarknessFalls

Rpg Maker Jesus - JS Dev.
Veteran
Joined
Jun 7, 2013
Messages
1,393
Reaction score
210
First Language
English
this is going to such. So much for my idea of allowing you to create currencies on the fly. thanks guys
 

Solistra

Veteran
Veteran
Joined
Aug 15, 2012
Messages
593
Reaction score
247
Primarily Uses
Creating currencies "on the fly" should be quite simple. Contact me through PMs if you need some specific help with your idea.
 

DarknessFalls

Rpg Maker Jesus - JS Dev.
Veteran
Joined
Jun 7, 2013
Messages
1,393
Reaction score
210
First Language
English
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.
 
Last edited by a moderator:

Solistra

Veteran
Veteran
Joined
Aug 15, 2012
Messages
593
Reaction score
247
Primarily Uses
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.
 
Last edited by a moderator:

DarknessFalls

Rpg Maker Jesus - JS Dev.
Veteran
Joined
Jun 7, 2013
Messages
1,393
Reaction score
210
First Language
English
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 :D
 

BigEd781

undefined method 'stupid_title' found for nil:NilC
Veteran
Joined
Mar 1, 2012
Messages
940
Reaction score
304
First Language
Dothraki
Primarily Uses
N/A
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.
 
Status
Not open for further replies.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??
Yay, now back in action Happy Christmas time, coming back!






Back in action to develop the indie game that has been long overdue... Final Fallacy. A game that keeps on giving! The development never ends as the developer thinks to be the smart cookie by coming back and beginning by saying... "Oh bother, this indie game has been long overdue..." How could one resist such? No-one c

Forum statistics

Threads
105,857
Messages
1,017,019
Members
137,564
Latest member
McFinnaPants
Top