Also, instead of making an init method and using .new then .init, just make an initialize method which takes the parameters for the init method, and do class.new(parameters)
and yeah, there must be something else interfering with that array.push call
Since it says undefined method push for nil class, that means the variable named array is the one that is nil (coz you're calling array.push after all, so it looks for the push method inside the array object)...
huh? did I miss something? the array is nil? how? it worked everytime to made up an array with array=[] and then use it later...
Probably show us the entire script that ur making so we could see where the problem lies... The error that you posted clearly shows that the array is nil...
@Dekita: yea, I know. this shouldn't work at all.
no no, I made this:
class MyClass @myArray = [] def method1 ... end def method2 ... end def method3 do something lot object = MyClass.new(param1, param2, ..., paramN) @myArray.push(object) endend # of MyClassand I didn't see any mistake there...
oh man... C++ is way more easy to me >.>
@myarray is never being initialized. Do this instead.
(@myArray||=[]).push(object)That will make @myarray equal an array if it does not already, from there you can push onto it. 🙂
Edit: and remove the @myarray = [] from the top of your code.
Edit 2:
To be clear, I mean put that new line in place of the line you have that pushes onto the array in method 3.
Uhm, try to do it this way
class MyClassattr_accessor: myArray def initialize @myArray = []enddef method1...enddef method2...enddef method3do something lotobject = MyClass.new(param1, param2, ..., paramN)@myArray.push(object)endend # of MyClassI do think instance variables (the @name variables) are only initialized (and can only be used) once the object/class is created, so in your case, it actually never initializes to [] since the @myArray=[] line is only run when the game inits, in which case the instance of the class is not yet existent.
oh crap ... why is ruby torturing me so much? XD
and why is it working in events scripts but not in the scripts itself?
anyway, thanks again for help!
uhm... this was for showing purpose, the array is in a different class XD
and now it works with your help! =)
did I see this right that attr_accessor :varName is to declare class variables? and later I have to initialize them! oh lord...
@Dekita: ah thanks! but I think it is not very useful in my case if it is shared by all instances.
@Fear
the
attr_accessor :variable
simply automates the creation of the instance variable's writer and reader methods.
Also I do think it's (@variables) called Instance variables since it's per instance of the class, while class variables are the @@variables, which is 1 variable for ALL instances of the class.
attr_accessor :varname # ^ the purpose of this is to aovid having to write the following... def varname @varname end def varname=(varname) @varname = varname endNotice, neither of these tehcniques actually initializes the variable, it just informs the script of the variables existance, so if for example if was called from outwith the class... ie, Object.varname is called on the object,it will return the value nil (which btw, nil is actually an object), rather than raise an error. hope that helps clarify a little 🙂
yeah, you won't need to use it unless you're planning to access that variable outside of that class.
okay, I think this helps in understanding =)
well... now I have to look how I fill my windows with information ^^
I come back here if I have questions XD
@FeaR616: In ruby, classes are just instances of the Class class and thus equivalent to other objects. By writing @myArray = [] outside of an instance method you accessed the instance variable of self. In the given scope, you changed the value of an instance variable of the class object MyClass itself.
You only need attr_accessor if the variable should be accessed from outside the object (getter and setter methods in other languages). The equasions have been already pointed out. 🙂
If you want to initialize the variable with a value other than nil, you can do that explicitely within the initialize method (EDIT: or do as Dekita demonstrates 10 posts above).
@Dekita:
I prefer this method expecially when combining multiple sets:
[*'0'..'9', *'A'..'Z', *'a'..'z', '_', *special_characters]
(as an alternative to using flatten(1) or +).
However, I don't even use a consistent style at the moment...
Yea, I'm fairly familiar with 'splatting things for kicks', such as this..
@mvarg ||= ['i',['p',*['i']*4,'p'],'MoveWindow',USER] @mvwin ||= Win32API.new(*@mvarg.reverse)But had never even thought of using a range with it.
Personally, I feel my only consistency is being inconsistent. Since I started coding my 'coding style' has changed dramatically. (as I'm sure it does with most people as they find things they like and generate preferences) but like, reading code I wrote not even a few months ago and now I would have written it completely differently. IMO - theres no harm in trying out something new, providing its not excessive.
A Performance question:
I heard that eval can slow down the game if used unwise...
So, I have this little dilemma...
I got two ways to set up my method. One uses an eval method and it takes only 2 lines to do.
The other one takes 18 lines with a case statement but without any eval method.
The method will be called every single time the player attacks or does any battle actions and the battle system in question is an ABS, so the player will be mashing buttons left and right for sure to attack the enemies...
What should be better to use?
Another question on an entirely different subject:
So, I have this note-tag grabber method:
def cooldown_bonus ary = Array.new if @note =~ /Cooldown Bonus = <(\d+[ ].*(?:\s*,\s*\d+[ ].*)*)>/i i = 0 $1.split(", ").map.each do |info| ary = Array.new info =~ /(\d+) (.*)/i ary.push($1.to_i) # value ary.push($2.to_s.downcase) # stat i += 1 end end return ary endWhich works fine.The actual note-tag would look like this, for example:
Cooldown Bonus = <12 AGI, 5 ATK, 2 MAT, ... >So, it is an "infinite" note-tag.After the method, I get an array like this:
ary = [[12,"agi"],[5,"atk"],[2,"mat"], ... ]And this is exactly what I need.I am pretty new in making these infinite long note-tags with mixed data (meaning number and string needed from it), so I was wondering if there is an easier/shorter method of getting the data from this kind of note-tag... So... Is it? 😀