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

    Posted on September 17th, 2009 RubyLove 1 comment

    The crc32() function in PHP generates the cyclic redundancy checksum (CRC) polynomial of 32-bit lengths of a string, and returns it as an integer.

    PHP

    $checksum = crc32('hello world');
    echo $checksum;
    // => 222957957

    Ruby

    require 'zlib';
    puts Zlib.crc32('hello world');
    # => 222957957
  • is_string

    Posted on August 12th, 2009 RubyLove No comments

    The is_string() function allows you to check if a particular variable is a string (type).

    PHP

    $string1 = 'Hello World';
    var_dump( is_string($string1) );
    // => true

    Ruby

    string1 = 'Hello World';
    puts string1.is_a?(String);
    # => true
  • ucwords

    Posted on July 13th, 2009 RubyLove 1 comment

    The ucwords() function turns the first character of each word in a string to upper-case, if the first character is alphabetic.

    PHP

    echo ucwords("ruby is easy.");
    => // Ruby Is Easy.

    Ruby doesn’t have a function which can capitalize all the words in a string - so to accomplish this it’s a little bit harder. You need to split the string into words, then capitalize the first character of each word, and then finally join all the words back into a string.

    Ruby

    puts "ruby is easy.".split(' ').select {|w| w.capitalize! || w }.join(' ');
    => # Ruby Is Easy.
  • lcfirst

    Posted on July 10th, 2009 RubyLove 2 comments

    The lcfirst() function returns a string, with the first character in lower case - only if the first character is alphabetic.

    PHP

    echo lcfirst("Java is OK.");
    => // java is OK.

    Ruby

    my_string = "Java is OK.";
     
    puts my_string[0,1].downcase + my_string[1..-1];
    => # java is OK.
  • ucfirst

    Posted on July 7th, 2009 RubyLove No comments

    The ucfirst function returns a string, with the first character capitalized - only if the first character is alphabetic.

    PHP

    echo ucfirst("ruby is great!");
    => // Ruby is great!

    Ruby

    puts "ruby is great!".capitalize
    => # Ruby is great!
  • nl2br

    Posted on July 4th, 2009 RubyLove 2 comments

    The nl2br() function inserts HTML <br> tags before any new lines in a string.

    PHP

    echo nl2br("foo has a\n bar");
    /* 
    foo has a<br>
    bar
    */

    Ruby

    p "foo has a\n bar".gsub("\n", "<br>\n")
    # foo has a<br>
    # bar
  • chop

    Posted on July 1st, 2009 RubyLove No comments

    The chop() function is only an alias of rtrim, which strips whitespace (or other characters) from the end of a string.

    For further information about how chop() can be used, see rtrim().

  • unpack

    Posted on June 28th, 2009 RubyLove No comments

    The unpack function decodes a binary string into an array according to the format given as a parameter.

    PHP

    $binary_string = pack("nvc*", 0x1234, 0x5678, 65, 66);
    $array = unpack("nvc*", $binary_string);

    Ruby

    a = [0x1234, 0x5678, 65, 66];
     
    binary_string = a.pack("nvc*");
     
    my_array = binary_string.unpack('nvc*');
  • pack

    Posted on June 25th, 2009 RubyLove No comments

    The pack function packs the given arguments into a binary string according to format provided as a parameter.

    PHP

    $binary_string = pack("nvc*", 0x1234, 0x5678, 65, 66);

    Ruby

    a = [0x1234, 0x5678, 65, 66];
     
    binary_string = a.pack("nvc*");
  • bin2hex

    Posted on June 22nd, 2009 RubyLove No comments

    The bin2hex function converts any string into an ASCII string containing the hexadecimal represenation of the original string. As the name of the function suggests, bin2hex is particularly useful to make a human-readable representation of binary strings.

    PHP

    $binary_string = pack("nvc*", 0x1234, 0x5678, 65, 66);
     
    echo bin2hex($binary_string);
     
    => // 123478564142

    Ruby doesn’t have a function equivalent to PHP’s bin2hex, but Bytes::unpack with a format string of H* will achieve the same functionality.

    Ruby

    a = [0x1234, 0x5678, 65, 66];
     
    binary_string = a.pack("nvc*");
     
    puts binary_string.unpack('H*');
    => # 123478564142