Chaos Project

RPG Maker => RPG Maker Scripts => Script Troubleshooting => Topic started by: diagostimo on July 01, 2012, 01:14:48 am

Title: quick question
Post by: diagostimo on July 01, 2012, 01:14:48 am
hey, i was wondering if it is possible to count how many objects are inside a self object in a module, ill give an example of what i mean:

module Config
  def self.whatever(id)
    return case id
   
    when 1 then []
    when 2 then []
    when 3 then []
  end
end

so what i was wanting to do is count how many objects are inside self.whatever(id), so in this case i would want it to return 3
i tryed it using a for statment like this:

for i in Config.whatever.size

then using i as the value but i just get an error with that  :huh:
Title: Re: quick question
Post by: ForeverZer0 on July 01, 2012, 01:40:48 am
There is practically no possible reason you would need to count that. I doubt that you are generating the method dynamically at run-time and do not know the count (in which case you could still track it). Just count how many "cases" you wrote.

Sure there is possible ways to psuedo-count, such as only using consecutive numbers, and iterating a range calling the method until it returns nil, but that is a god-awful practice that you should never, under an circumstances get in any habit of actually trying to do.

To answer your question, no, Ruby does not offer any way of counting the "cases" in a method, mainly because it does not make sense and is pointless. Just out of curiosity, what exactly do you need that for? I guarantee that you simply need to change the way you are doing something if your code requires such a thing.
Title: Re: quick question
Post by: diagostimo on July 01, 2012, 02:13:49 am
ok, the reason i needed to count how many cases where in the method is because im currently creating a custom window that allows the user to smelt bars in order for crafting purposes, each case would represent each bar and contain restrictions like smith lvl, items required etc, i wanted to count how many cases there where so that the index could auto generate depending on how many bars are setup, what would be the most practical way of doing this?
Title: Re: quick question
Post by: ForeverZer0 on July 01, 2012, 02:33:03 am
Its possible I am misunderstanding something here.
You want to know how many "case" blocks there are within the switch statement? If that is the case, then seriously, count how many you write. There is no reason to have the need to have a program do that. The number of cases you have written is not going to change at runtime.

If this is for a configuration the end-user is setting up, where you don't know the number, you may want to use a hash or array instead. You can count the number of items in a them, and it would provide the same functionality. Here would be an example based from what you posted above:

@whatever = []
@whatever[0] = []
@whatever[1] = []
@whatever[2] = []

Title: Re: quick question
Post by: diagostimo on July 01, 2012, 02:49:00 am
ye im just been lazy :P i figured if there was a way of counting the cases then i might aswell include it to void having to change the value every time i add to it, but thanks for the quick reponse :)