How to Create a for Statement in Ruby

One of the things that makes Ruby preferable to programmers is that it's an object-based language. Programmers can create a for statement in Ruby as a way to visit every object in a collection of objects and do something specific with them, a process commonly know as to "iterate". You can create a for statement with arrays, Range objects or any objects that include the Enumerable module. There are two ways of doing this: by creating a for statement or by creating the equivalent each statement.

Instructions

  1. Choose an Object

    • 1

      Choose an object you want to iterate over.

    • 2

      Define the desired outcome. What type of object you choose depends on the desired outcome. To count within a specific range you'll need to use a Range object. For example, to count from 1 to 10, use this code: a = (1..10).
      To create a list of items or objects, you will need to use an array object. For example, a list of states would look like this:
      a=%{Maine Michigan Alaska Florida}.

    Create a For Statement in Ruby

    • 3

      Compose the for statement. The basic structure is "for object in collection". In the following example "a" is a Range object. In the statement "for i in a" i is a number within the defined range of 1 to 10:
      a = (1..10)
      for i in a

    • 4

      Pass the for loop a block. The block is the section of code that is to be executed for every element in the collection:
      a = (1..10)
      for i in a
      puts "The number is #{i}"
      puts "Two times the number is #{i*2}
      end

    Create the Equivalent Each Statement

    • 5

      Use the expression: "collection.each do |object|". The each method is used more commonly than the equivalent "for". It's essentially a for loop in disguise. The expression will visit every object in a and assign it to your variable (object) before running the block.

    • 6

      Pass a block to the each command. It must be delimited by either "do...end" or "{...}". Use "do...end" if the block will be more than one line long. Use "{...}" if the block will be all on one line. For example:
      a = (1..10)
      a.each do|i|
      puts "The number is #{i}"
      puts "Two times the number is #{i*2}"
      end

Tips & Warnings

  • The each method of creating a for statement is more commonly used, and tends to have better syntax.

Related Searches:

Comments

You May Also Like

  • How to Handle Exceptions in Ruby

    Write some code that will throw an exception, using the raise statement. In Ruby, the raise statement will stop execution of the...

  • Ruby Gifts for a 40th Anniversary

    Ruby Gifts for a 40th Anniversary. Ruby anniversary celebrants rejoice in 40 years of marital life. Gifts for ruby anniversaries may include...

  • How to Create a While Loop in Ruby

    When you create a while loop in Ruby, you're essentially saying "while X is true, do Y" or even "until X is...

  • How to Create a Multidimensional Array in Ruby

    Though Ruby doesn't provide explicit support for multidimensional arrays, you can implement one yourself if you have a basic knowledge of the...

  • How to Upgrade Ruby Gems

    Ruby is an open source, object oriented programming language. By creating different objects, called Gems, programmers can assemble software packages that are...

  • How to Create a Class in Ruby

    Ruby is a language built for defining classes, as it is first and foremost an object-oriented language. Ruby provides a number of...

  • How to Know if It Is a Genuine Ruby

    Rubies are red. Everyone knows this. But garnets, zircons, spinels, cut glass and synthetic rubies can also be red. You can tell...

  • How to Use Inheritance in Ruby

    Inheritance is the core of object oriented programming. Building class structures makes smaller programs easy, and large programs possible. In Ruby, inheritance...

  • How Are Rubies Processed?

    A ruby is a red gemstone that receives its vibrant coloration from aluminum oxide and chrome. It is an extremely hard stone,...

  • Benefits of Yellow Sapphire

    Yellow sapphires and their rich, citron tone can make a bold, one-of-a-kind fashion statement. But this gemstone is also associated with numerous...

  • How to Create a Billing Statement in Microsoft Excel

    If your business deals with multiple items and sales to regular, repeat customers, using a billing statement is the most efficient way...

  • How to Create an Array in Ruby

    There are three primary ways to create an array in Ruby: the array literal, building the array and returning an array. An...

  • What Is the Converse of a Conditional Statement?

    In traditional logic, we use capital letters as placeholders for "simple statements," which are simple declarations of fact. For example, "P" could...

  • What Are the Uses for the Gemstone Ruby?

    Rubies have long been valued as beautiful and rare gemstones. The color of rubies ranges all along the spectrum of red from...

  • How to Create a Do While Loop in Perl

    A do while loop is almost exactly like a while loop, with one major difference: the code in the loop body is...

  • About Endothil CR

    Endothil-CR is a nutrition supplement created by David Summers as a self and healthful alternative to steroids in building muscles. The benefits...

  • Perennial Flowers & Ruby Slippers

    The phrase "ruby slippers" immediately conjures an image of the iconic shoes worn by Judy Garland in the film production of "The...

  • How to Input a File in Ruby

    Use one of a number of methods for reading and writing the file. To read each line in order, you can use...

  • How to Use Case Tutorials

    The Switch-Case statement common to most programming languages provides an easier and more readable way to handle decisions when there are a...

  • How to Create a While Loop in C

    Create a while loop any time your C program needs to iterate over values or variables. With a few simple precautions, while...

Related Ads

Featured