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

    Posted on May 29th, 2009 RubyLove No comments

    The explode function takes 2 parameters - the first is the delimiter, and the second is the string to be exploded. It returns an array of strings, each of which is a substring of the original, formed by splitting the original string on boundaries formed by the string delimiter.

    PHP

    $my_string = 'perl, python, java';
    $array = explode(', ', $my_string);
    var_dump($a);
    /*
    Array (
    	[0] => perl
    	[1] => python
    	[2] => java
    )
    */

    Ruby

    my_string = 'perl, python, java';
    puts my_string.split(', ');
    # => ["perl", "python", "java"]
  • shuffle

    Posted on May 26th, 2009 RubyLove No comments

    Randomizes the order of the elements in an array.

    PHP

    $a = array('a', 'b', 'c', 'd');
    shuffle($a);
    print_r($a);
    /*
    Array (
    	[0] => d
    	[1] => b
    	[2] => c
    	[3] => a
    )
    */

    Ruby

    a = [ "a", "b", "c", "d" ];
    puts a.sort_by{ rand };
    # => ["d", "b", "c", "a"]
  • str_pad

    Posted on May 23rd, 2009 RubyLove No comments

    str_pad allows a string to be added to a particular length using another string.

    PHP

    echo str_pad('Hi', 5);
    // => 'Hi   '
     
    echo str_pad('Hi', 5, '+');
    // => 'Hi+++'
     
    echo str_pad('78', 5, '0', STR_PAD_LEFT);
    // => '00078'

    Instead of padding a string, in Ruby we justify a string to the left or right.

    Ruby

    puts "Hi".ljust(5);
    # => 'Hi   '
     
    puts "Hi".ljust(5, '+');
    # => 'Hi+++'
     
    puts "78".rjust(5, '0');
    # => '00078'

    To pad both sides of a string, we need to use a combination of ljust and rjust.

    PHP

    echo str_pad('Special', 11, '*', STR_PAD_BOTH);
    // => '**Special**'

    Ruby

    puts "Special".ljust(9).rjust(11);
    # => '**Special**'
  • time

    Posted on May 20th, 2009 RubyLove No comments

    Returns the current Unix timestamp (epoch).

    PHP

    echo time();
    // => 1242744141

    Ruby

    puts Time.now.to_i;
    # => 1242744141
  • microtime

    Posted on May 17th, 2009 RubyLove 1 comment

    Returns the current Unix timestamp (epoch) with microseconds.

    PHP

    echo microtime();
    // => 0.63928794 1242744141

    Ruby

    epoch_mirco = Time.now.to_f;
    epoch_full = Time.now.to_i;
     
    epoch_fraction = epoch_mirco - epoch_full;
     
    puts epoch_fraction.to_s + ' ' + epoch_full.to_s;
    # => 0.639287948608398 1242744141
  • md5_file

    Posted on May 14th, 2009 RubyLove No comments

    The md5_file() function calculates the md5 hash of a file.

    PHP

    $my_file = '/home/ali/article.txt';
     
    echo md5_file($my_file);
     
    // => 745d3dfb68af1b1384aea0125aae5c3f

    MD5 encryption can be done using the Digest::MD5 class in Ruby, but to generate a hash for a file, we need to read it’s contents into a variable (string) first.

    Ruby

    require 'digest/md5';
     
    my_string = File.read('/home/ali/article.txt');
     
    print Digest::MD5.hexdigest(my_string);
     
    # => 745d3dfb68af1b1384aea0125aae5c3f
  • sha1_file

    Posted on May 11th, 2009 RubyLove No comments

    The sha1_file() function calculates the sha1 hash of a file.

    PHP

    $my_file = '/home/ali/article.txt';
     
    echo sha1_file($my_file);
     
    // => bef6b2b56f70eb6f2c250e00c855f4c0120832aa

    Sha1 encryption can be done using the Digest::SHA1 class in Ruby, but to generate a hash for a file, we need to read it’s contents into a variable (string) first.

    Ruby

    require 'digest/sha1';
     
    my_string = File.read('/home/ali/article.txt');
     
    print Digest::SHA1.hexdigest(my_string);
     
    # => bef6b2b56f70eb6f2c250e00c855f4c0120832aa
  • sha1

    Posted on May 8th, 2009 RubyLove No comments

    The sha1() function calculates the sha1 hash of a string.

    PHP

    echo sha1('apple');
    // => d0be2dc421be4fcd0172e5afceea3970e2f3d940

    Sha1 encryption can be done using the Digest::SHA1 class in Ruby. To use this class, we need to require the Digest library first…

    Ruby

    require 'digest/sha1';
     
    print Digest::SHA1.hexdigest('apple');
     
    # => d0be2dc421be4fcd0172e5afceea3970e2f3d940
  • md5

    Posted on May 5th, 2009 RubyLove No comments

    The md5() function calculates the md5 hash of a string.

    PHP

    echo md5('apple');
    // => 1f3870be274f6c49b3e31a0c6728957f

    MD5 encryption can be done using the Digest library in Ruby. To use the Digest library, just require it…

    Ruby

    require 'digest/md5';
     
    print Digest::MD5.hexdigest('apple');
     
    # => 1f3870be274f6c49b3e31a0c6728957f
  • error_log

    Posted on May 2nd, 2009 RubyLove No comments

    The error_log() function sends an error message to the web server’s error log, a TCP port or to a file (usually the web server’s error log).

    PHP

    error_log('My error message');
    // => My error message

    error_log() is probably one of the most common and convenient ways to debug PHP scripts, without having to install a full PHP debugger. As such, it is very popular with developers, often logging variables part way through a script, to see if the values are what they expect. Although Ruby doesn’t provide such a convenient global function, it does provide the Logger class which is much more powerful.

    Ruby

    require('Logger');
    Logger.new('/var/log/httpd/error.log').error('My error message');
    # => ERROR -- : My error message