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"}

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.