One of the most integral parts to programming is the basic IF statement or the conditional Statement. Ruby like most if not all languages uses the if keyword to define an IF statement. However it defers from other languages as you can use the if keyword as a statement or a modifier. In addition Ruby has the unless keyword to perform the else if logic without testing for a negative result.

The basic IF ELSE statement

The IF statement in Ruby is the same as Java or C++ with the following differences like methods you don’t need to utilize parenthesis and ELSE IF has been shortened into the elsif keyword. Lets start by looking at a basic example:

if var < 10 
    puts "The variable is less than ten."
elsif var >= 10 and var < 25
    puts "The variable is less than 25 but greater or equal to ten."
else
    puts "The variable is greater then or equal to 25."
end

The IF modifier

In addition to defining IF statements ruby lets you use conditionals as a modifier. For example you can write your command and append the if modifier so that the command only executes if the test conditional is true.

puts "The variable is less than 0" IF var < 0

In the example we have written our code for outputting our string but modified it by saying only output it IF our condition is met.

The UNLESS keyword

Ruby in addition to implementing the basic IF ELSE conditional has added in the unless keyword to their language. This keyword is used to say execute the following code if and only if this condition is not met.

if var < 0
    puts "The variable is greater than 0"
else
    puts "The variable is less than 0"
end

The unless keyword is really only a convenience method for those cases your are testing for false, so instead of negating the conditional statement you can simply use unless. Like the if keyword it can also be used as a modifier.

puts "The variable is less than 0" unless var >= 0

Not sure why it was determined that the unless keyword exists in Ruby as everything it can be used for can be completed with an if statement. However from what I have seen Ruby tries to let developers do whatever they want without imposing any restrictions so i guess writing unless is easier then a negated conditional for some.

, , , , , , ,

For some reason I have the utmost trouble remembering how to do a single line or inline if else statement. This article is mainly for myself so hopefully I will remember the next time I want to do an inline if else statement.

The usual format for performing an if else statement is:

if( $val1 > $val2 ) {
    // code executed if true
} else {
    // code executed if false
}

If you are trying to set a variable to a value depending on a condition then it is often the case that an inline if else statement will be sufficient and easier to read. This is done by the use of ?. The full format is (condition) ? (true output) : (false output) or as an example:

$val = ($var1 > $var2) ? $var1 : $var2;
, , , , ,

JavaServer Pages Standard Tag Library or JSTL for short is a library that contains a set of core functionality including iterations and conditionals.  The tag I find myself most often using is the forEach tag to output a collection of data in a consistent format. By use of the optional status object and the if tag you can even put logic in that executes only if the item being iterated is first or last in the collection.

Getting Started

To utilize JSTL you first need to download the appropriate jar file and import it into you project in the web library. Once in your project you need to import the tag definition into your page using the following code.

<%@ taglib prefix="c" uri="http://java/sun.com/jsp/jstl/core %>

Basic ForEach Tag Usage

Once the tag library is imported we can use it by specifying 2 attributes var and items. VAR is used to denote the reference that the current item being iterated in the collection can be accessed by, while ITEMS specifies the collection to be iterated upon. Lets assume me have a collection of User objects that each have a firstName, lastName, and email property.  We could iterate over them with the following code.

<c:forEach var="user" items="${users}">
</c:forEach>

The above code is functional but doesn’t output any data lets expand upon it by having it export rows of a table with the three properties each in their own cell.

<table>
  <tr>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Email</th>
  </tr>
  <c:forEach var="user" items="${users}">
    <tr>
      <td>${user.firstName}</td>
      <td>${user.lastName}</td>
      <td>${user.email}</td></tr>
  </c:forEach>
</table>

Advanced Usage: The varStatus Attribute

In addition to iteration over the collection the ForEach tag provides a status object that can be used to obtain information about where you are in the collection.

<c:forEach var "user" items="${users}" varStatus="status">
</c:forEach>
  • first and last property
    The status object contain a first and last property that returns a boolean set to true or false if the current item being iterated over is the first or last object in the collection. You can utilize the if tag to have code execute if it is one of these conditions.

    <c:if test="${status.last}">
      // code
    </c:if>
  • index property
    The index property gives you the current index of the object within the collection this value can be used to output different classes on odd and even rows for example, in addition to other uses.

    <table>
      <tr>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Email</th>
      </tr>
      <c:forEach var="user" items="${users}" varStatus="status">
        <tr class="${status.index % 2 == 0 ? 'even' : 'odd'}">
          <td>${user.firstName}</td>
          <td>${user.lastName}</td>
          <td>${user.email}</td>
        </tr>
      </c:forEach>
    </table>

Resources

, ,