techminders

Free Software,Free Society

Saturday 6 February 2016

Make Any Laptop Touchscreen with Airbar

No comments :

The Airbar will turn any Windows laptop into a touch screen. Very cool. The technology is friendly with Windows 8 and 10 and this is because it uses Microsoft’s “Gestures” technology to turn your laptop into a touchscreen laptop.
The Airbar was designed in Sweden and made in Sweden. The bar is $50 US Dollars.
The Airbar works by invisible light beams. To get it working you connect is via USB and set the bar at the bottom of your laptop screen, just like you see in the picture.
The Airbar will project light upward. As your fingers break the barrier of the projected light, the bar will translate this into gestures. Through the Windows API for gestures your actions will translate to the programs running.
This is certainly an economical way of transforming your laptop into a touch screen.
You can swipe, touch, point and pinch to interact with your PC. If the Airbar is projecting light upward, I can understand how pinching horizontally will interact, but I don’t understand how pinching vertically will interact. It seems the bottom finger would block the top finger during the pinch movement. I say this because the light is projected vertically upward, bottom to top.
GetUSB.info has emailed the company asking for further explication.
Source: air.bar

Monday 1 February 2016

How can startup CEOs do better than everyone else

No comments :

1. They are good at both hiring and firing

January 28,2016
They are good at both hiring and firing
"Whenever you find a really great CEO, you find someone who has a knack for hiring. That means selling other people on your dream or your business, especially when it doesn't seem all that important or seems very risky. I used to work for a CEO who was awesome at hiring, but couldn't fire anyone. Doomed the business. Many of the best CEOs get others to follow no matter what."

— Robert Scoble, futurist at Rackspace

2. Being street smart is more important than being book smart

January 28,2016
Being street smart is more important than being book smart

"Getting out and understanding customers is far more important than book smarts or computer research."

— Mark Suster, partner at Upfront Ventures

3. Put your self-interest behind

January 28,2016
Put your self-interest behind
"Most entrepreneurs have a healthy dose of self-interest. However, serially successful startup leaders place their stakeholders' well being before their own personal gain. Situations inevitably arise in which key executives could covertly self-deal and enhance their financial outcomes at the expense of their fellow employees and investors. Consistent winners resist this temptation."

— John Greathouse, entrepreneur, investor and professor

4. It's okay to be unconventional

January 28,2016
It's okay to be unconventional
"[Zappos CEO Tony Hsieh] has conviction in his ideas. From the big examples of offering people money to quit the company after training (and doubling down again offering people severance if they didn't adopt his new management system called Holocracy), to smaller examples of his choice to live in a trailer park next to the office with a pet Alpaca, despite selling Unicorn to Amazon, shows he will pursue his true beliefs and not let others get in the way of his vision."

— Daniel Rodic, co-founder of Exact Media

5. They are detail oriented, but don't micro-manage

January 28,2016
They are detail oriented, but don't micro-manage
"A CEO has to be detail-oriented, but not micro-manage (there is a difference). Building a company is all about making all the pieces fit together so the machine can deliver predictable results at scale."

— Henning Moe, sales guy and entrepreneur

6. Have the courage to do something big

January 28,2016
Have the courage to do something big
"Interestingly no one has said courage. You must have courage to even take the plunge. Thereafter, you need the courage to follow your own path when everyone around you says you're crazy, wrong, or inexperienced. You need the courage to do what you think is right regardless of conventional wisdom. And finally, you need the courage to stand out, to be extraordinary, to make enemies (if need be), to say 'no' when everyone else would have said 'yes', and to be confident enough to be yourself."

— Tolis Dimopoulos, startup attorney, Sophos Law Firm

7. Don't be delusional. Be realistic.

January 28,2016
Don't be delusional. Be realistic.
"Legendary startup CEOs understand the game they are playing. They know that less than 1% of technology startups are ever worth $1 billion. They are not delusional about the fact that our industry produces a small number of legendary successes, handfuls of also-rans, and buckets of road kill."

— Christopher Lochhead, co-founding partner

8. Be able to delegate

January 28,2016
Be able to delegate
"Founders are great because they can do so much. Unlimited energy, passion and time. But once the company gets to a stage where they have to delegate, many founders fail because they want to control. I call it the rule of tens. If you manage 0-10 people you can be on top of everything. If you manage 100 people (through your direct reports), then you have to delegate responsibility. Your direct reports often ask you for advice, but then act. If you manage (through your direct reports) 1000 people, then people will tell you what they did — after they did it. It takes a different skill set for each factor of ten employees."

— John Backus, venture capitalist & former entrepreneur

9. Every great entrepreneur is a salesman

January 28,2016
Every great entrepreneur is a salesman
"You can't start your business if you can't sell your product or service. So if it's not in your nature to convince someone to come around to your way of thinking, you're not ever going to get your business off the ground."

— Gerard Danford, academic PhD

10. They know how to motivate others

January 28,2016
They know how to motivate others
"They know not only how to sell their idea and vision, but also how to charge people with energy and drive to pursue them."

— Yulichka YD

11. Enjoy a certain level of tension

January 28,2016
Enjoy a certain level of tension
"I think that the ability to embrace, explore and allow creative tension is something that separates the top 10%. I've seen many CEOs who try to avoid conflict or aim for consensus most of the time, and this can stifle creativity. Of course, this is just one attribute to accompany all the other great ones listed here."

— Alice Chan, PR consultant

12. They happen to be lucky

January 28,2016
They happen to be lucky
"Luck. Anyone who could be classified as being in the top 10% who doesn't own up to having plenty of luck on their way there is kidding themselves."

— Chris Puttick, co-founder of Two Ten

13. Willing to learn every day

January 28,2016
Willing to learn every day
"You need to keep learning every day. The faster you can tighten up your feedback loops and make your organization a learning organization that can adapt and implement based upon things out in the marketplace the better off you will be."

— Joseph Puopolo

Tuesday 22 September 2015

Indian IT companies among world's worst paymasters

No comments :

According to MyHiringClub.com's Worldwide IT Salary 2015 survey, India is ranked 7th on the list of lowest paymasters for IT managers.
NEW DELHI: Indian IT companies are among the 10 worst paymasters in the world, says a survey -- a mid-level IT manager draws an average salary of $41,213 while his Swiss counterpart gets over four times more.

According to recruitment platform MyHiringClub.com's Worldwide IT Salary 2015 survey, India was ranked 7th on the list of lowest paymasters for information technology (IT) managers, down by one position from last year's.

While Indian IT managers drew an average salary of $41,213, Bulgaria topped the list with a meagre $25,680, followed by Vietnam and Thailand averaging at $30,938 and $34,423, respectively.

"The impact of outsourcing and off-shoring on IT roles in North America and Western Europe helps explain the pattern of global pay," MyHiringClub.com & FlikJobs.com CEO Rajesh Kumar said.

The list includes Indonesia at the fourth spot with average wages of $34,780, followed by the Philippines ($37,534), India ($41,213), China ($42,689), Czech Republic ($43,219), and Argentina ($51,380).

"Lower-level roles are being moved to regions where talent is cheaper. The jobs that remain in Western Europe and the United States may be fewer in number, but are more demanding and complex. However, there is an increasing evidence of India's growing stature and presence in the high-end value chain, where cost advantages may not be the only drivers of future growth," Kumar added.

At the other end of the spectrum, when it comes to countries that pay out the best IT salaries, Switzerland again topped the list with an average annual remuneration of $1,71,465. The same job if taken in Belgium, second on the list, would fetch $1,52,430, the survey said.

Monday 3 August 2015

Print Date,Time,Month calendar in Python

No comments :

import time
import calendar

localtime=time.asctime(time.localtime(time.time()) )
print("local time:",localtime)

cal=calendar.month(2015, 8)
print("calendar is:", cal)



OUTPUT:-|

Friday 10 July 2015

BUILD YOUR OWN GAMING PC UNDER 60,000

5 comments :

PC Configuration :-



1> Processor  Intel Core i5-4440  


   Price: Rs. 10,690 (approx) Cores: 4 
   Threads: 4 
   Base Frequency: 3.1GHz  
   Turbo Frequency: 3.3GHz  
   Socket: LGA 1150  
   Data Width: 64-bit 
   L3 cache: 6MB


2>Motherboard  ASUS B85M-G





Price: Rs. 5,989 (approx)

LGA1150 motherboard
Support for DDR3 4 x DIMM, Max. 32GB, DDR3 1600/1333/1066 MHz RAM
2 x PCIe 2.0
6 native SATA3 ports
4 USB 3.0 ports


3>RAM Gskill RipjawsX F3-12800CL9D-8GBXL (2 x 4GB) 


Price: Rs. 5,950 (approx) 
Memory type: (2 x 4GB) DDR3 1600MHz  
Timing: 9-9-9-24-2N


4>HDD  Seagate Barracuda 1TB 

Price: Rs. 3,550 (approx) 
RPM: 7200 Interface: 6GB/s  
Cache: 64MB  
Operating temperature: 60 degrees celsius


5>Monitor  Dell S2240L 

Price: Rs. 8,381 (approx)
Screen size: 21.5-inch Max 
resolution: 1920 x 1080p  
Brightness: 250 nits 
Contrast Ratio: 1,000:1  
Panel type: LCD

6>Graphics Card  ZOTAC GTX 660 2 GB 


Price: Rs. 12,800 (approx) 
Base clock: 993 MHz 
 vRAM: 2GB 192-bit 
GDDR5  2-fan cooling  
Maximum output (3840 x 2160)



7>Cabinet  Thermaltake Versa H21 


Price: Rs. 2,730 (approx) 
Mid-tower 3 internal 3.5-inch drive bays 3 
external 5.25-inch drive bays Upto ATX motherboard support 6.7Kg

8>SMPS  Seasonic S12II 620 Watts 



Price:Rs. 5,620 (approx) 
Output Capacity: 620W  Efficiency: >87%@ 
Typical Load  5 year warranty 
 Fan: 120mm fan bottom 

9>Speakers  F&D F680


Price: Rs. 2,647 (approx) 
70 W Total Power
2.1 Channel Configuration 
1 Year Warranty

10>Optical Drive Samsung SH-224DB/IDDS


Price: Rs. 950 (approx)

11>Mouse & Keyboard CM Storm Devastator (Bundle)


Price: Rs. 2,330 (approx)

                     

TOTAL











Wednesday 8 July 2015

E-commerce companies Require skills for higher salaries

No comments :
NEW DELHI:Industry experts, however, rule out a mass migration from the services sector immediately -- even if e-commerce companies are emerging as bigger pay masters -- mainly because of the advanced skill-sets in demand at the new firms.

"The kind of people e-commerce (firms) are looking for are not easily available in the service-based IT sector," said Alka Dhingra, regional manager at hiring firm TeamLease Services, which conducted the study. "They are looking for people mostly from product-based companies, and are willing to pay more for people working on new technology and tools because these people are scarce resources."

Vice presidents and chief marketing officers with 18-20 years' experience at IT service companies such as Tata Consultancy Services and Infosys are paid Rs 70 lakh to Rs 1.5 crore a year, and mid-level executives such as project managers Rs 15-25 lakh a year, according to TeamLease data.

At e-commerce companies such as Snapdeal and ShopClues, the annual compensation for senior executives reaches Rs 1.5-3 crore, while midlevel employees with eight to 14 years' experience can earn Rs 50 lakh to Rs 1.5 crore a year.

The pay scales have leaped since 2013, when senior executives at IT companies earned Rs 55-75 lakh a year and middle-level employees at e-commerce companies got Rs 35 lakh to Rs 50 lakh.

"This trend is more pronounced for coveted positions within the e-commerce companies," said a spokesperson for software industry body Nasscom. "Right now the IT companies are not very stressed on this trend because they have optimized their models."

"Indeed, the skill sets commanding high salaries at ecommerce companies are relatively new -- analytics, big data, data structuring, Python programming language, DevOps, Hadoop, user interface and mobility. Many of these are increasingly becoming important in the traditional IT services sector as well."


"E-commerce companies have very small core teams, and the kind of people they need are very different from the IT services companies," said Anuraag Gupta, chief executive officer of staffing company Magna Infotech. "They would not in the long run create an army of coders, testers, etc., and may eventually outsource work to the IT service sector, keeping the core technology team small."

India's digital commerce market is expected to grow 33% this year to about Rs 1 lakh crore, up from Rs 81,525 crore at the end of December, according to industry body Internet & Mobile Association of India.

IT services firms, on the other hand, have matured over the years and the industry is expected to grow at 12-14% this year, according to Nasscom.


Thursday 2 July 2015

ONLINE EXAMINATION SYSTEM (PHP+MYSQL)

No comments :



ONLINE EXAMINATION SYSTM:




USING : PHP+MYSQL

Require Software:- Wamp, Xammp etc.
                            

                   Main  Modules:
                                      1:user modules 

                                      2: Admin modules
                                                        

                                                                  




The download link is:
https://www.dropbox.com/sh/4snf73s93u1xobp/AAAQDKTlJ8sm6jQYoXJduoMFa?dl=0
 Please Read Readme.txt file first  in ' new folder/readme.text'

       

                        If you have any suggestion , Problems Please Comment Blow

                                      THANKS!




Tuesday 23 June 2015

JavaScript, PHP, Python, Ruby complete basic syntaxt guide with interpretation in one sheet

No comments :
Image result for JavaScript, PHP, Python, Ruby

JavaScript, PHP, Python, Ruby 


































































version
javascriptphppythonruby
version used5.52.7; 3.31.9; 2.0
show version$ node --version$ php --version$ python -V
$ python --version
$ ruby --version
implicit prologue<script src="underscore.js">
</script>
noneimport os, re, sysnone
grammar and execution
javascriptphppythonruby
interpreter$ node foo.js$ php -f foo.php$ python foo.py$ ruby foo.rb
repl$ node

mac:
$ osascript -l JavaScript -i
$ php -a$ python$ irb
command line program$ node -e 'var sys = require("sys");
sys.puts("hi world!");'

mac:
$ osascript -l JavaScript -e '1 + 1'
$ php -r 'echo "hi\n";'$ python -c 'print("hi")'$ ruby -e 'puts "hi"'
block delimiters{}{}and offside rule{}
do end
statement separator; or newline

newline not separator inside (), [], {}, "", '', or after binary operator

newline sometimes not separator when following line would not parse as a valid statement
;

statements must be semicolon terminated inside {}
newline or ;

newlines not separators inside (), [], {}, triple quote literals, or after backslash: \
newline or ;

newlines not separators inside (), [], {}, ``, '', "", or after binary operator or backslash: \
source code encodingnonePython 3 source is UTF-8 by default

# -*- coding: utf-8 -*-
Ruby 2.0 source is UTF-8 by default

# -*- coding: utf-8 -*-
end-of-line comment// comment// comment
# comment
# comment# comment
multiple line comment/* line
another line */
/* comment line
another line */
use triple quote string literal:

'''comment line
another line'''
=begin
comment line
another line
=end
variables and expressions
javascriptphppythonruby
local variablevar x = 1;

var y = 2, z = 3;
# in function body:
$v = NULL;
$a = [];
$d = [];
$x = 1;
list($y, $z) = [2, 3];
# in function body:
v = None
ad = [], {}
x = 1
yz = 2, 3
v = nil
a, d = [], {}
x = 1
y, z = 2, 3
regions which define lexical scopetop level:
  html page

nestable:
  function
top level:
  function or method body

nestable (with use clause):
  anonymous function body
nestable (read only):
  function or method body
top level:
  file
  class block
  module block
  method body

nestable:
  anonymous function body
  anonymous block
global variable// assign without using var
g = 1;

function incr_global () { g++; }
list($g1, $g2) = [7, 8];

function swap_globals() {
  global $g1, $g2;
  list($g1, $g2) = [$g2, $g1];
}
g1g2 = 7, 8

def swap_globals():
  global g1, g2
  g1, g2 = g2, g1
$g1$g2 = 7, 8

def swap_globals
  $g1$g2 = $g2$g1
end
constantnonedefine("PI", 3.14);

const PI = 3.14;
# uppercase identifiers
# constant by convention

PI = 3.14
# warning if capitalized
# identifier is reassigned

PI = 3.14
assignmentx = 1;$v = 1;assignments can be chained but otherwise don't return values:
v = 1
v = 1
parallel assignmentnonelist($x, $y, $z) = [1 ,2, 3];

# 3 is discarded:
list($x, $y) = [1, 2, 3];

# $z set to NULL:
list($x, $y, $z) = [1, 2];
xyz = 1, 2, 3

# raises ValueError:
xy = 1, 2, 3

# raises ValueError:
xyz = 1, 2
x, y, z = 1, 2, 3

# 3 is discarded:
x, y = 1, 2, 3

# z set to nil:
x, y, z = 1, 2
swaptmp = x;
x = y;
y = tmp;
list($x, $y) = [$y, $x];xy = y, xx, y = y, x
compound assignment
arithmetic, string, logical, bit
+= -= *= /= none %=
+=
none
<<= >>= &= |= ^=
+= -= *= none /= %= **=
.= none
&= |= none
<<= >>= &= |= ^=
# do not return values:
+= -= *= /= //= %= **=
+= *=
&= |= ^=
<<= >>= &= |= ^=
+= -= *= /= none %= **=
+= *=
&&= ||= ^=
<<= >>= &= |= ^=
increment and decrementvar x = 1;
var y = ++x;
var z = --y;
$x = 1;
$y = ++$x;
$z = --$y;
nonex = 1
# x and y not mutated:
y = x.succ
z = y.pred
nullnullNULL # case insensitiveNonenil
null testv === nullis_null($v)
! isset($v)
v == None
is None
v == nil
v.nil?
undefined variableundefinedNULLraises NameErrorraises NameError
conditional expressionx > 0 ? x : -x$x > 0 ? $x : -$xif x > 0 else -xx > 0 ? x : -x
arithmetic and logic
javascriptphppythonruby
true and falsetrue falseTRUE FALSE # case insensitiveTrue Falsetrue false
falsehoodsfalse null undefined "" 0 NaNFALSE NULL 0 0.0 "" "0" []False None 0 0.0 '' [] {}false nil
logical operators&& || !&& || !
lower precedence:
and or xor
and or not&& || !
lower precedence:
and or not
relational operators=== !== < > >= <=

perform type coercion:
== !=
== != or <> > < >= <=
no conversion: === !==
relational operators are chainable:
== != > < >= <=
== != > < >= <=
min and maxMath.min(1, 2, 3)
Math.max(1, 2, 3)

Math.min.apply(Math, [1, 2, 3])
Math.max.apply(Math, [1, 2, 3])
min(1, 2, 3)
max(1, 2, 3)
$a = [1, 2, 3]
min($a)
max($a)
min(1, 2, 3)
max(1, 2, 3)

min([1, 2, 3])
max([1, 2, 3])
[1, 2, 3].min
[1, 2, 3].max
three value comparisonnonenoneremoved from Python 3:
cmp(0, 1)
cmp('do''re')
0 <=> 1
"do" <=> "re"
arithmetic operators
addition, subtraction, multiplication, float division, quotient, remainder
+ - * / none %+ - * / none %+ - * see note // %

Python 2 does not have an operator which performs float division on integers. In Python 3 / always performs float division.
+ - * x.fdiv(y) / %
integer divisionMath.floor(x / y)(int)(13 / 5)13 // 513 / 5
divmodnonenoneq, r = divmod(13, 5)q, r = 13.divmod(5)
integer division by zeroreturns assignable value Infinity, NaN, or -Infinity depending upon whether dividend is positive, zero, or negative.

There are literals for Infinity and NaN.
returns FALSE with warningraises ZeroDivisionErrorraises ZeroDivisionError
float division13 / 513 / 5float(13) / 5

# Python 3:
13 / 5
13.to_f / 5 or
13.fdiv(5)
float division by zerosame behavior as for integersreturns FALSE with warningraises ZeroDivisionErrorreturns -Infinity, NaN, or Infinity
powerMath.pow(2, 32)pow(2, 32)2**322**32
sqrtMath.sqrt(2)sqrt(2)import math

math.sqrt(2)
include Math

sqrt(2)
sqrt -1NaNNaN# raises ValueError:
import math
math.sqrt(-1)

# returns complex float:
import cmath
cmath.sqrt(-1)
raises Errno::EDOM
transcendental functionsMath.exp Math.log Math.sin Math.cos Math.tan Math.asin Math.acos Math.atan Math.atan2exp log sin cos tan asin acos atan atan2from math import exp, log, \
sin, cos, tan, asin, acos, atan, atan2
include Math

exp log sin cos tan asin acos atan atan2
transcendental constants
Ï€ and e
Math.PI
Math.E
M_PI M_Eimport math

math.pi math.e
include Math

PI E
float truncationnone
Math.round(3.1)
Math.floor(3.1)
Math.ceil(3.1)
(int)$x
round($x)
ceil($x)
floor($x)
import math

int(x)
int(round(x))
math.ceil(x)
math.floor(x)
x.to_i
x.round
x.ceil
x.floor
absolute valueMath.abs(-3)abs($x)abs(x)x.abs
integer overflowall numbers are floatsconverted to floatbecomes arbitrary length integer of typelongbecomes arbitrary length integer of typeBignum
float overflowInfinityINFraises OverflowErrorInfinity
rational constructionnonefrom fractions import Fraction

x = Fraction(22, 7)
require 'rational'

x = Rational(22, 7)
rational decompositionnonex.numerator
x.denominator
x.numerator
x.denominator
complex constructionnonez = 1 + 1.414jrequire 'complex'

z = 1 + 1.414.im
complex decomposition
real and imaginary component, argument, absolute value, conjugate
noneimport cmath

z.real
z.imag
cmath.phase(z)
abs(z)
z.conjugate()
z.real
z.imag
z.arg
z.abs
z.conj
random number
uniform integer, uniform float, normal float
Math.floor(Math.random() * 100)
Math.random()
none
rand(0,99)
lcg_value()
none
import random

random.randint(0, 99)
random.random()
random.gauss(0, 1)
rand(100)
rand
none
random seed
set, get, restore
nonesrand(17);

none
import random

random.seed(17)
seed = random.getstate()
random.setstate(seed)
srand(17)

seed = srand
srand(seed)
bit operators<< >> & | ^ ~<< >> & | ^ ~<< >> & | ^ ~<< >> & | ^ ~
binary, octal, and hex literalsnone
052 // deprecated
0x2a
0b101010
052
0x2a
0b101010
052
0x2a
0b101010
052
0x2a
radix
convert integer to and from string with radix
(42).toString(7)
??
base_convert("42", 10, 7);
base_convert("60", 7, 10);
none
int("60", 7)
42.to_s(7)
"60".to_i(7)
strings
javascriptphppythonruby
string typeStringstringstrString
string literal"don't say \"no\""
'don\'t say "no"'
"don't say \"no\""
'don\'t say "no"'
'don\'t say "no"'
"don't say \"no\""
"don't " 'say "no"'
'''don't say "no"'''
"""don't say "no\""""
"don't say \"no\""
'don\'t say "no"'
"don't " 'say "no"'
newline in literalyes'first line
second line'

"first line
second line"
triple quote literals only:
'''first line
second line'''

"""first line
second line"""
'first line
second line'

"first line
second line"
literal escapessingle and double quotes:
\b \f \n \r \t \v \uhhhh \xhh \" \' \\
double quoted:
\f \n \r \t \v \xhh \$ \" \\ \ooo

single quoted:
\' \\
single and double quoted:
\newline \\ \' \" \a \b \f \n \r \t \v \ooo\xhh

Python 3:
\uhhhh \Uhhhhhhhh
double quoted:
\a \b \cx \e \f \n \r \s \t \v \xhh \ooo\uhhhh \u{hhhhh}

single quoted:
\' \\
here documentnone$word = "amet";

$s = <<<EOF
lorem ipsum
dolor sit $word
EOF
;
noneword = "amet"

s = <<EOF
lorem ipsum
dolor sit
 #{word}
EOF
variable interpolation// None; use string concatenation.
// Both of these expressions are '11':

1 + "1"
"1" + 1
$count = 3;
$item = "ball";
echo "$count ${item}s\n";
count = 3
item = 'ball'
print('{count} {item}s'.format(
  **locals()))
count = 3
item = "ball"
puts "#{count} #{item}s"
expression interpolationnonenone'1 + 1 = {}'.format(1 + 1)"1 + 1 = #{1 + 1}"
format string// None; use string concatenation.
// Evaluates to "12.35":

12.3456.toFixed(2)
$fmt = "lorem %s %d %f";
sprintf($fmt"ipsum", 13, 3.7);
'lorem %s %d %f' % ('ipsum', 13, 3.7)

fmt = 'lorem {0} {1} {2}'
fmt.format('ipsum', 13, 3.7)
"lorem %s %d %f" % ["ipsum", 13, 3.7]
are strings mutable?no$s = "bar";
$s2 = $s;
# sets s to "baz"; s2 is unchanged:
$s[2] = "z";
nos = "bar"
s2 = s
# sets s and s2 to "baz":
s[2] = "z"
copy stringnone$s2 = $s;nones = "bar"
s2 = s.clone
# s2 is not altered:
s[2] = "z"
concatenates = "Hello, " + "World!";$s = "Hello, ";
$s2 = $s . "World!";
s = 'Hello, '
s2 = s + 'World!'

# juxtaposition can be used to
# concatenate literals:

s2 = 'Hello, ' "World!"
s = "Hello, "
s2 = s + "World!"

# juxtaposition can be used to
# concatenate literals:

s2 = "Hello, " 'World!'
replicatevar hbar = Array(80).join("-");$hbar = str_repeat("-", 80);hbar = '-' * 80hbar = "-" * 80
translate case
to upper, to lower
"lorem".toUpperCase()
"LOREM".toLowerCase()
strtoupper("lorem")
strtolower("LOREM")
'lorem'.upper()
'LOREM'.lower()
"lorem".upcase
"LOREM".downcase
capitalize
string, words
noneucfirst("lorem")
ucwords("lorem ipsum")
import string

'lorem'.capitalize()
string.capwords('lorem ipsum')
"lorem".capitalize
none
trim
both sides, left, right
" lorem ".trim()
# some browsers:
" lorem".trimLeft()
"lorem ".trimRight()
trim(" lorem ")
ltrim(" lorem")
rtrim("lorem ")
' lorem '.strip()
' lorem'.lstrip()
'lorem '.rstrip()
" lorem ".strip
" lorem".lstrip
"lorem ".rstrip
pad
on right, on left, centered
nonestr_pad("lorem", 10)
str_pad("lorem", 10, " "STR_PAD_LEFT)
str_pad("lorem", 10, " "STR_PAD_BOTH)
'lorem'.ljust(10)
'lorem'.rjust(10)
'lorem'.center(10)
"lorem".ljust(10)
"lorem".rjust(10)
"lorem".center(10)
number to string"value: " + 8"value: " . 8'value: ' + str(8)"value: " + 8.to_s
string to number7 + parseInt("12", 10)
73.9 + parseFloat(".037")

// 12:
parseInt("12A")
// NaN:
parseInt("A")
7 + "12"
73.9 + ".037"

# 12:
0 + "12A"
# 0:
0 + "A"
7 + int('12')
73.9 + float('.037')

# raises ValueError:
int('12A')
# raises ValueError:
int('A')
7 + "12".to_i
73.9 + ".037".to_f

# 12:
"12A".to_i
# 0:
"A".to_i
string join["do", "re", "mi"].join(" ")$a = ["do""re""mi""fa"];
implode(" ", $a)
' '.join(['do''re''mi''fa'])

# raises TypeError:
' '.join([1, 2, 3])
%w(do re mi fa).join(' ')

# implicitly converted to strings:
[1, 2, 3].join(' ')
split// [ "do", "re", "", "mi", "" ]:
"do re  mi ".split(" ")

// [ "do", "re", "mi", "" ]:
"do re  mi ".split(/\s+/)
# [ "do", "re", "", "mi", "" ]:
explode(" ""do re  mi ")

# [ "do", "re", "mi", "" ]:
preg_split('/\s+/', "do re  mi ")
# ['do', 're', '', 'mi', '']:
'do re  mi '.split(' ')

# ['do', 're', 'mi']:
'do re  mi '.split()
# ["do", "re", "", "mi"]:
"do re  mi ".split(/ /)

# ["do", "re", "mi"]:
"do re  mi ".split
split in twononepreg_split('/\s+/'"do re mi fa", 2)'do re mi fa'.split(None, 1)"do re mi fa".split(/\s+/, 2)
split and keep delimitersnonepreg_split('/(\s+)/'"do re mi fa",
  NULLPREG_SPLIT_DELIM_CAPTURE)
re.split('(\s+)''do re mi fa')"do re mi fa".split(/(\s+)/)
prefix and suffix test"foobar".startsWith("foo")
"foobar".endsWith("bar")
'foobar'.startswith('foo')
'foobar'.endswith('bar')
'foobar'.start_with?('foo')
'foobar'.end_with?('bar')
length"lorem".lengthstrlen("lorem")len('lorem')"lorem".length
"lorem".size
index of substring
first, last
// returns -1 if not found:
"lorem ipsum".indexOf("ipsum")
# returns FALSE if not found:
strpos("do re re""re")
strrpos("do re re""re")
# raises ValueError if not found:
'do re re'.index('re')
'do re re'.rindex('re')

# returns -1 if not found:
'do re re'.find('re')
'do re re'.rfind('re')
# returns nil if not found:
"do re re".index("re")
"do re re".rindex("re")
extract substring
by start and length, by start and end, by successive starts
"lorem ipsum".substr(6, 5)
"lorem ipsum".substring(6, 11)
substr("lorem ipsum", 6, 5)
none
none
none
none
'lorem ipsum'[6:11]
"lorem ipsum"[6, 5]
"lorem ipsum"[6..10]
"lorem ipsum"[6...11]
character lookup"lorem ipsum"[6]# syntax error to use index notation
# directly on string literal:

$s = "lorem ipsum";
$s[6];
'lorem ipsum'[6]"lorem ipsum"[6]
chr and ordString.fromCharCode(65)
"A".charCodeAt(0)
chr(65)
ord("A")
chr(65)
ord('A')

# Python 3:
chr(0x3bb)
ord('\u03bb')
65.chr
"A".ord

0x3bb.chr('UTF-8')
"\u03bb".codepoints.first
to array of characters"abcd".split("")str_split("abcd")list('abcd')"abcd".split("")
translate charactersnone$ins = implode(range("a""z"));
$outs = substr($ins, 13, 13) .
  substr($ins, 0, 13);
strtr("hello", $ins, $outs)
from string import lowercase as ins
from string import maketrans

outs = ins[13:] + ins[:13]
'hello'.translate(maketrans(ins,outs))
"hello".tr("a-z""n-za-m")
delete charactersnone$vowels = str_split("aeiou");
$s = "disemvowel me";
$s = str_replace($vowels"", $s);
"disemvowel me".translate(None"aeiou")"disemvowel me".delete("aeiou")
squeeze charactersnone$s = "too  much  space";
$s = = preg_replace('/(\s)+/''\1', $s);
re.sub('(\s)+', r'\1''too  much  space')"too  much  space".squeeze(" ")
regular expressions
javascriptphppythonruby
literal, custom delimited literal/lorem|ipsum/'/lorem|ipsum/'
'(/etc/hosts)'
re.compile('lorem|ipsum')
none
/lorem|ipsum/
%r(/etc/hosts)
character class abbreviations. \d \D \s \S \w \W. \d \D \h \H \s \S \v \V \w \W. \d \D \s \S \w \W. \d \D \h \H \s \S \w \W
anchors^ $ \b \B^ $ \A \b \B \z \Z^ $ \A \b \B \Z^ $ \A \b \B \z \Z
match testif (s.match(/1999/)) {
  alert("party!");
}
if (preg_match('/1999/', $s)) {
  echo "party!\n";
}
if re.search('1999', s):
  print('party!')
if /1999/.match(s)
  puts "party!"
end
case insensitive match test"Lorem".match(/lorem/i)preg_match('/lorem/i'"Lorem")re.search('lorem''Lorem', re.I)/lorem/i.match("Lorem")
modifiersg i me i m s xre.I re.M re.S re.Xi o m x
substitutions = "do re mi mi mi";
s.replace(/mi/g, "ma");
$s = "do re mi mi mi";
$s = preg_replace('/mi/'"ma", $s);
s = 'do re mi mi mi'
s = re.compile('mi').sub('ma', s)
s = "do re mi mi mi"
s.gsub!(/mi/"ma")
match, prematch, postmatchm = /\d{4}/.exec(s);
if (m) {
  match = m[0];
  # no prematch or postmatch
}
nonem = re.search('\d{4}', s)
if m:
  match = m.group()
  prematch = s[0:m.start(0)]
  postmatch = s[m.end(0):len(s)]
m = /\d{4}/.match(s)
if m
  match = m[0]
  prematch = m.pre_match
  postmatch = m.post_match
end
group capturerx = /^(\d{4})-(\d{2})-(\d{2})$/;
m = rx.exec('2009-06-03');
yr = m[1];
mo = m[2];
dy = m[3];
$s = "2010-06-03";
$rx = '/(\d{4})-(\d{2})-(\d{2})/';
preg_match($rx, $s, $m);
list($_, $yr, $mo, $dy) = $m;
rx = '(\d{4})-(\d{2})-(\d{2})'
m = re.search(rx, '2010-06-03')
yrmody = m.groups()
rx = /(\d{4})-(\d{2})-(\d{2})/
m = rx.match("2010-06-03")
yr, mo, dy = m[1..3]
named group capturenone$s = "foo.txt";
$rx = '/^(?P<file>.+)\.(?P<suffix>.+)$/';
preg_match($rx, $s, $m);

$m["file"]
$m["suffix"]
rx = '^(?P<file>.+)\.(?P<suffix>.+)$'
m = re.search(rx, ''foo.txt')

m.groupdict()['file']
m.groupdict()['suffix']
rx = /^(?<file>.+)\.(?<suffix>.+)$/
m = rx.match('foo.txt')

m["file"]
m["suffix"]
scanvar a = "dolor sit amet".match(/\w+/g);$s = "dolor sit amet";
preg_match_all('/\w+/', $s, $m);
$a = $m[0];
s = 'dolor sit amet'
a = re.findall('\w+', s)
a = "dolor sit amet".scan(/\w+/)
backreference in match and substitution/(\w+) \1/.exec("do do")

"do re".replace(/(\w+) (\w+)/, '$2 $1')
preg_match('/(\w+) \1/'"do do")

$s = "do re";
$rx = '/(\w+) (\w+)/';
$s = preg_replace($rx'\2 \1', $s);
none

rx = re.compile('(\w+) (\w+)')
rx.sub(r'\2 \1''do re')
/(\w+) \1/.match("do do")

"do re".sub(/(\w+) (\w+)/'\2 \1')
recursive regexnone'/\(([^()]*|($R))\)/'none/(?<foo>\(([^()]*|\g<foo>)*\))/
dates and time
javascriptphppythonruby
broken-down datetime typeDateDateTimedatetime.datetimeTime
current datetimevar t = new Date();$t = new DateTime("now");
$utc_tmz = new DateTimeZone("UTC");
$utc = new DateTime("now", $utc_tmz);
import datetime

t = datetime.datetime.now()
utc = datetime.datetime.utcnow()
t = Time.now
utc = Time.now.utc
current unix epoch(new Date()).getTime() / 1000$epoch = time();import datetime

t = datetime.datetime.now()
epoch = int(t.strftime("%s"))
epoch = Time.now.to_i
broken-down datetime to unix epochMath.round(t.getTime() / 1000)$epoch = $t->getTimestamp();from datetime import datetime as dt

epoch = int(t.strftime("%s"))
epoch = t.to_i
unix epoch to broken-down datetimevar epoch = 1315716177;
var t2 = new Date(epoch * 1000);
$t2 = new DateTime();
$t2->setTimestamp(1304442000);
t = dt.fromtimestamp(1304442000)t = Time.at(1304442000)
format datetimenonestrftime("%Y-%m-%d %H:%M:%S", $epoch);
date("Y-m-d H:i:s", $epoch);
$t->format("Y-m-d H:i:s");
t.strftime('%Y-%m-%d %H:%M:%S')t.strftime("%Y-%m-%d %H:%M:%S")
parse datetimenone$fmt = "Y-m-d H:i:s";
$s = "2011-05-03 10:00:00";
$t = DateTime::createFromFormat($fmt,
  $s);
from datetime import datetime

s = '2011-05-03 10:00:00'
fmt = '%Y-%m-%d %H:%M:%S'
t = datetime.strptime(s, fmt)
require 'date'

s = "2011-05-03 10:00:00"
fmt = "%Y-%m-%d %H:%M:%S"
t = DateTime.strptime(s, fmt).to_time
parse datetime w/o formatvar t = new Date("July 7, 1999");$epoch = strtotime("July 7, 1999");# pip install python-dateutil
import dateutil.parser

s = 'July 7, 1999'
t = dateutil.parser.parse(s)
require 'date'

s = "July 7, 1999"
t = Date.parse(s).to_time
date partst.getFullYear()
t.getMonth() + 1
t.getDate() # getDay() is day of week
(int)$t->format("Y")
(int)$t->format("m")
(int)$t->format("d")
t.year
t.month
t.day
t.year
t.month
t.day
time partst.getHours()
t.getMinutes()
t.getSeconds()
(int)$t->format("H")
(int)$t->format("i")
(int)$t->format("s")
t.hour
t.minute
t.second
t.hour
t.min
t.sec
build broken-down datetimevar yr = 1999;
var mo = 9;
var dy = 10;
var hr = 23;
var mi = 30;
var ss = 0;
var t = new Date(yr, mo - 1, dy, hr, mi, ss);
import datetime

yr = 1999
mo = 9
dy = 10
hr = 23
mi = 30
ss = 0
t = datetime.datetime(yr, mo, dy, hr, mi, ss)
yr = 1999
mo = 9
dy = 10
hr = 23
mi = 30
ss = 0
t = Time.new(yr, mo, dy, hr, mi, ss)
datetime subtractionnumber containing time difference in milliseconds# DateInterval object if diff method used:
$fmt = "Y-m-d H:i:s";
$s = "2011-05-03 10:00:00";
$then = DateTime::createFromFormat($fmt, $s);
$now = new DateTime("now");
$interval = $now->diff($then);
datetime.timedelta object

use total_seconds() method to convert to float representing difference in seconds
Float containing time difference in seconds
add durationvar t1 = new Date();
var delta = (10 * 60 + 3) * 1000;
var t2 = new Date(t1.getTime() + delta);
$now = new DateTime("now");
$now->add(new DateInterval("PT10M3S");
import datetime

delta = datetime.timedelta(
  minutes=10,
  seconds=3)
t = datetime.datetime.now() + delta
require 'date/delta'

s = "10 min, 3 s"
delta = Date::Delta.parse(s).in_secs
t = Time.now + delta
local time zone determination# DateTime objects can be instantiated without
# specifying the time zone if a default is set:

$s = "America/Los_Angeles";
date_default_timezone_set($s);
a datetime object has no time zone information unless a tzinfo object is provided when it is createdif no time zone is specified the local time zone is used
nonlocal time zone# pip install pytz
import pytz
import datetime

tmz = pytz.timezone('Asia/Tokyo')
utc = datetime.datetime.utcnow()
utc_dt = datetime.datetime(
  *utc.timetuple()[0:5],
  tzinfo=pytz.utc)
jp_dt = utc_dt.astimezone(tmz)
# gem install tzinfo
require 'tzinfo'

tmz = TZInfo::Timezone.get("Asia/Tokyo")
jp_time = tmz.utc_to_local(Time.now.utc)
time zone info

name and UTC offset
$tmz = date_timezone_get($t);

timezone_name_get($tmz);
date_offset_get($t) / 3600;
import time

tm = time.localtime()
  
time.tzname[tm.tm_isdst]
(time.timezone / -3600) + tm.tm_isdst
t.zone
t.utc_offset / 3600
daylight savings test$t->format("I");import time

tm = time.localtime()
  
tm.tm_isdst
t.dst?
microsecondslist($frac, $sec) = explode(" ", microtime());
$usec = $frac * 1000 * 1000;
t.microsecondt.usec
sleepnone# a float argument will be truncated
# to an integer:

sleep(1);
import time

time.sleep(0.5)
sleep(0.5)
timeoutuse set_time_limit to limit execution time of the entire script; usestream_set_timeout to limit time spent reading from a stream opened with fopen orfsockopenimport signal, time

class Timeout(Exception): pass

def timeout_handler(signo, fm):
  raise Timeout()

signal.signal(signal.SIGALRM,
  timeout_handler)

try:
  signal.alarm(5)
  might_take_too_long()
except Timeout:
  pass
signal.alarm(0)
require 'timeout'

begin
  Timeout.timeout(5) do
    might_take_too_long
  end
rescue Timeout::Error
end
resizable arrays
javascriptphppythonruby
literala = [1, 2, 3, 4]$a = [1, 2, 3, 4];

# older syntax:
$a = array(1, 2, 3, 4);
a = [1, 2, 3, 4]a = [1, 2, 3, 4]

# a = ['do', 're', 'mi']
a = %w(do re mi)
sizea.lengthcount($a)len(a)a.size
a.length # same as size
empty test// TypeError if a is null or undefined:
a.length === 0
# NULL tests as empty:
!$a
# None tests as empty:
not a
# NoMethodError if a is nil:
a.empty?
lookupa[0]$a[0]

# PHP uses the same type for arrays and
# dictionaries; indices can be negative
# integers or strings
a[0]

# returns last element:
a[-1]
a[0]

# returns last element:
a[-1]
updatea[0] = "lorem"$a[0] = "lorem";a[0] = 'lorem'a[0] = "lorem"
out-of-bounds behaviorreturns undefined$a = [];
# evaluates as NULL:
$a[10];
# increases array size to one:
$a[10] = "lorem";
a = []
# raises IndexError:
a[10]
# raises IndexError:
a[10] = 'lorem'
a = []
# evaluates as nil:
a[10]
# increases array size to 11:
a[10] = "lorem"
element index

first and last occurrence
// return -1 if not found:
[6, 7, 7, 8].indexOf(7)
[6, 7, 7, 8].lastIndexOf(7)
$a = ["x""y""y""z"];

# returns FALSE if not found:
$i = array_search("y", $aTRUE);
none
a = ['x''y''y''z']

# raises ValueError if not found:
a.index('y')
none
a = %w(x y y z)

# return nil if not found:
a.index('y')
a.rindex('y')
slice
by endpoints, by length
// select 3rd and 4th elements:
["a", "b", "c", "d"].slice(2, 4)
none
# select 3rd and 4th elements:
none
array_slice($a, 2, 2)
# select 3rd and 4th elements:
a[2:4]
a[2:2 + 2]
# select 3rd and 4th elements:
a[2..3]
a[2, 2]
slice to end["a", "b", "c", "d"].slice(1)array_slice($a, 1)a[1:]a[1..-1]
manipulate backa = [6, 7, 8];
a.push(9);
i = a.pop();
$a = [6, 7, 8];
array_push($a, 9);
$a[] = 9; # same as array_push
array_pop($a);
a = [6, 7, 8]
a.append(9)
a.pop()
a = [6, 7, 8]
a.push(9)
<< 9 # same as push
a.pop
manipulate fronta = [6, 7, 8];
a.unshift(5);
i = a.shift();
$a = [6, 7, 8];
array_unshift($a, 5);
array_shift($a);
a = [6, 7, 8]
a.insert(0, 5)
a.pop(0)
a = [6, 7, 8]
a.unshift(5)
a.shift
concatenatea = [1, 2, 3].concat([4, 5, 6]);$a = [1, 2, 3];
$a2 = array_merge($a, [4, 5, 6]);
$a = array_merge($a, [4, 5, 6]);
a = [1, 2, 3]
a2 = a + [4, 5, 6]
a.extend([4, 5, 6])
a = [1, 2, 3]
a2 = a + [4, 5, 6]
a.concat([4, 5, 6])
replicatenone$a = array_fill(0, 10, NULL);a = [None] * 10
a = [None for i in range(0, 10)]
a = [nil] * 10
a = Array.new(10, nil)
copy
address copy, shallow copy, deep copy
a = [1, 2, [3, 4]];
a2 = a;
a3 = a.slice(0);
a4 = JSON.parse(JSON.stringify(a));
$a = [1, 2, [3, 4]];
$a2 =& $a;
none
$a4 = $a;
import copy

a = [1,2,[3,4]]
a2 = a
a3 = list(a)
a4 = copy.deepcopy(a)
a = [1,2,[3,4]]
a2 = a
a3 = a.dup
a4 = Marshal.load(Marshal.dump(a))
array as function argumentparameter contains address copyparameter contains deep copyparameter contains address copyparameter contains address copy
iterate over elements_.each([1, 2, 3], function(n) {
  alert(n);
})
foreach ([1, 2, 3] as $i) {
  echo "$i\n";
}
for i in [1, 2, 3]:
  print(i)
[1, 2, 3].each { |i| puts i }
iterate over indices and elementsvar len = a.length;
for (var i = 0; i < len; i++ ) {
  alert(a[i]);
}
$a = ["do""re""mi" "fa"];
foreach ($a as $i => $s) {
  echo "$s at index $i\n";
}
a = ['do''re''mi''fa']
for i, s in enumerate(a):
  print('%s at index %d' % (s, i))
a = %w(do re mi fa)
a.each_with_index do |s, i|
  puts "#{s} at index #{i}"
end
iterate over rangenot space efficient; use C-style for loopnot space efficient; use C-style for loop# use range() in Python 3:
for i in xrange(1, 1000001):
  code
(1..1_000_000).each do |i|
  code
end
instantiate range as arrayvar a = _.range(1, 10);$a = range(1, 10);a = range(1, 11)
Python 3:
a = list(range(1, 11))
a = (1..10).to_a
reverse
non-destructive, in-place
var a = [1, 2, 3];
a.reverse();
$a = [1, 2, 3];

array_reverse($a);
$a = array_reverse($a);
a = [1, 2, 3]

a[::-1]
a.reverse()
a = [1, 2, 3]

a.reverse
a.reverse!
sort
non-destructive,
in-place,
custom comparision
var a = [3, 1, 4, 2];
a.sort();
$a = ["b""A""a""B"];

none
sort($a);
none, but usort sorts in place
a = ['b''A''a''B']

sorted(a)
a.sort()
# custom binary comparision
# removed from Python 3:

a.sort(key=str.lower)
a = %w(b A a B)

a.sort
a.sort!
a.sort do |x, y|
  x.downcase <=> y.downcase
end
dedupe
non-destructive, in-place
var a = [1, 2, 2, 3];

var a2 = _.uniq(a);
a = _.uniq(a);
$a = [1, 2, 2, 3];

$a2 = array_unique($a);
$a = array_unique($a);
a = [1, 2, 2, 3]

a2 = list(set(a))
a = list(set(a))
a = [1, 2, 2, 3]

a2 = a.uniq
a.uniq!
membership_.contains(a, 7)in_array(7, $a)in aa.include?(7)
intersection_.intersection([1, 2], [2, 3, 4])$a = [1, 2];
$b = [2, 3, 4]
array_intersect($a, $b)
{1, 2} & {2, 3, 4}[1, 2] & [2 ,3, 4]
union_.union([1, 2], [2, 3, 4])$a1 = [1, 2];
$a2 = [2, 3, 4];
array_unique(array_merge($a1, $a2))
{1, 2} | {2, 3, 4}[1, 2] | [2, 3, 4]
relative complement, symmetric difference_.difference([1, 2, 3], [2])
none
$a1 = [1, 2, 3];
$a2 = [2];
array_values(array_diff($a1, $a2))
none
{1, 2, 3} - {2}
{1, 2} ^ {2, 3, 4}
require 'set'

[1, 2, 3] - [2]
Set[1, 2] ^ Set[2 ,3, 4]
map// callback gets 3 args:
// value, index, array

a.map(function(x) { return x * x })
array_map(function ($x) {
    return $x * $x;
  }, [1, 2, 3])
map(lambda x: x * x, [1, 2, 3])
# or use list comprehension:
[x * x for x in [1, 2, 3]]
[1, 2, 3].map { |o| o * o }
filtera.filter(function(x) { return x > 1 })array_filter([1, 2, 3],
  function ($x) {
    return $x>1;
  })
filter(lambda x: x > 1, [1, 2, 3])
# or use list comprehension:
[x for x in [1, 2, 3] if x > 1]
[1, 2, 3].select { |o| o > 1 }
reducea.reduce(function(m, o) {
    return m + o;
  }, 0)
array_reduce([1, 2, 3],
  function($x,$y) {
    return $x + $y;
  }, 0)
# import needed in Python 3 only
from functools import reduce

reduce(lambda x, y: x + y, [1, 2, 3], 0)
[1, 2, 3].inject(0) { |m, o| m + o }
universal and existential testsvar a = [1, 2, 3, 4];
var even = function(x) {
  return x % 2 == 0;
};

a.every(even)
a.some(even)
use array_filterall(i % 2 == 0 for i in [1, 2, 3, 4])
any(i % 2 == 0 for i in [1, 2, 3, 4])
[1, 2, 3, 4].all? {|i| i.even? }
[1, 2, 3, 4].any? {|i| i.even? }
shuffle and samplevar a = [1, 2, 3, 4];
a = _.shuffle(a);
var samp = _.sample([1, 2, 3, 4], 2);
$a = [1, 2, 3, 4];
shuffle($a);
$samp = array_rand(|[1, 2, 3, 4], 2);
from random import shuffle, sample

a = [1, 2, 3, 4]
shuffle(a)
samp = sample([1, 2, 3, 4], 2)
[1, 2, 3, 4].shuffle!
samp = [1, 2, 3, 4].sample(2)
flatten
one level, completely
var a = [1, [2, [3, 4]]];
var a2 = _.flatten(a, true);
var a3 = _.flatten(a);
nonenonea = [1, [2, [3, 4]]]
a2 = a.flatten(1)
a3 = a.flatten
zipvar a = _.zip(
  [1, 2, 3],
  ["a", "b", "c"]);
$a = array_map(NULL,
  [1, 2, 3],
  ["a""b""c"]);
a = zip([1, 2, 3], ['a''b''c'])a = [1, 2, 3].zip(["a""b""c"])
dictionaries
javascriptphppythonruby
literald = {"t": 1, "f": 0};
// keys do not need to be quoted if they
// are a legal JavaScript variable name
//and not a reserved word
$d = ["t" => 1, "f" => 0];

# older syntax:
$d = array("t" => 1, "f" => 0);
d = {'t': 1, 'f': 0}d = {'t' => 1, 'f' => 0}

# keys are symbols:
symbol_to_int = {t: 1, f: 0}
sizevar size = 0;
for (var k in d) {
  if (d.hasOwnProperty(k)) size++;
}
count($d)len(d)d.size
d.length # same as size
lookupd.t
d["t"]
$d["t"]d['t']d['t']
updated["t"] = 2;
d.t = 2;
$d["t"] = 2;d['t'] = 2d['t'] = 2
missing key behaviorvar d = {};
// sets s to undefined:
var s = d["lorem"];
// adds key/value pair:
d["lorem"] = "ipsum";
$d = [];
# sets $s to NULL:
$s = $d["lorem"];
# adds key/value pair:
$d["lorem"] = "ipsum";
d = {}
# raises KeyError:
s = d['lorem']
# adds key/value pair:
d['lorem'] = 'ipsum'
d = {}
# sets s to nil:
s = d['lorem']
# adds key/value pair:
d['lorem'] = 'ipsum'
is key presentd.hasOwnProperty("t");array_key_exists("y", $d);'y' in dd.key?('y')
deletedelete d["t"];
delete d.t;
$d = [1 => "t", 0 => "f"];
unset($d[1]);
d = {1: True, 0: False}
del d[1]
d = {1 => true, 0 => false}
d.delete(1)
from array of pairs, from even length arrayvar a = [["a", 1], ["b", 2], ["c", 3]];
var d = _.object(a);

none
a = [['a', 1], ['b', 2], ['c', 3]]
d = dict(a)

a = ['a', 1, 'b', 2, 'c', 3]
d = dict(zip(a[::2], a[1::2]))
a = [['a', 1], ['b', 2], ['c', 3]]
d = Hash[a]

a = ['a', 1, 'b', 2, 'c', 3]
d = Hash[*a]
mergevar d1 = {"a": 1, "b": 2};
var d2 = {"c": 3, "d": 4};
d1 = _.extend(d1, d2);
$d1 = ["a" => 1, "b" => 2];
$d2 = ["b" => 3, "c" => 4];
$d1 = array_merge($d1, $d2);
d1 = {'a': 1, 'b': 2}
d2 = {'b': 3, 'c': 4}
d1.update(d2)
d1 = {'a' => 1, 'b' => 2}
d2 = {'b' => 3, 'c' => 4}
d1.merge!(d2)
invertvar to_num = {'t': 1, 'f': 0};
var to_let = _.invert(to_num);
$to_num = ["t" => 1, "f" => 0];
$to_let = array_flip($to_num);
to_num = {'t': 1, 'f': 0}
# dict comprehensions added in 2.7:
to_let = {v: k for k, v
  in to_num.items()}
to_num = {'t' => 1, 'f' => 0}
to_let = to_num.invert
iteratefor (var k in d) {
  use k or d[k]
}
foreach ($d as $k => $v) {
  code
}
for k, v in d.iteritems():
  code

Python 3:
for k, v in d.items():
  code
d.each do |k,v|
  code
end
keys and values as arrays_.keys(d)
_.values(d)
array_keys($d)
array_values($d)
d.keys()
d.values()

Python 3:
list(d.keys())
list(d.values())
d.keys
d.values
sort by valuesfunction cmp2(a, b) {
  if (a[1] < b[1]) { return -1; }
  if (a[1] > b[1]) { return 1; }
  return 0;
}

for (p in _.pairs(d).sort(cmp2)) {
  alert(p[0] + ": " + p[1]);
}
asort($d);

foreach ($d as $k => $v) {
  print "$k: $v\n";
}
from operator import itemgetter

pairs = sorted(d.iteritems(),
  key=itemgetter(1))

for k, v in pairs:
  print('{}: {}'.format(k, v))
d.sort_by { |k, v| v }.each do |k, v|
  puts "#{k}: #{v}"
end
default value, computed valuenone$counts = [];
$counts['foo'] += 1;

extend ArrayObject for computed values and defaults other than zero or empty string.
from collections import defaultdict

counts = defaultdict(lambda: 0)
counts['foo'] += 1

class Factorial(dict):
  def __missing__(self, k):
    if k > 1:
      return k * self[k-1]
    else:
      return 1

factorial = Factorial()
counts = Hash.new(0)
counts['foo'] += 1

factorial = Hash.new do |h,k|
  k > 1 ? k * h[k-1] : 1
end
functions
javascriptphppythonruby
definefunction add(x, y) {
  return x+y;
}
function add3($x1, $x2, $x3)
{
  return $x1 + $x2 + $x3;
}
def add3(x1, x2, x3):
  return x1 + x2 + x3
def add3(x1, x2, x3)
  x1 + x2 + x3
end

# parens are optional and customarily
# omitted when defining functions
# with no parameters
invokeadd(1, 2)add3(1, 2, 3);

# function names are case insensitive:
ADD3(1, 2, 3);
add3(1, 2, 3)add3(1, 2, 3)

# parens are optional:
add3 1, 2, 3
missing argument behaviorset to undefinedset to NULL with warningraises TypeError if number of arguments doesn't match function arityraises ArgumentError if number of arguments doesn't match function arity
extra argument behaviorignoredignoredraises TypeError if number of arguments doesn't match function arityraises ArgumentError if number of arguments doesn't match function arity
default argumentnonefunction my_log($x, $base=10)
{
  return log($x) / log($base);
}

my_log(42);
my_log(42, M_E);
import math

def my_log(x, base=10):
  return math.log(x) / math.log(base)

my_log(42)
my_log(42, math.e)
def my_log(x, base=10)
  Math.log(x) / Math.log(base)
end

my_log(42)
my_log(42, Math::E)
variadic functionargs in arguments[0], arguments[1], … with number of args in arguments.lengthfunction first_and_last()
{

  $arg_cnt = func_num_args();

  if ($arg_cnt >= 1) {
    $n = func_get_arg(0);
    echo "first: " . $n . "\n";
  }

  if ($arg_cnt >= 2) {
    $a = func_get_args();
    $n = $a[$arg_cnt-1];
    echo "last: " . $n . "\n";
  }
}
def first_and_last(*a):

  if len(a) >= 1:
    print('first: ' + str(a[0]))

  if len(a) >= 2:
    print('last: ' + str(a[-1]))
def first_and_last(*a)

  if a.size >= 1
    puts "first: #{a[0]}"
  end

  if a.size >= 2
    puts "last: #{a[-1]}"
  end
end
pass array elements as separate argumentsnone$a = [1, 2, 3];

call_user_func_array("add3", $a);
a = [2, 3]

add3(1, *a)

# splat operator can only be used once
# and must appear after other
# unnamed arguments
a = [2, 3]

add3(1, *a)

# splat operator can be used multiple
# times and can appear before regular
# arguments
parameter aliasnonefunction first_and_second(&$a)
{
  return [$a[0], $a[1]];
}
nonenone
named parametersnonenonedef fequal(x, y, eps=0.01):
  return abs(x - y) < eps

fequal(1.0, 1.001)
fequal(1.0, 1.001, eps=0.1**10)
def fequal(x, y, opts={})
  eps = opts[:eps|| 0.01
  (x - y).abs < eps
end

fequal(1.0, 1.001)
fequal(1.0, 1.001, :eps=>0.1**10)

# Ruby 2.0:
def fequals(x, y, eps: 0.01)
  (x - y).abs < eps
end

fequals(1.0, 1.001)
fequals(1.0, 1.001, eps: 0.1**10)
return valuereturn arg or undefined. If invoked withnew and return value not an object, returnsthisreturn arg or NULLreturn arg or Nonereturn arg or last expression evaluated
multiple return valuesnonefunction first_and_second(&$a)
{
  return [$a[0], $a[1]];
}

$a = [1, 2, 3];
list($x, $y) =
  first_and_second($a);
def first_and_second(a):
  return a[0], a[1]

xy = first_and_second([1, 2, 3])
def first_and_second(a)
  return a[0], a[1]
end

x, y = first_and_second([1, 2, 3])
anonymous function literalvar sqr = function(x) { return x*x; }$sqr = function ($x) {
  return $x * $x;
};
# body must be an expression:
sqr = lambda x: x * x
sqr = lambda { |x| x * x }
invoke anonymous functionsqr(2)$sqr(2)sqr(2)sqr.call(2) or
sqr[2]
function as valuevar func = add;$func = "add";func = addfunc = lambda { |*args| add(*args) }
function with private statefunction counter() {
  counter.i += 1;
  return counter.i;
}

counter.i = 0;
alert(counter());
function counter()
{
  static $i = 0;
  return ++$i;
}

echo counter();
# state not private:
def counter():
  counter.i += 1
  return counter.i

counter.i = 0
print(counter())
none
closurefunction make_counter() {
  var i = 0;

  return function() {
    i += 1;
    return i;
  }
}
function make_counter()
{
  $i = 0;
  return function () use (&$i) {
    return ++$i;
  };
}

$nays = make_counter();
echo $nays();
# Python 3:
def make_counter():
  i = 0
  def counter():
    nonlocal i
    i += 1
    return i
  return counter

nays = make_counter()
def make_counter
  i = 0
  return lambda { i +=1; i }
end

nays = make_counter
puts nays.call
generator# PHP 5.5:
function make_counter() {
  $i = 0;
  while (1) {
    yield ++$i;
  }
}

$nays = make_counter();
# does not return a value:
$nays->next();
# runs generator if generator has not
# yet yielded:

echo $nays->current();
# The itertools library contains
# standard generators.
# c.f. itertools.count()


def make_counter():
  i = 0
  while True:
    i += 1
    yield i

nays = make_counter()
print(nays.next())
def make_counter
  return Fiber.new do
    i = 0
    while true
      i += 1
      Fiber.yield i
    end
  end
end

nays = make_counter
puts nays.resume
decoratordef logcall(f):
  def wrapper(*a, **opts):
    print('calling ' + f.__name__)
    f(*a, **opts)
    print('called ' + f.__name__)
  return wrapper

@logcall
def square(x):
  return x * x
invoke operator like functionimport operator

operator.mul(3, 7)

a = ['foo''bar''baz']
operator.itemgetter(2)(a)
3.*(7)

a = ['foo''bar''baz']
a.[](2)
execution control
javascriptphppythonruby
ifif (0 == n) {
  alert("no hits");
} else if (1 == n) {
  alert("1 hit");
} else {
  alert(n + " hits");
}
if ( 0 == $n ) {
  echo "no hits\n";
elseif ( 1 == $n ) {
  echo "one hit\n";
else {
  echo "$n hits\n";
}
if 0 == n:
  print('no hits')
elif 1 == n:
  print('one hit')
else:
  print(str(n) + ' hits')
if n == 0
  puts "no hits"
elsif 1 == n
  puts "one hit"
else
  puts "#{n} hits"
end
switchswitch (n) {
case 0:
  alert("no hits\n");
  break;
case 1:
  alert("one hit\n");
  break;
default:
  alert(n + " hits\n");
}
switch ($n) {
case 0:
  echo "no hits\n";
  break;
case 1:
  echo "one hit\n";
  break;
default:
  echo "$n hits\n";
}
nonecase n
when 0
  puts "no hits"
when 1
  puts "one hit"
else
  puts "#{n} hits"
end
whilewhile (i < 100) {
  i += 1;
}
while ( $i < 100 ) { $i++; }while i < 100:
  i += 1
while i < 100 do
  i += 1
end
forfor (var i = 0; i < 10; i++) {
  alert(i);
}
for ($i = 1; $i <= 10; $i++) {
  echo "$i\n";
}
nonenone
breakbreakbreakbreakbreak
continuecontinuecontinuecontinuenext
statement modifiersnonenonenoneputs "positive" if i > 0
puts "nonzero" unless i == 0
exceptions
javascriptphppythonruby
base exceptionAny value can be thrown.ExceptionBaseException

User-defined exceptions should subclassException.

In Python 2 old-style classes can be thrown.
Exception

User-defined exceptions should subclassStandardError.
predefined exceptionsError
  EvalError
  RangeError
  ReferenceError
  SyntaxError
  TypeError
  URIError
Exception
  LogicException
    BadFunctionCallException
      BadMethodCallException
    DomainException
    InvalidArgumentException
    LengthException
    OutOfRangeException
  RuntimeException
    OutOfBoundsException
    OverflowException
    RangeException
    UnderflowException
    UnexpectedValueException
BaseException
  SystemExit
  KeyboardInterrupt
  GeneratorExit
  Exception
    StopIteration
    StandardError
      BufferError
      ArithmeticError
        FloatingPointError
        OverflowError
        ZeroDivisionError
      AssertionError
      AttributeError
      EnvironmentError
       EOFError
      ImportError
      LookupError
        IndexError
        KeyError
      MemoryError
      NameError
      ReferenceError
      RuntimeError
        NotImplementedError
      SyntaxError
      SystemError
      TypeError
      ValueError
        UnicodeError

Python 3 has a different tree
Exception
  NoMemoryError
  ScriptError
    LoadError
    NotImplementedError
    SyntaxError
  SignalException
  StandardError
    ArgumentError
    IOError
      EOFError
    IndexError
    LocalJumpError
    NameError
    RangeError
    RegexpError
    RuntimeError
    SecurityError
    SocketError
    SystemCallError
      Errno::*
    SystemStackError
    ThreadError
    TypeError
    ZeroDivisionError
  SystemExit
  fatal
raise exceptionthrow new Error("bad arg");throw new Exception("bad arg");raise Exception('bad arg')# raises RuntimeError
raise "bad arg"
catch-all handlertry {
  risky();
}
catch (e) {
  alert("risky failed: " + e.message);
}
try {
  risky();
catch (Exception $e) {
  echo "risky failed: ",
    $e->getMessage(), "\n";
}
try:
  risky()
except:
  print('risky failed')
# catches StandardError
begin
  risky
rescue
  print "risky failed: "
  puts $!.message
end
re-raise exceptiontry {
  throw new Error("bam!");
}
catch (e) {
  alert("re-raising...");
  throw e;
}
try:
  raise Exception('bam!')
except:
  print('re-raising...')
  raise
begin
  raise "bam!"
rescue
  puts "re-raising…"
  raise
end
global variable for last exceptionnonenonelast exception: sys.exc_info()[1]last exception: $!
backtrace array of exc.: $@
exit status of child: $?
define exceptionfunction Bam(msg) {
  this.message = msg;
}

Bam.prototype = new Error;
class Bam extends Exception
{
  function __construct()
  {
    parent::__construct("bam!");
  }
}
class Bam(Exception):
  def __init__(self):
    super(Bam, self).__init__('bam!')
class Bam < Exception
  def initialize
    super("bam!")
  end
end
handle exceptiontry {
  throw new Bam("bam!");
}
catch (e) {
  if (e instanceof Bam) {
    alert(e.message);
  }
  else {
    throw e;
  }
}
try {
  throw new Bam;
catch (Bam $e) {
  echo $e->getMessage(), "\n";
}
try:
  raise Bam()
except Bam as e:
  print(e)
begin
  raise Bam.new
rescue Bam => e
  puts e.message
end
finally blockacquire_resource();
try {
  risky();
}
finally {
  release_resource();
}
PHP 5.5:
acquire_resource();
try {
  risky();
}
finally {
  release_resource();
}
acquire_resource()
try:
  risky()
finally:
  release_resource()
acquire_resource
begin
  risky
ensure
  release_resource
end
concurrency
javascriptphppythonruby
start threadnoneclass sleep10(threading.Thread):
  def run(self):
    time.sleep(10)

thr = sleep10()
thr.start()
thr = Thread.new { sleep 10 }
wait on threadthr.join()thr.join
________________________________________________________________________________________
streams
nodephppythonruby
standard file handlesprocess.stdin
process.stdout
process.stderr
only set by CLI; not set when reading script from stdin:
STDIN STDOUT STDERR
sys.stdin sys.stdout sys.stderr$stdin $stdout $stderr
read line from stdin$line = fgets(STDIN);line = sys.stdin.readline()line = gets
end-of-file behaviorreturns string without newline or FALSEreturns string without newline or ''returns non-empty string without newline or raises EOFError
chompchop($line);line = line.rstrip('\r\n')line.chomp!
write line to stdoutconsole.log("Hello, World");echo "Hello, World!\n";print('Hello, World!')puts "Hello, World!"
write formatted string to stdoutprintf("%.2f\n"M_PI);import math

print('%.2f' % math.pi)
printf("%.2f\n"Math::PI)
open file for reading$f = fopen("/etc/hosts""r");f = open('/etc/hosts')f = File.open("/etc/hosts")
open file for writingvar f = fs.openSync("/tmp/test", "w");$f = fopen("/tmp/test""w");f = open('/tmp/test''w')f = File.open("/tmp/test""w")
set file handle encodingimport codecs

fin = codecs.open('/tmp/foo',
  encoding='utf-8')

fout = codecs.open('/tmp/bar''w',
  encoding='utf-8')
fin = File.open("/tmp/foo""r:utf-8")

fout = File.open("/tmp/bar""w:utf-8")
open file for appending$f = fopen("/tmp/test""a");f = open('/tmp/err.log''a')f = File.open("/tmp/err.log""a")
close filefclose($f);f.close()f.close
close file implicitlynonewith open('/tmp/test''w'as f:
  f.write('lorem ipsum\n')
File.open("/tmp/test""w"do |f|
  f.puts("lorem ipsum")
end
i/o errorreturn false value and write warning to stderrraise IOError exceptionraise IOError or subclass ofSystemCallError exception
encoding errorraise UnicodeDecodeError on read; raiseUnicodeEncodeError on write
read line$line = fgets($f);f.readline()f.gets
iterate over file by linewhile (!feof($f)) {
  $line = fgets($f);
  echo $line;
}
for line in f:
  print(line)
f.each do |line|
  print(line)
end
read file into array of strings$a = file("/etc/hosts");a = f.readlines()a = f.lines.to_a
read file into stringvar fs = require('fs');

var s = fs.readFileSync('/etc/hosts', 'utf8');
$s = file_get_contents(
  "/etc/hosts");
s = f.read()s = f.read
write stringfwrite($f"lorem ipsum");f.write('lorem ipsum')f.write("lorem ipsum")
write linefwrite($f"lorem ipsum\n");f.write('lorem ipsum\n')f.puts("lorem ipsum")
flush file handleCLI output isn't buffered
fflush($f);
f.flush()f.flush
end-of-file testfeof($f)nonef.eof?
file handle position

get, set
ftell($f)
fseek($f, 0);
f.tell()
f.seek(0)
f.tell
f.seek(0)

f.pos
f.pos = 0
open temporary file$f = tmpfile();

fwrite($f"lorem ipsum\n");

# no way to get file name

fclose($f);

file is removed when file handle is closed
import tempfile

f = tempfile.NamedTemporaryFile()

f.write('lorem ipsum\n')

print("tmp file: %s" % f.name)

f.close()

file is removed when file handle is closed
require 'tempfile'

f = Tempfile.new('')

f.puts "lorem ipsum"

puts "tmp file: #{f.path}"

f.close

file is removed when file handle is garbage-collected or interpreter exits
in memory file$meg = 1024 * 1024;
$mem = "php://temp/maxmemory:$meg";
$f = fopen($mem"r+");
fputs($f"lorem ipsum");
rewind($f);
$s = fread($f, $meg);
from StringIO import StringIO

f = StringIO()
f.write('lorem ipsum\n')
s = f.getvalue()

Python 3 moved StringIO to the io module
require 'stringio'

f = StringIO.new
f.puts("lorem ipsum")
f.rewind
s = f.read
files
nodephppythonruby
file exists test, file regular testvar fs = require('fs');

var qry = fs.existsSync('/etc/hosts');

var stat = fs.statSync('/etc/hosts');
var qry2 = stat.isFile();
file_exists("/etc/hosts")
is_file("/etc/hosts")
os.path.exists('/etc/hosts')
os.path.isfile('/etc/hosts')
File.exists?("/etc/hosts")
File.file?("/etc/hosts")
file sizevar fs = require('fs');

var stat = fs.statSync('/etc/hosts');
var sz = stat.size;
filesize("/etc/hosts")os.path.getsize('/etc/hosts')File.size("/etc/hosts")
is file readable, writable, executableis_readable("/etc/hosts")
is_writable("/etc/hosts")
is_executable("/etc/hosts")
os.access('/etc/hosts', os.R_OK)
os.access('/etc/hosts', os.W_OK)
os.access('/etc/hosts', os.X_OK)
File.readable?("/etc/hosts")
File.writable?("/etc/hosts")
File.executable?("/etc/hosts")
set file permissionsvar fs = require('fs');

fs.chmodSync('/tmp/foo',
  parseInt('755', 8));
chmod("/tmp/foo", 0755);os.chmod('/tmp/foo', 0755)File.chmod(0755, "/tmp/foo")
last modification timevar fs = require('fs');

var stat = fs.statSync('/etc/hosts');
var dt = stat.mtime;
# unix epoch:
$t = stat('/etc/passwd')['mtime'];

# DateTime object:
$t2 = new DateTime('UTC');
$t2->setTimestamp($t);
from datetime import datetime as dt

# unix epoch:
t = os.stat('/etc/passwd').st_mtime

# datetime object:
t2 = dt.fromtimestamp(t)
# Time object:
t2 = File.stat('/etc/passwd').mtime

# unix epoch:
t = t2.to_i
copy file, remove file, rename file# npm install fs-extra
var fs = require('fs-extra');

fs.copySync('/tmp/foo', '/tmp/bar');
fs.unlinkSync('/tmp/foo');
fs.renameSync('/tmp/bar', '/tmp/foo');
copy("/tmp/foo""/tmp/bar");
unlink("/tmp/foo");
rename("/tmp/bar""/tmp/foo");
import shutil

shutil.copy('/tmp/foo''/tmp/bar')
os.remove('/tmp/foo')
shutil.move('/tmp/bar''/tmp/foo')
require 'fileutils'

FileUtils.cp("/tmp/foo""/tmp/bar")
FileUtils.rm("/tmp/foo")
FileUtils.mv("/tmp/bar""/tmp/foo")
create symlink, symlink test, readlinkvar fs = require('fs');

fs.symlinkSync('/etc/hosts',
  '/tmp/hosts');
var stat = fs.statSync('/tmp/hosts');
stat.isSymbolicLink();
var path = fs.readlinkSync(
  '/tmp/hosts');
symlink("/etc/hosts""/tmp/hosts");
is_link("/etc/hosts")
readlink("/tmp/hosts")
os.symlink('/etc/hosts''/tmp/hosts')
os.path.islink('/tmp/hosts')
os.path.realpath('/tmp/hosts')
File.symlink("/etc/hosts""/tmp/hosts")
File.symlink?("/etc/hosts")
File.realpath("/tmp/hosts")
generate unused file name$path = tempnam("/tmp""foo");
$f = fopen($path"w");
import tempfile

f, path = tempfile.mkstemp(
  prefix='foo',
  dir='/tmp')
file formats
nodephppythonruby
parse csvvar fs = require('fs');
# npm install csv
var csv = require('csv');

var s = fs.readFileSync('no-header.csv');
var a;
csv().from.string(s).to.array(function(d) { a = d });
$f = fopen("no-header.csv""r");
while (($row = fgetcsv($f)) != FALSE) {
  echo implode("\t", $row) . "\n";
}
import csv

with open('foo.csv'as f:
  cr = csv.reader(f)
  for row in cr:
    print('\t'.join(row))
require 'csv'

CSV.foreach("foo.csv"do |row|
  puts row.join("\t")
end
generate csv# npm install csv
var csv = require('csv');

var a = [['one', 'une', 'uno'],
['two', 'deux', 'dos']];

var s;
csv().from.array(a).to.string(function (o) { s = o; });
import csv

with open('foo.csv''w'as f:
  cw = csv.writer(f)
  cw.writerow(['one''une''uno'])
  cw.writerow(['two''deux''dos'])
require 'csv'

CSV.open("foo.csv""w"do |csv|
  csv << ["one""une""uno"]
  csv << ["two""deux""dos"]
end
parse jsonvar s1 = '{"t":1,"f":0}';
var d1 = JSON.parse(s1);
$s1 = '{"t":1,"f":0}';
$a1 = json_decode($s1TRUE);
import json

d = json.loads('{"t":1,"f":0}')
require 'json'

d = JSON.parse('{"t":1,"f":0}')
generate jsonvar d2 = {'t': 1, 'f': 0};
var s2 = JSON.stringify(d1);
$a2 = array("t" => 1, "f" => 0);
$s2 = json_encode($a2);
import json

s = json.dumps({'t': 1, 'f': 0})
require 'json'

s = {'t' => 1,'f' => 0}.to_json
parse yaml# sudo pip install PyYAML
import yaml

data = yaml.safe_load('{f: 0, t: 1}\n')
# sudo gem install safe_yaml
require 'safe_yaml'

data = YAML.safe_load("—-\nt: 1\nf: 0\n")
generate yaml# sudo pip install PyYAML
import yaml

s = yaml.safe_dump({'t': 1, 'f': 0})
# sudo gem install safe_yaml
require 'safe_yaml'

s = YAML.dump({'t' => 1, 'f' => 0})
parse xml
all nodes matching xpath query; first node matching xpath query
# npm install xmldom xpath
var dom = require('xmldom').DOMParser;
var xpath = require('xpath');

var xml = '<a><b><c ref="3">foo</c></b></a>';
var doc = new dom().parseFromString(xml);
var nodes = xpath.select('/a/b/c', doc);
nodes.length;
nodes[0].firstChild.data;
$xml = "<a><b><c ref='3'>foo</c></b></a>";

# returns NULL and emits warning if not
# well-formed:

$doc = simplexml_load_string($xml);

$nodes = $doc->xpath("/a/b/c");
echo count($nodes);
echo $nodes[0];

$node = $nodes[0];
echo $node;
echo $node["ref"];
from xml.etree import ElementTree

xml = '<a><b><c ref="3">foo</c></b></a>'

# raises xml.etree.ElementTree.ParseError
# if not well-formed:

doc = ElementTree.fromstring(xml)

nodes = doc.findall('b/c')
print(len(nodes))
print(nodes[0].text)

node = doc.find('b/c')
print(node.text)
print(node.attrib['ref'])
require 'rexml/document'
include REXML

xml = "<a><b><c ref='3'>foo</c></b></a>"

# raises REXML::ParseException if
# not well-formed:

doc = Document.new(xml)

nodes = XPath.match(doc,"/a/b/c")
puts nodes.size
puts nodes[0].text

node = XPath.first(doc,"/a/b/c")
puts node.text
puts node.attributes["ref"]
generate xml# npm install xmlbuilder
var builder = require('xmlbuilder');

var xml = builder.create('a').ele('b', {id: 123}, 'foo').end();
$xml = "<a></a>";
$sxe = new SimpleXMLElement($xml);
$b = $sxe->addChild("b""foo");
$b->addAttribute("id", "123");

# <a><b id="123">foo</b></a>:
echo $sxe->asXML();
import xml.etree.ElementTree as ET

builder = ET.TreeBuilder()
builder.start('a', {})
builder.start('b', {'id': '123'})
builder.data('foo')
builder.end('b')
builder.end('a')
et = builder.close()

# <a><b id="123">foo</b></a>:
print(ET.tostring(et))
# gem install builder
require 'builder'

builder = Builder::XmlMarkup.new
xml = builder.a do |child|
  child.b("foo", :id=>"123")
end

# <a><b id="123">foo</b></a>:
puts xml
parse html$html = file_get_contents("foo.html");
$doc = new DOMDocument;
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);

$nodes = $xpath->query("//a/@href");
foreach($nodes as $href) {
  echo $href->nodeValue;
}
# pip install beautifulsoup4
import bs4

html = open('foo.html').read()
doc = bs4.BeautifulSoup(html)

for link in doc.find_all('a'):
  print(link.get('href'))
# gem install nokogiri
require 'nokogiri'

html = File.open("foo.html").read
doc = Nokogiri::HTML(html)
doc = doc.xpath("//a").each do |link|
  puts link["href"]
end
directories
nodephppythonruby
working directoryvar old_dir = process.cwd();

process.chdir("/tmp");
$old_dir = getcwd();

chdir("/tmp");
old_dir = os.path.abspath('.')

os.chdir('/tmp')
old_dir = Dir.pwd

Dir.chdir("/tmp")
build pathname"/etc" . DIRECTORY_SEPARATOR . "hosts"os.path.join('/etc''hosts')File.join("/etc""hosts")
dirname and basenamedirname("/etc/hosts")
basename("/etc/hosts")
os.path.dirname('/etc/hosts')
os.path.basename('/etc/hosts')
File.dirname("/etc/hosts")
File.basename("/etc/hosts")
absolute pathname
and tilde expansion
# file must exist; symbolic links are
# resolved:

realpath("foo")
realpath("/foo")
realpath("../foo")
realpath("./foo")
# no function for tilde expansion
# symbolic links are not resolved:
os.path.abspath('foo')
os.path.abspath('/foo')
os.path.abspath('../foo')
os.path.abspath('./foo')
os.path.expanduser('~/foo')
# symbolic links are not resolved:
File.expand_path("foo")
File.expand_path("/foo")
File.expand_path("../foo")
File.expand_path("./foo")
File.expand_path("~/foo")
iterate over directory by fileif ($dir = opendir("/etc")) {
  while ($file = readdir($dir)) {
    echo "$file\n";
  }
  closedir($dir);
}
for filename in os.listdir('/etc'):
  print(filename)
Dir.open("/etc").each do |file|
  puts file
end
glob pathsforeach (glob("/etc/*"as $file) {
  echo "$file\n";
}
import glob

for path in glob.glob('/etc/*'):
  print(path)
Dir.glob("/etc/*").each do |path|
  puts path
end
make directorymkdir("/tmp/foo/bar", 0755, TRUE);dirname = '/tmp/foo/bar'
if not os.path.isdir(dirname):
  os.makedirs(dirname)
require 'fileutils'

FileUtils.mkdir_p("/tmp/foo/bar")
recursive copynoneimport shutil

shutil.copytree('/tmp/foodir',
  '/tmp/bardir')
require 'fileutils'

FileUtils.cp_r("/tmp/foodir",
  "/tmp/bardir")
remove empty directoryrmdir("/tmp/foodir");os.rmdir('/tmp/foodir')File.rmdir("/tmp/foodir")
remove directory and contentsnoneimport shutil

shutil.rmtree('/tmp/foodir')
require 'fileutils'

FileUtils.rm_rf("/tmp/foodir")
directory testis_dir("/tmp")os.path.isdir('/tmp')File.directory?("/tmp")
generate unused directoryimport tempfile

path = tempfile.mkdtemp(dir='/tmp',
  prefix='foo')
require 'tmpdir'

path = Dir.mktmpdir("/tmp/foo")
system temporary file directorysys_get_temp_dir()import tempfile

tempfile.gettempdir()
require 'tmpdir'

Dir.tmpdir
processes and environment
nodephppythonruby
command line arguments
and script name
process.argv.slice(2)
process.argv[1]
// process.argv[0] contains "node"
$argv
$_SERVER["SCRIPT_NAME"]
sys.argv[1:]
sys.argv[0]
ARGV
$PROGRAM_NAME
environment variable

get, set
process.env["HOME"]

process.env["PATH"] = "/bin";
getenv("HOME")

putenv("PATH=/bin");
os.getenv('HOME')

os.environ['PATH'] = '/bin'
ENV["HOME"]

ENV["PATH"] = "/bin"
get pid, parent pidprocess.pid
none
posix_getpid()
posix_getppid()
os.getpid()
os.getppid()
Process.pid
Process.ppid
user id and name$uid = posix_getuid();
$uinfo = posix_getpwuid($uid);
$username = $uinfo["name"];
import getpass

os.getuid()
getpass.getuser()
require 'etc'

Process.uid
Etc.getpwuid(Process.uid)["name"]
exitprocess.exit(0);exit(0);sys.exit(0)exit(0)
set signal handlerimport signal

def handler(signo, frame):
  print('exiting...')
  sys.exit(1)

signal.signal(signal.SIGINT, handler)
Signal.trap("INT",
  lambda do |signo|
    puts "exiting..."
    exit 1
  end
)
executable testis_executable("/bin/ls")os.access('/bin/ls', os.X_OK)File.executable?("/bin/ls")
external commandsystem("ls -l /tmp", $retval);
if ($retval) {
  throw new Exception("ls failed");
}
if os.system('ls -l /tmp'):
  raise Exception('ls failed')
unless system("ls -l /tmp")
  raise "ls failed"
end
shell-escaped external command$path = chop(fgets(STDIN));
$safe = escapeshellarg($path);
system("ls -l " . $safe, $retval);
if ($retval) {
  throw new Exception("ls failed");
}
import subprocess

cmd = ['ls''-l''/tmp']
if subprocess.call(cmd):
  raise Exception('ls failed')
path = gets
path.chomp!
unless system("ls""-l", path)
  raise "ls failed"
end
command substitution$files = `ls -l /tmp`;import subprocess

cmd = ['ls''-l''/tmp']
files = subprocess.check_output(cmd)
files = `ls -l /tmp`
unless $?.success?
  raise "ls failed"
end

files = %x(ls)
unless $?.success?
  raise "ls failed"
end
option parsing
nodephppythonruby
command line options
boolean option, option with argument, usage
// $ npm install commander
program = require('commander');

program.option('-f, —file <file>')
  .option('-v, —verbose')
  .parse(process.argv);

var file = program.file;
var verbose = program.verbose;

// The flags -h and --help are generated
// automatically.
//
// Processing stops at the first positional
// argument.
//
// Positional arguments are available
// in program.args.
$usage = "usage: " .
  $_SERVER["SCRIPT_NAME"] .
  " [-f FILE] [-v] [ARG ...]\n";

$opts = getopt("f:hv",
  array("file:""help""verbose"));

if (array_key_exists("h", $opts||
    array_key_exists("help", $opts)) {
  echo $usage;
  exit(0);
}

$file = $opts["f"] ? $opts["f"] :
  $opts["file"];

if (array_key_exists("v", $opts||
    array_key_exists("verbose", $opts)) {
  $verbose = TRUE;
}

# Processing stops at first positional
# argument.
#
# Unrecognized options are ignored.
# An option declared to have an
# argument is ignored if the argument
# is not provided on the command line.
#
# getopt() does not modify $argv or
# provide means to identify positional
# arguments.
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('positional_args',
  nargs='*',
  metavar='ARG')
parser.add_argument('--file''-f',
  dest='file')
parser.add_argument('--verbose''-v',
  dest='verbose',
  action='store_true')

args = parser.parse_args()

the_file = args.file
verbose = args.verbose

# The flags -h and --help and the
# usage message are generated
# automatically.
#
# Positional arguments are in
# args.positional_args
#
# Options can follow positional
# arguments.
require 'optparse'

options = {}
OptionParser.new do |opts|
  opts.banner =
    "usage: #{$0} [OPTIONS] [ARG ...]"

  opts.on("-f""--file FILE"do |arg|
    options[:file] = arg
  end

  opts.on("-v""--verbose"do |arg|
    options[:verbose] = arg
  end
end.parse!

file = options[:file]
verbose = options[:verbose]

# The flags -h and --help and the
# usage message are generated
# automatically.
#
# After calling OptionParser.parse! only
# positional arguments are in ARGV.
#
# Options can follow positional args.
libraries and namespaces
nodephppythonruby
load libraryvar foo = require('./foo.js');

var foo = require('foo');
require_once("foo.php");# searches sys.path for foo.pyc or foo.py:
import foo
require 'foo.rb'

# searches $LOAD_PATH for foo.rb, foo.so,
# foo.o, foo.dll:

require 'foo'
load library in subdirectoryvar bar = require('./foo/bar.js');require_once('foo/bar.php');# foo must contain __init__.py file
import foo.bar
require 'foo/bar.rb'

require 'foo/bar'
hot patchdelete require.cache[require.resolve('./foo.js')];
var foo = require('./foo.js');
require("foo.php");reload(foo)load 'foo.rb'
load errorraises Errror exceptionrequire and require_once raise fatal error if library not found; include andinclude_once emit warningsraises ImportError if library not found; exceptions generated when parsing library propagate to clientraises LoadError if library not found; exceptions generated when parsing library propagate to client
main routine in libraryif (require.main == module) {
  code
}
noneif __name__ == '__main__':
  code
if $PROGRAM_NAME == __FILE__
  code
end
library pathnone$libpath = ini_get("include_path");

ini_set("include_path",
  $libpath . ":/some/path");
sys.path

sys.path.append('/some/path')
# $: is synonym for $LOAD_PATH:
$LOAD_PATH

$LOAD_PATH
 << "/some/path"
library path environment variable$ NODE_PATH=~/lib node foo.jsnone$ PYTHONPATH=~/lib python foo.py$ RUBYLIB=~/lib ruby foo.rb
library path command line optionnonenonenone$ ruby -I ~/lib foo.rb
simple global identifiersvariables defined outside of functions or with global keywordbuilt-in functionsvariables which start with $
multiple label identifiersclasses, interfaces, functions, and constantsmodulesconstants, classes, and modules
label separator\Foo\Bar\baz();foo.bar.baz()Foo::Bar.baz
root namespace definition\foonone# outside of class or module; only
# constants in root namespace:

FOO = 3

# inside class or module:
::FOO = 3
namespace declarationnamespace Foo;put declarations in foo.pyclass Foo
  # class definition
end

module Foo
  # module definition
end
child namespace declarationnamespace Foo\Bar;foo must be in sys.path:
$ mkdir foo
$ touch foo/__init__.py
$ touch foo/bar.py
module Foo::Bar
  # module definitions
end

module Foo
  module Bar
    # module definitions
  end
end

# classes can nest inside classes or
# modules; modules can nest in classes
import definitionsonly class names can be importedfrom foo import bar, baznone
import all definitions in namespacenonefrom foo import *# inside class or module:
include Foo
import all subnamespaces# subnamespaces in list __all__ of
foo/__init__.py are imported

from foo import *
shadow-avoidanceuse Foo as Fu;# rename namespace:
import foo as fu

# rename identifier:
from sys import path as the_path
Fu = Foo.dup

include Fu
list installed packages, install a package$ pear list
$ pear install Math_BigInteger
$ pip freeze
$ pip install jinja2
$ gem list
$ gem install rails
package specification formatin setup.py:

#!/usr/bin/env python

from distutils.core import setup

setup(
  name='foo',
  author='Joe Foo',
  version='1.0',
  description='a package',
  py_modules=['foo'])
in foo.gemspec:

spec = Gem::Specification.new do |s|
  s.name = "foo"
  s.authors = "Joe Foo"
  s.version = "1.0"
  s.summary = "a gem"
  s.files = Dir["lib/*.rb"]
end
objects
javascriptphppythonruby
define classfunction Int(i) {
  this.value = i === undefined ? 0 : i;
}
class Int
{
  public $value;
  function construct($int=0)
  {
    $this->value = $int;
  }
}
class Int(object):
  def __init__(self, v=0):
    self.value = v
class Int
  attr_accessor :value
  def initialize(i=0)
    @value = i
  end
end
create objectvar i = new Int();
var i2 = new Int(7);
$i = new Int();
$i2 = new Int(7);
i = Int()
i2 = Int(7)
i = Int.new
i2 = Int.new(7)
instance variable visibilitypublicvisibility must be declaredpublic; attributes starting with underscore private by conventionprivate by default; use attr_reader, attr_writer, attr_accessor to make public
get and set instance variablevar v = i.value;
i.value = v + 1;
$v = $i->value;
$i->value = $v + 1;
v = i.value
i.value = v + 1
v = i.value
i.value = v + 1
define method// inside constructor:
this.plus = function(v) {
  return this.value + v;
};

// outside constructor:
Int.prototype.plus = function (v) {
  return this.value + v;
}
function plus($i)
{
  return $this->value + $i;
}
def plus(self,v):
  return self.value + v
def plus(i)
  value + i
end
invoke methodi.plus(3);$i->plus(7)i.plus(7)i.plus(7)
define class method@classmethod
def get_instances(cls):
  return Counter.instances
class Foo
  def Foo.one
    puts "one"
  end
end
invoke class methodCounter::getInstances()Counter.get_instances()Foo.one
define class variableclass Foo:
  instances = 0
class Foo
  @@instances = 1
end
get and set class variableclass Foo:
  def __init(self):
    Foo.instances += 1
class Foo
  def initialize
    @@instances += 1
  end
end
handle undefined method invocationfunction __call($name, $args)
{
  $argc = count($args);
  echo "no def: $name " .
    "arity: $argc\n";
}
def __getattr__(self, name):
  s = 'no def: ' + name + ' arity: %d'

  return lambda *a: print(s % len(a))
def method_missing(name, *a)
  puts "no def: #{name}" +
    " arity: #{a.size}"
end
alias methodclass Point

  attr_reader :x:y:color

  alias_method :colour:color

  def initialize(x, y, color=:black)
    @x@y = x, y
    @color = color
  end
end
destructorfunction __destruct()
{
  echo "bye, $this->value\n";
}
def __del__(self):
  print('bye, %d' % self.value)
val = i.value
ObjectSpace.define_finalizer(int) {
  puts "bye, #{val}"
}
inheritance and polymorphism
javascriptphppythonruby
subclassclass Counter extends Int
{
  private static $instances = 0;
  function __construct($int=0)
  {
    Counter::$instances += 1;
    parent::__construct($int);
  }
  function incr()
  {
    $this->value++;
  }
  static function getInstances()
  {
    return $instances;
  }
}
class Counter(Int):

  instances = 0

  def __init__(self, v=0):
    Counter.instances += 1
    Int.__init__(self, v)

  def incr(self):
    self.value += 1
class Counter < Int

  @@instances = 0

  def initialize
    @@instances += 1
    super
  end

  def incr
    self.value += 1
  end

  def self.instances
    @@instances
  end
end
mixin
overload operatorclass Fixnum
  def /(n)
    self.fdiv(n)
  end
end
reflection
javascriptphppythonruby
object idnoneid(o)o.object_id
inspect typetypeof([]) === 'object'gettype(array()) == "array"

returns object for objects
type([]) == list[].class == Array
basic typesnumber
string
boolean
undefined
function
object

# these evaluate as 'object':
typeof(null)
typeof([])
typeof({})
NULL
boolean
integer
double
string
array
object
resource
unknown type
NoneType
bool
int
long
float
str
SRE_Pattern
datetime
list
array
dict
object
file
NilClass
TrueClass
FalseClass
Fixnum
Bignum
Float
String
Regexp
Time
Array
Hash
Object
File
inspect class// returns prototype object:
Object.getPrototypeOf(o)
returns FALSE if not an object:
get_class($o) == "Foo"
o.__class__ == Foo
isinstance(o, Foo)
o.class == Foo
o.instance_of?(Foo)
inspect class hierarchyvar pa = Object.getPrototypeOf(o)
//prototype's of prototype object:
var grandpa = Object.getPrototypeOf(pa)
get_parent_class($o)o.__class__.__bases__o.class.superclass
o.class.included_modules
has method?o.reverse && typeof(o.reverse) === 'function'method_exists($o"reverse")hasattr(o, 'reverse')o.respond_to?("reverse")
message passingnot a standard featurefor ($i = 1; $i <= 10; $i++) {
  call_user_func(array($o,
    "phone$i"), NULL);
}
for i in range(1,10):
  getattr(o, 'phone'+str(i))(None)
(1..9).each do |i|
  o.send("phone#{i}="nil)
end
evaleval('1 + 1')eval evaluates to argument of returnstatement or NULL:
while ($line = fgets(STDIN)) {
  echo eval($line) . "\n";
}
argument of eval must be an expression:
while True:
  print(eval(sys.stdin.readline()))
loop do
  puts eval(gets)
end
list object methodsget_class_methods($o)[m for m in dir(o)
  if callable(getattr(o,m))]
o.methods
list object attributesget_object_vars($o)dir(o)o.instance_variables
list loaded libraries# relative to directory in lib path:
$LOADED_FEATURES
$"
list loaded namespacesdir()Class.constants.select do |c|
  Module.const_get(c).class == Class
end
inspect namespaceimport urlparse

dir(urlparse)
require 'uri'

URI.constants
URI.methods
URI.class_variables
pretty-print$d = array("lorem"=>1,
  "ipsum"=>array(2,3));

print_r($d);
import pprint

d = {'lorem':1, 'ipsum':[2,3]}

pprint.PrettyPrinter().pprint(d)
require 'pp'

d = {'lorem' => 1, 'ipsum' => [2, 3]}

pp d
source line number and file name__LINE__
__FILE__
import inspect

cf = inspect.currentframe()
cf.f_lineno
cf.f_code.co_filename
__LINE__
__FILE__
command line documentationnone$ pydoc math
$ pydoc math.atan2
$ ri -c
$ ri Math
$ ri Math.atan2
net and web
javascriptphppythonruby
get local hostname, dns lookup, reverse dns lookup$host = gethostname();
$ip = gethostbyname($host);
$host2 = gethostbyaddr($ip);
import socket

host = socket.gethostname()
ip = socket.gethostbyname(host)
host2 = socket.gethostbyaddr(ip)[0]
require 'socket'

hostname = Socket.gethostname

ip = Socket.getaddrinfo(
  Socket.gethostname,
  "echo")[0][3]

host2 = Socket.gethostbyaddr(ip)[0]
http get$url = 'http://www.google.com';
$s = file_get_contents($url);
import httplib

url = 'www.google.com'
conn = httplib.HTTPConnection(url)
conn.request("GET"'/')
resp = conn.getresponse()
if resp.status == httplib.OK:
  s = resp.read()
require 'net/http'

url = "www.google.com"
r = Net::HTTP.start(url, 80) do |f|
  f.get("/")
end
if r.code == "200"
  s = r.body
end
http postimport httplib
import urllib

url = 'www.acme.com'
conn = httplib.HTTPConnection(url)
data = urllib.urlencode({
  'item''anvil',
  'qty': 1})
conn.request('POST''/orders', data)
resp = conn.getresponse()
if resp.status == httplib.OK:
  s = resp.read()
serve working directory$ php -S localhost:8000$ python -m SimpleHTTPServer 8000$ ruby -rwebrick -e \
'WEBrick::HTTPServer.new(:Port => 8000, '\
':DocumentRoot => Dir.pwd).start'
absolute url
from base and relative url
noneimport urlparse

urlparse.urljoin('http://google.com',
  'analytics')
require 'uri'

URI.join("http://google.com""analytics")
parse url$url = "http://google.com:80/foo?q=3#bar";
$up = parse_url($url);

$protocol = $up["scheme"];
$hostname = $up["host"];
$port = $up["port"];
$path = $up["path"];
$query_str = $up["query"];
$fragment = $up["fragment"];

# $params is associative array; if keys
# are reused, later values overwrite
# earlier values

parse_str($query_str, $params);
# Python 3 location: urllib.parse
import urlparse

url = 'http://google.com:80/foo?q=3#bar'
up = urlparse.urlparse(url)

protocol = up.scheme
hostname = up.hostname
port = up.port
path = up.path
query_str = up.query
fragment = up.fragment

# returns dict of lists:
params = urlparse.parse_qs(query_str)
require 'uri'

url = "http://google.com:80/foo?q=3#bar"
up = URI(url)

protocol = up.scheme
hostname = up.host
port = up.port
path = up.path
query_str = up.query
fragment = up.fragment

# Ruby 1.9; returns array of pairs:
params = URI.decode_www_form(query_str)
url encode/decodeurlencode("lorem ipsum?")
urldecode("lorem+ipsum%3F")
# Python 3 location: urllib.parse
import urllib

urllib.quote_plus("lorem ipsum?")
urllib.unquote_plus("lorem+ipsum%3F")
require 'cgi'

CGI::escape("lorem ipsum?")
CGI::unescape("lorem+ipsum%3F")
html escape
escape character data, escape attribute value, unescape html entities
$s = htmlspecialchars("<>&");

$s2 = htmlspecialchars("<>&\"'",
  ENT_NOQUOTES | ENT_QUOTES);

$s3 = htmlspecialchars_decode($s2);
import cgi
from HTMLParser import HTMLParser

s = cgi.escape('<>&')
s2 = cgi.escape('<>&"', True)
s3 = HTMLParser().unescape(
require 'cgi'

s2 = CGI.escapeHTML('<>&"')
s3 = CGI.unescapeHTML(s2)
base64 encode/decode$s = file_get_contents("foo.png");
$b64 = base64_encode($s);
$s2 = base64_decode($b64);
import base64

s = open('foo.png').read()
b64 = base64.b64encode(s)
s2 = base64.b64decode(b64)
require 'base64'

s = File.open("foo.png").read
b64 = Base64.encode64(s)
s2 = Base64.decode64(b64)
gui
nodephppythonruby
pop-up boxvar app = Application.currentApplication()
app.includeStandardAdditions = true
app.displayAlert('hi world')
unit tests
nodephppythonruby
test class// npm install -g nodeunit

exports.testFoo = function(test) {
  test.ok(true, 'not true!.');
  test.done();
}
# pear install pear.phpunit.de/PHPUnit

<?php

Class FooTest extends
  PHPUnit_Framework_TestCase
{
  public function test_01()
  {
    $this->assertTrue(true,
      "not true!");
  }
}
?>
import unittest

class TestFoo(unittest.TestCase):
  def test_01(self):
    self.assertTrue(True'not True!')

if __name__ == '__main__':
  unittest.main()
require 'test/unit'

class TestFoo < Test::Unit::TestCase
  def test_01
    assert(true"not true!")
  end
end
run tests, run test method$ nodeunit test_foo.js

$ nodeunit -t testFoo test_foo.js
$ phpunit test_foo.php

$ phpunit --filter test_01 test_foo.php
$ python test_foo.py
$ python test_foo.py TestFoo.test_01
$ ruby test_foo.rb
$ ruby test_foo.rb -n test_01
equality assertionvar s = 'do re mi';
test.equals(s, 'do re mi');
$s = "do re me";
$this->assertEquals($s"do re mi");

# also asserts args have same type:
$this->assertSame($s"do re mi");
s = 'do re me'
self.assertEqual('do re me',
  s,
  's: {}'.format(s))
s = "do re me"
assert_equal("do re me", s)
approximate assertion$x = 10.0 * (1.0 / 3.0);
$y = 10.0 / 3.0;

$this->assertEquals($x, $y,
  "not within delta",
  pow(0.1, 6));
x = 10.0 * (1.0 / 3.0)
y = 10.0 / 3.0

# default for delta is 0.1**7
self.assertAlmostEqual(x, y, delta=0.1**6)
x = 10.0 * (1.0 / 3.0)
y = 10.0 / 3.0

# default for delta is 0.001
assert_in_delta(x, y, 0.1**6)
regex assertion$s = "lorem ipsum";
$this->assertRegExp("/lorem/", $s);
s = 'lorem ipsum'
# uses re.search, not re.match:
self.assertRegexpMatches(s, 'lorem')
s = "lorem ipsum"
assert_match(/lorem/, s)
exception assertionclass Bam extends Exception {};

public function test_exc {
  $this->SetExpectedException("Bam");
  throw new Bam("bam!");
}
a = []
with self.assertRaises(IndexError):
  a[0]
assert_raises(ZeroDivisionErrordo
  1 / 0
end
mock method$mock = $this->getMock('Foo', ['foo']);
$mock->expects($this->once())
  ->method('foo')
  ->with(13)
  ->will($this->returnValue(7));

$mock->foo(13);
# pip install mock
import mock

foo = Foo()
foo.run = mock.MagicMock(return_value=7)

self.assertEqual(7, foo.run(13))
foo.run.assert_called_once_with(13)
# gem install mocha
require 'mocha'

foo = mock()
foo.expects(:run).returns(7).with(13).once

foo.run(13)
setupexports.setUp = function(callback) {
  console.log('setting up...');
  callback();
}
public function setUp()
{
  echo "setting up\n";
}
# in class TestFoo:
def setUp(self):
  print('setting up')
# in class TestFoo:
def setup
  puts "setting up"
end
teardownexports.tearDown = function(callback) {
  console.log('tearing down...');
  callback();
}
public function tearDown()
{
  echo "tearing down\n";
}
# in class TestFoo:
def tearDown(self):
  print('tearing down')
# in class TestFoo:
def teardown
  puts "tearing down"
end
debugging and profiling
javascriptphppythonruby
check syntax$ php -l foo.phpimport py_compile

# precompile to bytecode:
py_compile.compile('foo.py')
$ ruby -c foo.rb
flags for stronger and strongest warningsnone$ python -t foo.py
$ python -3t foo.py
$ ruby -w foo.rb
$ ruby -W2 foo.rb
lint$ npm install jshint
$ ./node_modules/jshint/bin/jshint foo.js
$ sudo pip install pylint
$ pylint foo.py
$ sudo gem install rubocop
$ rubocop -D foo.rb
source cleanup$ sudo pip install pep8
$ pep8 foo.py
$ sudo gem install rubocop
$ rubocop -D foo.rb
run debugger$ python -m pdb foo.py$ sudo gem install ruby-debug
$ rdebug foo.rb
debugger commandsh l n s b c w u d p qh l n s b c w u down p q
benchmark codeimport timeit

timeit.timeit('i += 1',
  'i = 0',
  number=1000000)
require 'benchmark'

n = 1_000_000
i = 0
puts Benchmark.measure do
  n.times { i += 1 }
end
profile code$ python -m cProfile foo.py$ sudo gem install ruby-prof
$ ruby-prof foo.rb
java interoperation
javascriptphppythonruby
versionJython 2.5
compatible with Python 2.5 :(
JRuby 1.7
compatible with Ruby 1.9
repl$ jython$ jirb
interpreter$ jython$ jruby
compilernone$ jrubyc
prologueimport javanone
newrnd = java.util.Random()rnd = java.util.Random.new
methodrnd.nextFloat()rnd.next_float
importfrom java.util import Random

rnd = Random()
java_import java.util.Random
rnd = Random.new
non-bundled java librariesimport sys

sys.path.append('path/to/mycode.jar')
import MyClass
require 'path/to/mycode.jar'
shadowing avoidanceimport java.io as javaiomodule JavaIO
  include_package "java.io"
end
convert native array to java arrayimport jarray

jarray.array([1, 2, 3], 'i')
[1, 2, 3].to_java(Java::int)
are java classes subclassable?yesyes
are java class open?noyes
________________________________________________________________________________________________________________________________________________________________________________