This Is A Custom Widget

This Sliding Bar can be switched on or off in theme options, and can take any widget you throw at it or even fill it with your custom HTML Code. Its perfect for grabbing the attention of your viewers. Choose between 1, 2, 3 or 4 columns, set the background color, widget divider color, activate transparency, a top border or fully disable it on desktop and mobile.

This Is A Custom Widget

This Sliding Bar can be switched on or off in theme options, and can take any widget you throw at it or even fill it with your custom HTML Code. Its perfect for grabbing the attention of your viewers. Choose between 1, 2, 3 or 4 columns, set the background color, widget divider color, activate transparency, a top border or fully disable it on desktop and mobile.
18 11, 2010

file_get_contents

By | 2017-05-16T17:29:25+00:00 November 18th, 2010|Categories: PHP, Ruby|Tags: , , |1 Comment

The file_get_contents() function in PHP reads the content of a file into a string (or reads the HTML of a web page into a string).

PHP

$body = file_get_contents( 'http://www.google.com' );
echo $body;

/* 
... HTML source of www.google.com ...
*/

To replicate this functionality in Ruby, we will use the Net/HTTP class from Ruby.

Ruby

require 'net/http';
uri = 'http://www.google.com';
body = Net::HTTP.get_response(URI.parse(self)).body;

p result
# ... HTML source of www.google.com ...

This page was contributed by Cemil Necefov. Thanks!

11 11, 2010

array_fill_keys

By | 2010-11-11T13:02:39+00:00 November 11th, 2010|Categories: PHP, Ruby|Tags: , , , |1 Comment

The array_fill_keys() function in PHP allows you to populate the values of an array while specifying its keys.

PHP

$keys = array('write', 'debug', 'execute');
$result = array_fill_keys($keys, 'code');
var_export($result);
// => array('write' => 'code', 'debug' => 'code', 'execute' => 'code')

To replicate this functionality in Ruby, we need to use a Hash object, since arrays in Ruby don’t use associative key/value pairs.

Ruby

keys = ['write', 'debug', 'execute']
result = keys.inject({}) do |hash, key| 
  hash[key] = 'code'
  hash 
end
p result
# => {"write"=>"code", "debug"=>"code", "execute"=>"code"}
19 10, 2010

array_merge

By | 2010-10-19T11:01:55+00:00 October 19th, 2010|Categories: PHP, Ruby|Tags: , , , , |1 Comment

The array_merge() function in PHP merges 2 arrays by appending the second array onto the first array, and returning the resulting array.

The way to do this in Ruby depends on the type of data – if dealing with associative arrays (known as a hash in Ruby), we can use the merge() method of the Ruby Hash class.

PHP

$user_details = array('name' => 'John', 'age' => 20);
$account_details = array('credits' => '50', 'id' => 4);

$user = array_merge($user_details, $account_details);

print_r($user);
/*
Array (
    [name] => John
    [age] => 20
    [credits] => 50
    [id] => 4
)
*/

Ruby

user_details = { :name => 'John', :age => 20 }
account_details = { :credits => 50, :id => 4 }

p user_details.merge(account_details);
# => {:credits=>50, :name=>"John", :id=>4, :age=>20}

Merging numeric arrays in Ruby is much easier as shown below (PHP example given first).

PHP

$start = array(1, 2, 3);
$finish = array(4, 5, 6);

$nums = array_merge($start, $finish);

print_r($nums);
/*
Array (
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
)
*/

Ruby

start  = [1, 2, 3];
finish = [4, 5, 6];

nums = start + finish;

p nums;
# => [1, 2, 3, 4, 5, 6]
13 10, 2010

array_fill

By | 2010-10-13T11:17:44+00:00 October 13th, 2010|Categories: PHP, Ruby|Tags: , , , , , |1 Comment

The array_fill() function in PHP allows an array to be populated (i.e. filled) with a particular value.

PHP

$a = array_fill(3, 5, 'php');
print_r($a);
/*
Array (
	[3] => php
	[4] => php
	[5] => php
	[6] => php
	[7] => php
)
*/

In Ruby this is not really possible because Ruby arrays must have their keys filled in the correct order, i.e. you cant skip assigning values to keys in a Ruby array. As such, the next best thing is to fill those values with nil, or use a hash instead.

Ruby

a = [nil] * 3 + ['php'] * 5;
puts a;
# => [nil, nil, nil, 'php', 'php', 'php', 'php', 'php']
19 04, 2010

array_flip

By | 2010-07-05T23:17:47+00:00 April 19th, 2010|Categories: PHP, Ruby|Tags: , , , |3 Comments

The array_flip() PHP function changes all the keys in an array into values, and all the values into keys.

PHP

$a = array('apple' => 1, 'ibm' => 2, 'sun' => 3);
$flipped = array_flip($a);
print_r($flipped);
/*
Array (
	[1] => apple
	[2] => ibm
	[3] => sun
)
*/

To replicate this functionality in Ruby, we will use a Hash object, since arrays in Ruby don’t use associative key / value pairs.

Ruby

hash = { "apple" => 1, "ibm" => 2, "sun" => 3 };
flipped = hash.invert;
p flipped;
# => {1 => "apple", 2 => "ibm", 3 => "sun"}

In PHP, the array_flip() function will over write any conflicting keys. The Ruby invert() method behaves the same – any keys which are the same as other keys will overwrite the previous one.

27 03, 2010

call_user_func

By | 2010-03-27T09:07:44+00:00 March 27th, 2010|Categories: PHP, Ruby|Tags: , |Comments Off on call_user_func

The call_user_func() function in PHP calls a user defined function as specified by the first parameter. You can also use call_user_func() to call an instance method of an object by using an array(instance, methodName) parameter as follows:

PHP

class User{
  private $name = null;
 
  public function __construct($name){
     $this->name = $name;
  }

  public function getName(){
    return $this->name;
  }
}

$user = new User('Shaymol');
echo call_user_func( array($user, 'getName') );

// => Shaymol 

To get same behavior in Ruby we can call the send() method of an object as follows:

Ruby

# define a user class
class User
  attr_accessor :name

  def initialize(name)
     @name = name.capitalize
  end

end

# create a user object
user = User.new('Shaymol')

# this is similar to user.name, and in PHP similar to call_user_func($obj, 'methodName');
puts user.send(:name)

# => Shaymol

This page was contributed by Shaymol. Thanks!

24 02, 2010

array_keys

By | 2010-02-24T12:31:01+00:00 February 24th, 2010|Categories: PHP, Ruby|Tags: , , |Comments Off on array_keys

The array_keys() function in PHP takes an array as it’s argument and returns all the keys in that array (as a numeric array).

PHP

$array = array('go' => 'green', 'stop' => 'red');
var_dump( array_keys($array) );
/*
Array (
	[0] => go
	[1] => stop
)
*/

To replicate this functionality in Ruby, we need to use a Hash object, since arrays in Ruby don’t use associative key/value pairs.

Ruby

array = { :go => 'green', :stop => 'red' };
puts array.keys;
# => [:go, :stop]
17 02, 2010

array_product

By | 2010-02-17T11:38:14+00:00 February 17th, 2010|Categories: PHP, Ruby|Tags: , , |Comments Off on array_product

The array_product() PHP function returns the product of all values in an array. In other words, it multiplies all values in the array together, and returns the result as a number.

PHP

$a = array('1', '2', '3');
$product_total = array_product($a);
echo $product_total;
// => 6 

Ruby

a = [ "1", "2", "3" ];
product_total = a.inject {|product, element| product * element }
puts product_total;
# => 6 
16 02, 2010

array_rand

By | 2010-02-16T15:18:42+00:00 February 16th, 2010|Categories: PHP, Ruby|Tags: , , , , , |1 Comment

The array_rand() function in PHP randomly selects one or more elements from an array.

PHP

$users = array('john', 'jane', 'tim', 'alex');
$lucky_winner = array_rand($users);
echo $lucky_winner;
// alex 

Ruby

users = [ 'john', 'jane', 'tim', 'alex' ];
lucky_winner = users[rand(users.length)];
puts lucky_winner;
# => alex 
25 10, 2009

array_reverse

By | 2009-11-16T14:46:56+00:00 October 25th, 2009|Categories: PHP, Ruby|Tags: , , |Comments Off on array_reverse

The array_reverse() function in PHP reverses the order of the elements in an array.

PHP

$a = array('php', 'ruby', 'java');
$results = array_reverse($a);
print_r($results);
/*
Array (
	[0] => 'java'
	[1] => 'ruby'
	[2] => 'php'
)
*/

To replicate this functionality in Ruby, we can use the reverse method of the Array object.

Ruby

a = [ "php", "ruby", "java" ];
p a.reverse;
# => ["java", "ruby", "php"]