Skip to main content
added 239 characters in body
Source Link
cris.h
  • 233
  • 2
  • 7

next is similar to the continue keyword in the c family of languages, except in ruby it makes an iterator move to the next iteration. Since blocks always have some sort of return value you can choose to pass one as an argument to next.

next is typically used in cases like iterating through a list of files and taking action (or not) depending on the filename.

next can take a value, which will be the value returned for the current iteration of the block.

  sizes = [0,1,2,3,4].map do |n|
    next("big") if n > 2
    puts "Small number detected!"
    "small"
  end

  p sizes

Output:

  Small number detected!
  Small number detected!
  Small number detected!
  ["small", "small", "small", "big", "big"]

from http://ruby-doc.org/docs/keywords/1.9/

next is typically used in cases like iterating through a list of files and taking action (or not) depending on the filename.

next can take a value, which will be the value returned for the current iteration of the block.

  sizes = [0,1,2,3,4].map do |n|
    next("big") if n > 2
    puts "Small number detected!"
    "small"
  end

  p sizes

Output:

  Small number detected!
  Small number detected!
  Small number detected!
  ["small", "small", "small", "big", "big"]

from http://ruby-doc.org/docs/keywords/1.9/

next is similar to the continue keyword in the c family of languages, except in ruby it makes an iterator move to the next iteration. Since blocks always have some sort of return value you can choose to pass one as an argument to next.

next is typically used in cases like iterating through a list of files and taking action (or not) depending on the filename.

next can take a value, which will be the value returned for the current iteration of the block.

  sizes = [0,1,2,3,4].map do |n|
    next("big") if n > 2
    puts "Small number detected!"
    "small"
  end

  p sizes

Output:

  Small number detected!
  Small number detected!
  Small number detected!
  ["small", "small", "small", "big", "big"]

from http://ruby-doc.org/docs/keywords/1.9/

Source Link
cris.h
  • 233
  • 2
  • 7

next is typically used in cases like iterating through a list of files and taking action (or not) depending on the filename.

next can take a value, which will be the value returned for the current iteration of the block.

  sizes = [0,1,2,3,4].map do |n|
    next("big") if n > 2
    puts "Small number detected!"
    "small"
  end

  p sizes

Output:

  Small number detected!
  Small number detected!
  Small number detected!
  ["small", "small", "small", "big", "big"]

from http://ruby-doc.org/docs/keywords/1.9/