Raven vs PHP vs Ruby

String Operations

PHP

list ($a, $b, $c) = explode(':', 'a:b:c');

Ruby

a, b, c = 'a:b:c'.split(':')

Raven

'a:b:c' ':' split into $a, $b, $c

PHP

$text = join("\t", explode(',', $csv_line));

Ruby

text = csv_line.split(',').join("\t")

Raven

$csv_line ',' split "\t" join as $text

PHP

$chars = strlen($string);

Ruby

chars = string.size

Raven

$string length as $chars

PHP

$text = str_replace('pattern', 'repl', $data);

Ruby

text = data.gsub('pattern', 'repl')

Raven

$data 'repl' 'pattern' replace as $text

PHP

$today = date($pattern, time());

Ruby

today = Time.new.strftime(pattern)

Raven

time int $pattern date as $today

Arrays, Lists and Hashes

Numeric Arrays vs Lists

PHP

$test = array(1, 2, 3);

Ruby

test = [1, 2, 3]

Raven

[1 2 3] as $test

PHP

$test[] = 17;
array_push($test, 17);

Ruby

test << 17

Raven

17 $test push

PHP

$num = array_pop($test);

Ruby

num = test.pop

Raven

$test pop as $num

PHP

$num = $test[1];

Ruby

num = test[1]

Raven

$test.1 as $num

PHP

$test[1] = $num;

Ruby

test[1] = num

Raven

$num $test:1

Associative Arrays vs Hashes

PHP

$test = array('a' => 1, 'b' => 2, 'c' => 3);

Ruby

test = {'a' => 1, 'b' => 2, 'c' => 3}

Raven

{'a' 1 'b' 2 'c' 3} as $test

PHP

$var = $test['a'];
$test['b'] = 5;

Ruby

var = test['a']
test['b'] = 5

Raven

$test 'a' get as $var
5 $test 'b' set

# or equiv
$test.a as $var
5 $test:a

PHP

$items = count($array);

Ruby

items = array.size

Raven

$array length as $items

PHP

$keys = array_keys($test);

Ruby

keys = test.keys

Raven

$test keys as $keys

Loops

Looping Over Structures

PHP

foreach ($test as $key => $val) {
	func_1($key);
	func_2($val);
}

Ruby

test.each do |key, val|
  func_1 key
  func_2 val
end

Raven

# equivalent code
$test each pair as $key , $val
	$key func_1
	$val func_2

# optimal code
$test each pair func_1 func_2

One-liner Loops

PHP

foreach ($test as $key => $val) print "$key => $val\n";

Ruby

test.each { |key, val| puts "#{key} => #{value}" }

Raven

$test each pair "%s => %d\n" print

Conditional Loops

PHP

while (some_condition())
	some_function();

while (!some_condition())
	some_function();

Ruby

while some_condition do
  some_function
end

until some_condition do
  some_function
end

Raven

repeat some_condition while
	some_function

repeat some_condition until
	some_function

Counted Loops

PHP

for ($i = 0; $i < 10; $i++)
	do_something($i);

Ruby

10.times do |i|
  do_something i
end

Raven

# equivalent code
10 each as $i
	$i do_something

# optimal code
10 each do_something

PHP

for ($i = 0; i < 10; i+=2)
	do_something($i);

Ruby

(0..10).step(2) do |i|
  puts i
end

Raven

# equivalent code
0 10 2 range each as $i
	$i do_something

# optimal code
5 each 2 shl do_something

MySQL

PHP

$rs = mysql_query('SELECT * FROM mytable');
while ($row = mysql_fetch_assoc($rs))
	print_r($row);

Raven

# equivalent code
'SELECT * FROM mytable' $mysql query as rs
rs each as $row
	$row print

# optimal code
'SELECT * FROM mytable' $mysql query each print

PHP

$rs = mysql_query('SELECT * FROM mytable');
while (list ($field_1, $field_2, $field_3) = mysql_fetch_array($rs))
	do_something();

Raven

# equivalent code
'SELECT * FROM mytable' $mysql query as rs
rs each values into $field_1 , $field_2 , $field_3
	do_something

# optimal code
'SELECT * FROM mytable' $mysql query each values into $field_1 , $field_2 , $field_3
	do_something

PHP

$mysql = mysql_connect('localhost', 'user', 'password');
mysql_select_db('test', $mysql);

Raven

'mysql://user:password@localhost/test' open as $mysql

PHP

$rs = mysql_query('INSERT INTO mytable (field_1, field_2, field_3) VALUES (1, 2, 3)');
$id = mysql_insert_id();

Raven

# equivalent code
'INSERT INTO mytable (field_1, field_2, field_3) VALUES (1, 2, 3)' $mysql query as rs
rs inserted as $id

# optimal code
'INSERT INTO mytable (field_1, field_2, field_3) VALUES (1, 2, 3)' $mysql query inserted as $id

Files

PHP

$data = file_get_contents('myfile.txt');

Raven

'myfile.txt' read as $data

PHP

$lines = explode("\n", trim(file_get_contents('myfile.txt')));
foreach ($lines as $line)
	do_something($line);

Raven

# equivalent code
'myfile.txt' read trim "\n" split as $lines
$lines each as $line
	$line do_something

# optimal code
'myfile.txt' read trim "\n" split each do_something

eof