Convert PHP code into Ruby!
RSS icon Email icon Home icon
  • end

    Posted on September 2nd, 2009 RubyLove No comments

    The end() function in PHP moves the internal array pointer to the last element in the array. However in Ruby, there is no internal array pointer - as such Ruby doesn’t have an equivalent to PHP’s end() function. However Ruby does have the Array::last() method which returns the last element of an array.

    PHP

    $programming = array('java', 'ruby', 'python', 'php');
    $language = end($programming);
    print_r($language);
    // => php

    Ruby

    programming = ['java', 'ruby', 'python', 'php'];
    language = programming.last;
    puts language;
    => # php