| perl | ruby |
perldoc YAML | ri YAML |
"value of x is $x\n" |
"value of x is #{x}\n" |
qq(value of x is $x\n); |
%(value of x is #{x}\n)
# or
%Q(value of x is #{x}\n)
|
q(no variable interpolation);
q{no variable interpolation};
q!no variable interpolation!; |
%q(no variable interpolation)
%q{no variable interpolation}
%q!no variable interpolation! |
my @x = ( 1, 'two', 3.14 ); |
x = [ 1, 'two', 3.14 ] |
my @x = qw(one two three); |
x = %w(one two three) |
@x = (); |
x = []
# or
x = Array.new |
my %x = ( one => 1, two => 2, three => 3); |
x = { 'one' => 1, 'two' => 2, 'three' => 3 }
(see also Hash.new)
|
undef | nil |
if ($x > 0) {
# something
}
elsif ($y > 1) {
# something
}
else {
# something
}
|
if x > 0
# something
elsif y > 1
# something
else
# something
end
|
while (($x < 10) && (y > 0)) {
# something
}
|
while x < 10 and y > 0
# something
end
|
my $n = 10;
do { print "$n\n" } until --$n < 1; |
n = 10
begin
puts(n)
n -= 1
end until n < 1 |
print "$x\n" if $x > 10; |
print "#{x}\n" if x > 10
# or
puts x if x > 10
|
my $x = chr(65);
my $y = ord('A');
|
x = 65.chr
y = ?A
|
++$x;
--$y;
|
x += 1
y -= 1
|
if ($x =~ /^something$/) {
# something
}
|
if x =~ /^something$/
# something
end
# or
if /^something$/.match(x)
# something
end
|
lc('Some String');
# or
"\LSome String";
|
'Some String'.downcase |
uc('Some String');
# or
"\USome String";
|
'Some String'.upcase |
$ENV{HOME} |
ENV["HOME"] |
print "$_ = $x{$_}\n" for(keys %x); |
x.each_key do |v|
print "#{v} = #{x[v]}\n"
end
|
qr(this is a regex)
qr{this is another regex}
qr!this is yet another regex!
|
%r(this is a regex)
%r{this is another regex}
%r!this is yet another regex!
|
chomp($x) | x.chomp! |
$x =~ s/pattern/replacement/ |
x.sub!(/pattern/, 'replacement')
|
$x =~ s/pattern/replacement/g |
x.gsub!(/pattern/, 'replacement')
|
my $s = ' string with leading and trailing whitespace ';
for ($s) {
s/^\s+//;
s/\s+$//;
}
|
s = ' string with leading and trailing whitespace '
s.sub!(/^\s+/, '').sub!(/\s+$/, '')
|
-e '/some/file' |
test(?e, '/some/file')
# or
File.exist?('/some/file')
|
print "not a dir\n" unless -d '/x' |
puts 'not a dir' if File.stat('/x').directory? == false
|
my $f = sub { $_[0] * 2 };
my $double = $f->(8);
|
f = lambda { |n| n * 2 }
double = f.call(8)
|
sub counter {
my $n = shift;
sub { ++$n }
}
my $c = counter(5);
my $d = counter(2);
print $c->(), "\t" $d->(), "\n"\n for (0..3)
|
def counter(n)
lambda { n += 1 }
end
c = counter(5)
d = counter(2)
(0..3).each {
print a.call, "\t", b.call, "\n"
}
|
die 'too low' if $x < 99; |
raise 'too low' if x < 99 |
my @cmdline = ('echo');
for (qw(PATH EDITOR PAGER)) {
if (defined $ENV{$_}) {
push @cmdline, "$_ is set. ";
}
}
system(@cmdline);
# or
system(
'echo',
map { "$_ is set. " }
grep { defined $ENV{$_} }
qw(PATH EDITOR PAGER)
);
|
cmdline = ['echo']
cmdline.
push(*%w(PATH EDITOR PAGER).
find_all { |k| ENV[k] }.
map { |k| k + ' is set. ' })
system(*cmdline)
|
if (my $pid = open(KID, '|-')) {
print KID int(rand(100)), "\n" for (0..9);
} else {
exec('sort', '-n');
}
|
IO.popen('-', 'w') do |f|
if (f == nil)
exec('sort', '-n')
else
(0..9).each { f.puts(rand(100)) }
end
end
|
$object->can('do_something')->(); |
object.method('do_something').call |
eval { $object->does_not_exist() };
warn "hey: $@";
|
begin
object.does_not_exist
rescue
warn "hey: #{$!}"
end |
open(my $F, '/etc/hosts') || die $!;
while(<$F>) {
next if /^#/; # skip comments
print;
}
|
File.open('/etc/hosts').each_line { |l|
next if /^#/.match(l) # skip comments
puts(l)
}
|
my @files = do {
opendir(my $DIR, '.') || die $!;
grep { -f } readdir $DIR;
};
|
files = Dir.new('.').find_all { |f| File.file?(f) }
|
ref($obj) | obj.class |
$obj->isa('Some::Type'); # if we know $obj is a blessed reference
UNIVERSAL::isa($obj, 'Some::Type'); # otherwise
|
obj.is_a?('Some::Type') |
my $pid = open(my $h, '-|');
if ($pid) { # reader parent
my @r = <$h>;
} else { # writer child
exec('ls', '-l');
}
|
pipe = IO.popen('-', 'r')
if pipe # reader parent
r = pipe.readlines
else # writer child
exec 'ls', '-l'
end
|
/some pattern/s | /some pattern/m |
Data::Dumper($obj); |
obj.inspect
(this tip lifted from xoa.petdance.com) |
foreach my $meth (qw(eins zwei drei)) {
no strict 'refs';
*{"DynamicDef::$meth"} = sub { return $meth };
}
my $d = bless {}, 'DynamicDef';
$d->eins; # returns 'eins'
$d->zwei; # returns 'eins'
$d->dry; # dies
|
class DynamicDef
%w(eins zwei drei).each { |meth|
self.send(:define_method, meth) { return meth }
}
end
d = DynamicDef.new
d.eins # returns 'eins'
d.zwei # returns 'zwei'
d.dry # dies
|