Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

for i+1 in 0..3 in ruby??

Zeriab

Sponsor

To answer the topic:
for i in -1..2

As for the contents. Why would want to iterate from 2 to 2?
I mean you can do for i in 2..2, but why not just i = 2?

What are you trying to do?
 
Sorry for my english, what i'm trying to do is to emulate C for function, where you can make an iteration with any modifier you want, i want to iterate something like this:
for 2 in 0..8
for example, and only make 2,4,6,8, but with the "i" because i have to use it with an array.
This is what im doing now, it works, but maybe there are something better:

for i in 0...pos.size/2
matriz_enemiga[pos[i*2],pos[i*2+1]] = -(enemies.index+1)
enemies.posicion[0] = pos[i*2]
enemies.posicion[1] = pos[i*2+1]

its an iteration over an 16 lenght array, in C it will be more easy:
for (i=i+1; i<=16; i=i++)
{
matriz_enemiga[pos,pos[i+1]] = -(enemies[i/2].index+1)
}
2,4,6,8...etc
pos[0], pos.[1]// pos[2],pos[3]//...etc


Also another thing i don't like is that you can't modify i variable in the same bucle.

for i in 0..8
if battler.exist?
i*2
end
end

Its just an example...this dont work in ruby, iterations are totally linear and static or i'm missing something?
 
Wait, you're trying to iterate all even numbers? 2, 4, 6, 8?

You could do...

Code:
[2, 4, 6, 8].each {|n| something += n}

...or...

Code:
for i in 0..8

  next if (i % 2 == 1)

  something += n

end

...The second example, when using (number) % 2 it'll return 0 if even and 1 if odd. Also, you're doing this wrong...

Code:
for i in 0..8

  if battler.exist?

    i * 2 # Should be (i *= 2) if you're trying to actually change the number

  end

end

Just doing (number) + (number), (number) - (number), (number) * (number) won't change the number, it just checks the operator difference with the number, you'd do +=, -=, *=, or /= to actually change the number.

Code:
i = 5

i + 1 #=> Returns 6, but [b]i[/b]'s value is still 5

i - 1 #=> Returns 4, but [b]i[/b]'s value is still 5

i += 1 #=> Returns 6, and [b]i[/b]'s value has now been changed to 6

i -= 1 #=> Returns 4, and [b]i[/b]'s value has now been changed to 4

Hope this has helped you some!
 
Thanks.
The second example is what i need, but the truth is that i knew i could use next, but i was wondering if there are a better way.

Mmm... I really like the C for bucle then. Strange that they didn't put this functionality directly in ruby, no? Like the ++ or --. Because if you only want to skip one iteration this is ok, but if you want to make more complex things then it became even more hard.

About that what was wrong, it was only an example with a mistake. I know that. But the problem is that you can't modify i in the bucle, it will be another variable.
 
You can use also:
Code:
0.step(8, 2) do |i|

  Things here.

end
Tracing i, it would be for each iteration: 0, 2, 4, 6, 8.

The step method starts from the first number and ends when the value passed to i is greater than 8 in this case, and i will increment in for 2 each iteration.
 

Zeriab

Sponsor

You can always modified the variable in the for-loop.
Let's for example look at this:
[rgss]for i in 0..4  # for variable in collection
  i *= 2
  # Do something
end
[/rgss]

The .each method is on the used for yielding (retrieving) successive members of the collection.
0..4 creates a Range object. It's on this object the .each method is used.
We can freely change i because it will be set to a new variable in the next iteration. (Unless the same object is present in the collection more than once, which is not the case with ranges)

Since we know this we can make our own objects. Look at this class I have just made:
[rgss]class Iterator
  ##
  # Includes the Enumerable module to provide a number of lovely functions
  # Note: The functions provided are not safe to use when dealing with
  # infinite collections. (The game can easily freeze)
  #
  include Enumerable
 
  ##
  # Object Initialization
  #   min : Minimum value.
  #   max : Maximum value.
  #   start : The starting value of the iterator. (Default is min)
  #   &block : An optional block which is called for retrieving successors.
  #            Default is incrementing the value by 1. (Same as Range)
  #
  def initialize(min, max, start = min, &block)
    @min = min
    @max = max
    @start = start
    @block = (block == nil ? Proc.new {|n| n+1} : block)
  end
 
  ##
  # Yields successive members until a member is out of range.
  # Assumes that the start value and each successor can be compared to the
  # mininum and maximum values.
  # Note: The collection if the start value is out of range.
  #
  def each
    # Sets the current value to the start value
    value = @start
    # Check that the value is in range
    while value >= @min && value <= @max
      # Let's give the value
      yield value
      # Retrieve the successor
      value = @block.call(value)
    end
  end
end
[/rgss]

I have included the Enumerable module since it provides a number of functions which are often nice to have available.
The idea behind this iterator is to specify a minimum value, maximum value and a Proc for calculating successive members. You can optionally specify the starting value. (Default is the minimum value)
I think how it can be used will be more obvious with an example:
[rgss]arr = []
for i in Iterator.new(0,8) {|n| n+2}
  arr << i
end
 
p *arr
[/rgss]
Guess what it does :P

If we want it to go the other way we can do that by specifying the starting value:
[rgss]arr = []
for i in Iterator.new(0,8,8) {|n| n-2}
  arr << i
end
 
p *arr
[/rgss]
So now we are counting down.

The starting value can be different from the minimum and maximum values:
[rgss]arr = []
for i in Iterator.new(-8,8,0) {|n| n > 0 ? (n*-1)-1 : (n*-1)+1}
  arr << i
end
 
p *arr
[/rgss]

Yes, we here have a Turing complete language available for describing successors.


If we want we could just make an .each method on an object. It really depends on what you prefer and what's the easiest to do in your situation. Example where a method is added to a string:
[rgss]foo = "Oh noes"
def foo.each
  a = 7
  b = 2
  x = 0
  20.times do
    x = a + b*x
    yield x
  end
end
 
arr = []
for i in foo
  arr << i
end
 
p *arr
[/rgss]

*hugs*
- Zeriab
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top