This Season
 

How to Compare Strings in Ruby

Ruby is an extremely powerful programming/scripting language. With powerful capabilities and an efficient syntax, many things are possible with Ruby. This article explains how to compare the text that is stored in string variables in Ruby.

Related Searches:
    Difficulty:
    Easy

    Instructions

    Things You'll Need

    • Ruby interpreter
    • Keyboard
      • 1

        Open up any text editor, such as notepad or SciTE. We are going to compare two strings using simple equality comparison.

      • 2

        Type in the following code:

        s = "This is my string."
        s2 = "This is my other string."

        puts s.eql?(s2)

      • 3

        The first thing we do above is assign a value to the string 's' and then a different value to the string s2. The first line beginning with puts will print out "false" because it runs a basic equality test on the two strings. These strings are not equal, so it prints false.

      • 4

        Save your program as 'myprogram.rb' and open up a command line prompt. Change to the directory where you saved your program and type 'ruby myprogram.rb' to see the output.

    Related Searches

    Read Next:

    Comments

    You May Also Like

    • How to Handle Exceptions in Ruby

      Occasionally, a method will encounter an error, at which point it will fail and tell you about it via the a mechanism...

    • What is the Komodo Ruby Debugger?

      Komodo is an Integrated Development Environment (IDE) for PERL, Python and Ruby developers. An IDE is designed to provide a single unified...

    • How to Write a Simple Program in Ruby

      Ruby is an extremely powerful programming/scripting language. With powerful capabilities and an efficient syntax, many things are possible with Ruby. This article...

    • How to Find a Substring in Java

      In Java, a string is an array of characters (letters, numbers or symbols). A substring of a string is simply a collection...

    • Instructions for Beading a Barefoot Sandal

      Barefoot sandals are like necklaces for your feet. They consist of a string of beads with a loop on the end. The...

    • How to Use Ruby Scripts in Sketchup

      Google SketchUp is a 3D Modelling program that can create both simple and sophisticated models of buildings. It is used most heavily...

    • 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 to Pick a Ruby

      Are you thinking of buying ruby jewelry or loose gems? Here are some practical ways to select the best ruby for your...

    • How to Grade Rubies

      Grading rubies is a centuries-old profession and a necessity for dealers and others looking to understand this gem's beauty. The red intensity...

    Follow eHow

    Related Ads