Ruby/RGSSx questions that don't deserve their own thread

Spoiler
If you are not sure how to get the desired data for a database item, here is what you can do:Make an event. In the event, just make a script call command like this:

p $data_object[id]You can do this in a script in the script editor also.

Examples: p $data_skills[12], p $data_actors[5], p $data_weapons[34], etc.

This will print everything out about an object of your choice.

You will see many "@var_name" texts in the console and their value.

Those are the datas of the object you just printed to the console.

If you enter $data_object[id].var_name, you will get that data returned.

Some of them are "sub-datas", just like the element of the skills, but those can be identified quickly as well. You will see the same "@var_name" displayed, but after the name of it, you will see something like "<RPG:😮bjName...". If you see that, you will first need to enter the first @var_name which was before the <RPG:😮bjName... text and after that, enter the second @var_name inside that RPG:😮ject.

This is how I find specific data for an object. Much quicker than searching for it in the help files which can't even show you the new, custom effects from custom scripts. It also shows the value of each data, so even if I don't know what some data means, I can still assume it if I see their values.

Sorry if I failed to explain this in an understandable way, I kinda suck at explaining these scripting things. 😛
Wow, this might be very helpfull for developing and finding things, thank you!! =D

and I totally understand what you want to explain, no worries! ^^ 

again, thank you for this tip!
okay, another question... sorry to ask so much =/

lets say I have an array with 20 elements and need every 5th value because I need the 4 values at index 0, 5, 10 and 15 .

I thought I can do this with a for loop like this:

for i in array do something i += 5endbut when I test that... it reads out every value, so the for loop iterate over every value in array and counts i intern.

how can I achieve that I read every nth value??
Code:
[0,5,10,15].each {|index| do something with array[index]}
is there a way to "do something" in more than one line? or must it be in one line within { } ?

nevermind, I think its like this:

[0,5,10,15].each do |i| do somethingendcorrect me if I'm wrong...
[0,5,10,15].each do |index|


 something with array[index]


end
again, thank you all so much for helping, this community rocks! =D
If you don't want to be limited in any way (meaning infinite long of arrays, you can do something like this:

array.each_index do |i| if i == 0 || (i+1) % 5 == 0 do something with array endendThis would select every 5th element in your array starting from the first element, regardless how long your array is.NOTE:

You wrote that you need every fifth element, but your example stated the numbers 0, 5, 10 and 15.

That is not every fifth value.

The index of the elements starts from 0, so the index of 5 would be 6 elements away from the first index.

Every fifth element in an array would look like: 0, 4, 9, 14, 19, and so on.

Or if you leave out 0 in a conditional check: 1, 5, 10, 15, 20, and so on.

It could be a typo only, but the given examples could be wrong above if it was a typo on your side.
Sometimes we work with a data that we didn't know the exact value or how many the elements of the array has.
This code below will do the same thing like what you want, but this one you don't need to know how many the elements of the array has, but will always print the data in element 0 and the next nth element after "that"("that" = 0). I give example for 20 elements below, but try to change it into different elements, and it will still printing the 0 element and every nth element after "that"("that" = 0).

ary = ["a","b","c","d","e","f",'g','h','i','j','k','l','m','n','o','p','q','r', 's', 't','u', 'v', 'w', 'x',]starter_index = 0 # your starter element to be printednth = 5 # the nth range element between each data to be printed (ary.size/nth + 1).times do |i|    p ary[starter_index+ nth*i] unless ary[starter_index + nth*i].nil? endMaybe there's a better way to do this but I can't find out how.

And Sixth has a point, you stated for every 5th(yet from what parameter?) if 5th of array then so it should be 0, 4, 9, 14, 19, like Sixth said, but if 5th from the starter element to be printed you are right 0,5,10,15 because 5th after 0 is 1,2,3,4,5. My code used for the later, which is 5th from the starter(which is 0 or whatever you set in the starter_index variable).

@Sixth` so the index of 5 would be 6 elements away from the first index.
That is not correct, first index is 0, so from 0 to 5 is 5. But index of 5 is yes the 6th element of the array
@sixth:

yes, I notice by myself that it have to be 0, 4, 9, 14 to get 1, 5, 10, 15 and so on. XD

and thank you, this works even better! =D awesome

how can I be so dumb and didn't think of this? my math is not the very best.. =(

@BoluBolu:

thanks, but I tested the method from sixth and it works really fine for me! =)
Yes Sixth method is works really fine ^__^
I just interested in your topic and create that code for fun, yet mine code let you change the nth range(not limited with only 5th) and starter element to be printed(not limited from index 0) easily.
Either way you use I hope you gain more knowledge involving array =)
oh for f**ks sake, I made a mistake in my head with values and index and bla, my head is spinnin...  

I tested your method @BoluBolu and it delivers the now right results for me! gosh... I use this one now

and I think I need to smoke a cigarette and clear my minds...  ._.
Please, for the sake of all humanity, change this line...

ary = ["a","b","c","d","e","f",'g','h','i','j','k','l','m','n','o','p','q','r', 's', 't','u', 'v', 'w', 'x',]
to this...

ary = ("a".."x").to_a 🙂
Two methods that could have also been used here:

Numeric#step
This method behaves similar to what BoluBolu and Sixth already did. It iterates from a starting number to a target with a specified step size:
0.step(10, 2) { |i| p i }
# => 0  2  4  6  8  10
# 0: starting number, 10: target (inclusive), 2: step size
 
Used for your example:
array = [*'A'..'Z']
0.step(array.length - 1, 5) { |i| p array }

# => A  F  K  P  U  Z
 
Enumerable#each_slice
Instead of iterating over each element of an enumerable object (as arrays), this method cuts the array into slices of n consecutive elements and iterates over the slices:
array = [*'A'..'N']
array.each_slice(5) { |set| p set }
# => [A, B, C, D, E]  [F, G, H, I, J]  [K, L, M, N]

array.each_slice(5) { |set| p set[0] }

# => A  F  K

array.each_slice(5) { |l1, l2, l3, l4, l5| p l1 }

# => A  F  K
@Sixth` so the index of 5 would be 6 elements away from the first index.


That is not correct, first index is 0, so from 0 to 5 is 5. But index of 5 is yes the 6th element of the array
Yeah, that is what I meant.
Blasted arrays and their starting points. 😀


@Dekita


Wow, didn't know we could do that with letters too. o.o


Good stuff!


@Another Fen


I guess you found the easiest method for it. That is awesome!


I learn a lot today! 😀
@Another Fen: do I see this right, if I use step for my array, I have only access to the results and not the rest of it?
[*'A'..'Z']^ That is beautiful. Never thought of using splat on a range. Just, epic.
@Dekita
lololol for the sake of all humanity(I can't stop laughing, not lying) xD, hehe but I did it so fear616 won't confused(will he/she?, lol), and yes that splat from Another Fen is epic I agree, I never know about that to be honest.

@Fen
as expected from the experienced one that method is really nice.

@Sixth
you know sometimes I get annoyed with array too  >__<

EDIT : Oh yes, I also remember I read somewhere that using #length is better than #size, I just forgot why xD


 
EDIT : Oh yes, I also remember I read somewhere that using #length is better than #size, I just forgot why xD
http://batsov.com/articles/2014/02/17/the-elements-of-style-in-ruby-number-13-length-vs-size-vs-count/
@BoluBolu: he 😉  lol

I ran into a new problem with those god damn arrays...

I made my own class in my script to store some data.

later in that script I made an array to store objects from my class in this array... something like this:

array = []object = MyClass.newobject.init(params...)array.push(object)but I got an error which says, array don't know a method named push.

can it be, that I can not put objects of own classes into arrays?

I thought in the help file says obj, which is for me, every object...
Objects can easily be placed in arrays. All ruby objects are held in a similar regard, whether its an instance of a class or a hash.

  array = []  array.push(Object.new)  p array
There must be something else happening. Perhaps you overwrite the [] method in your class or something?

Original thread: https://forums.rpgmakerweb.com/threads/ruby-rgssx-questions-that-dont-deserve-their-own-thread.150/