Group: Member
Posts: 64
Type: Musician
RM Skill: Intermediate
Looks simple enough...but it does seem to have some unnecessary layers of complication. Why not take out the create method and place its contents in initialize?
CODE
class SomeClass
attr_reader :elements
def initialize @elements = Array.new end
def add(object) @elements.push(object) return @elements end
This is my whole script. I didn't want to post it because 1) I am still learning the fundamentals of ruby syntax. I understand ruby as a whole in terms of OOP and its related concepts. How ever The syntax is my enemy and how things are done over such languages as java and 2) I didnt want to be made fun of >_>
CODE
class RGSSVector
attr_reader :elements attr_reader :size
def initialize #Does nothing but initialize the class to be used #with global variables such as $vector end
def create(size) @size = size
if @size <= 0 raise "Error, vector size cannot be less then or equal to 0 elements in size." elsif @size != 0 @elements = Array.new(@size) return @elements elsif @size == 0 @elements = Array.new return @elements end end
def create() @elements = Array.new end
def clearVector if @size != 0 @size = 0 else @elements = Array.new return @elements end end
def sizeOfVector if @elements.empty? != true return @elements.size end end
def add(object) if @size != 0 for @size in @elements if @size < @elements @elements.push(object) return @elements end end else @elements.push(object) return @elements end end
def remove(object) if @size != 0 for @size in @elements if @elements.index(object) != nil @elements.delete(object) return @elements end end elsif @elements.empty? != true @elements.delete(object) return @elements end end
def isEmpty? return @elements.empty? end
end
This is why initialize is never used.
Add never add in this code. (for loops in ruby are also giving me a hard time when I am so use to doing for (int i = 0; i<something; i++){})
Thanks for your help
__________________________
Games I am working on | Each image is a link to the game
Group: +Gold Member
Posts: 1,199
Type: Scripter
RM Skill: Undisclosed
ah, I see, well, no one's going to fault you for learning RGSS/2; a couple things I'd like to throw off the bat, if you're gonna attribute size and elements, be sure to initialize them (in the def initialize, put: @elements =[], @size = 0) because all variables start out as nil, and you have bits of code where you are checking only if @size is <= 0. also, with your def create(size) and def create() you can compound those into:
CODE
def create(size=nil) end
This way saves coding and achieves the same result. also, when specifying a new variable, you dont need to define it by it's type like in C++, so instead of @ary = Array.new you can put @ary = []
and regarding for loops, yeah, they can be confusing in comparison to C++/Java's way, but if you liken it to javascript's for each loop, it might make a little more sense.
edit- just checked it, if you put:
CODE
@size = 0 @elements = []
In def initialize, that should fix it, and you can leave the rest as is without issues.
__________________________
Got 30 minutes? Then you've enough time to play this awesome game: - potentially promising project page - thanks holder
I cannot compound them because unlike other languages like actionscript you cannot have "optional parameters" in ruby. so I have one blank that states create an array, and one with a parameters that says create an array of this size.
are my for loops correct?
Also is this code, or can we explain why this code, is not adding anything to the array?
ok will try this
also can you explain how to create a global variable INSIDE THIS CLASS to be used across other scripts?
also doing def create(size=nil) messes up the if statement of if @size <= 0 do this. it throws a no method error on <= should it be in this case !=0? the reason for the check is if the user enters a size we all know vectors of fixed sizes cannot be 0 or less, or other wise how will you store elements
one last thing, both create methods have been changed to createVectorOfSize(size) and createVector() because ruby does not support method over loading >_>
This post has been edited by Adrien.: Sep 1 2011, 11:26 AM
__________________________
Games I am working on | Each image is a link to the game
Group: +Gold Member
Posts: 1,199
Type: Scripter
RM Skill: Undisclosed
Ruby does allow optional parameters, like I showed, you can do def optionalArg(argument=<defaultvaluehere>) you just need to check if argument == <defaultvaluehere> for example:
CODE
def create(size=nil) if size == nil @elements = [] return @elements elsif size <= 0 raise "Error, vector size cannot be less then or equal to 0 elements in size." else @elements = Array.new(size) return @elements end end
edit- all you need to do is have it catch the nil first, like shown above
2) To me, it looks like @size represents the size of @elements, which you dont need to do, you can put @elements.size or .length to get the size of @elements, but other than that, I guess your for loops are fine.
3) The code wasnt pushing to the array because in the def add you have the comparison: if @size != 0 nil != 0 and thus that if is entered, then the for loop runs basically like: for (size = nil; size<elements; size++) which doesnt itterate because nil and nil already match up (elements is also nil at the beginning) and so its skipping the push process. (I hope this is all making sense)
4) basically, if you specify a global variable ($<namehere>) it's accessable from anywhere; all you'd need to do would be to specify it in the scope you need it.
also, it would seem that @size is unnecessary, because of the fact that you can use @elements.size or .length
edit - ruby doesnt allow overloading, no, instead it uses polymorphism, and default values, like stated above.
__________________________
Got 30 minutes? Then you've enough time to play this awesome game: - potentially promising project page - thanks holder
@Size is used to set the size of the array. so if you want you can have a vector of any size through not setting the size or you can have a size specified and the vector will only hold that many elements. so:
if nil, then create a array which will hold as many as possible elements, if size = 5 then create an array capable of holding only 5 elements. thats my logic behind it. One subject i struggled heavily on in school was the idea of polymorphism >_> so please be paitent if I tend to ask question I should know or seem dumb.
I see that ruby has a vector class and its largley array based - hence my implementation here. What i see they do is create @element = []. They do not set a size, I was planning to set that size by stating the size you enter is as many elements as you can hold. .size is used to say how many elements are in the vector. because, as stated before, the vector class is largley array based I can use array based methods inside my methods to gather data.
in response to number 3, so by recreating the create statement as you stated, the add should now work?
in response to number 4, if I were to say (at the top of this script out side the class) $Vector = RGSSVector.new and then call $Vector.create I get the error undefined method. Thats why I am strugelling with global variables because I do not understand where I need to initalize a global variable to avoid this error
Other responses
-> Whats the difference between polymophism and method overloading and how does it make for better/or worse programming in ruby?
When googleing how to do optional parameters in ruby I got a lot of forums and blog posts stating that ruby does not allow optional parameters but that there was a work around with using hash tags:
My hash tag usage might be a bit off in this example but i hope you get the main idea.
I did not know you could go method (value=nil) (note: you can also do this in action script and it just clicked to me >_>)
Other
With create(size=nil)
if I do vector.create and not pass anything in then I get wrong number of arguments, (0 for 1). I thought it should default to nil?
also in my add function (when I pass in 1 to create it works) on the second if I am getting the error no method error for '<' when comparing if the size I set for the array is less the elements in the array, since I set the array size to 1 should this be <=, if so that poses the problem of if I have 5 elements in an array that can hold 5 elements, and i push one more it will now contain 6 elements.....(note: I still get no method error on <=)
Thanks for your help. If I ever get this working I will credit you in the script
__________________________
Games I am working on | Each image is a link to the game
Group: +Gold Member
Posts: 1,199
Type: Scripter
RM Skill: Undisclosed
1) @elements.size is dynamic in comparison to @size, you can only set @size when you're creating. if you're pushing values to @elements, the size will become larger than @size eventually, then you have issues; which is why you shouldnt use @size.
2) no, rewriting 'create' will not be enough, you need to set force @size to an actual value, or omit it, or make the if statement that surrounds the for loop have a condition for if @size == nil
3) like I mentioned in your other topic, you dont use global variables for self, or reflection. if you wanted to run def create from within the vector class you would use self.create, or, you'd initalize the vector class AS the global variable ($myvector = Vector.new) and then call create from the variable ($myvector.create). the reason why you get the error is because you're calling def create before it was interpreted
4) they're generally the same, with a few differences, polymorphism includes method overloading. but polymorphism is actually irrelevent in this case, I was just explaining one of the ways of 'overloading' arguments in Ruby
5) ruby does allow optional parameters, see Window_Selectable under initialize, spacing is default to 32
6) ruby does allow nil as default I just double checked it again to make sure. check to make sure you're using one def and not 2, and that you wrote it right.
7) This is the result of using @size separate from @elements.size (refer back to 1)
Hope that helps!
__________________________
Got 30 minutes? Then you've enough time to play this awesome game: - potentially promising project page - thanks holder
It does but it leave me with more question >_> I hate asking questions. >_>
1) If I am to remove @size all together how will I Set the size of the array to that of which the user sepcifies? for example user wants a vector to contain 5 elements and only 5 elements. How would this get done if @size is to be removed? as you see when you se the size I am doing @elements = Array.new(@size) according to the ruby doc for arrays Array.new(size) allows you to set the size of the array to contain that many elements.
2) for global variables then the user of this script will have to create their own global variable? as I cannot do so here? I was so hoping to stop them from having to go vector = RGSSVector.new vector.create and so on. Is it wise in the documentation to state $Vector.create does this that and those? or should I just say the create method....
This is my create statement that throws an argument error (there is only ONE create statement as of now)
CODE
def create(size=nil) @size = size
if @size == nil @elements = [] return @elements elsif @size != nil @elements = Array.new(@size) #I set the size of the array to be allowed to hold this many elements elsif @size <= 0 raise "Error, vector size cannot be less then or equal to 0 elements in size." return @elements end end
vector = RGSSSVector.new vector.create #upon running I get the argument error
to go back quickly to your "you don't need @size" I hope this code, explains why I need @size. @element.size will check the size of the array, which is fine. But if the user specified I want this array to only have 5 elements and no more....@element.size will not help. If I understand you correctly. Could you maybe post an example of how you would do this with out @size and make sure the user could still pass in a specified size for the vector or pass in nothing and just let the vector grow to what ever size they wish for what ever data they are pushing in? (which can be dangerous)
It really does help, I am better understanding ruby concepts as a whole. Its the syntax I am having issues with and some logic. but these are just growing pains of a new language
__________________________
Games I am working on | Each image is a link to the game
Group: +Gold Member
Posts: 1,199
Type: Scripter
RM Skill: Undisclosed
Well, I cant think of any other way to do this so.. if I were to make this script, and kept in @size, then here's what I would do:
CODE
class RGSSVector attr_reader :elements attr_reader :size def initialize #Does nothing but initialize the class to be used #with global variables such as $vector @elements = [] @size = nil end
def create(size=nil) @size = size if @size == nil return @elements elsif @size <= 0 raise "Error, vector size cannot be less then or equal to 0 elements in size." else @elements = Array.new(size) return @elements end end
def clearVector self.create end
def sizeOfVector if @elements.empty? != true return @elements.size end end
def add(object) if @elements.size == @size and @size != nil @elements[@elements.nitems] = object return @elements else @elements.push(object) return @elements end end
def remove(object) if @elements.include?(object) @elements.delete(object) return @elements end end
def isEmpty? return @elements.empty? end end
if you have any questions, then I'd be happy to answer them. also, if you wanted to save scripters from using $Vectorthing = VectorClass.new you have 2 options, create it as a module instead, OR put $Vectorthing = VectorClass.new in main.
edit- a module is actually FAR more well suited for what it seems you're trying to accomplish with this snippet.
__________________________
Got 30 minutes? Then you've enough time to play this awesome game: - potentially promising project page - thanks holder
Group: +Gold Member
Posts: 1,199
Type: Scripter
RM Skill: Undisclosed
you'd rename class RGSSVector to module RGSSVector; then you'd add self. before every def name(ie: def self.add). then if you wanted to add/remove/whatever to your elements array you'd be like:
CODE
RGSSVector.add(object)
And you wouldnt need the global variable
__________________________
Got 30 minutes? Then you've enough time to play this awesome game: - potentially promising project page - thanks holder
sweet jesus. Like I said I need to read up on modules but this has all very much helped me. As i go back to my last (possibly last) question. the usage or baseage of my code off yours, whats your rules for that if you see my script ever published and are like "hey....I wrote one or more of those methods...." is credit enough or....?
__________________________
Games I am working on | Each image is a link to the game
no no this is purly open source and for free. If I release this script to the comunity I will create a special thanks section for you you in that thread post, blog post, google code section. For now I will credit your name in the documentation
Problem:
if I do:
CODE
module RgssVector
attr_reader :elements protected :elements
@size = nil @elements = []
def self.create(size=nil) @size = size
if size == nil return @elements elsif size != nil return @elements = Array.new(@size) elsif size <= 0 raise "Vector Error -> Size of vector cannot be 0!" end end end
and then do RGSSVector.create(5)
I get:
NameError ocurred while running script Uninitialized constant Game_Interpreter::RGSSVector
I am pretty sure I am doing something wrong
This post has been edited by Adrien.: Sep 1 2011, 04:03 PM
__________________________
Games I am working on | Each image is a link to the game