Posts mit dem Label ruby werden angezeigt. Alle Posts anzeigen
Posts mit dem Label ruby werden angezeigt. Alle Posts anzeigen

Sonntag, 11. Oktober 2009

RUBY codes: code blocks and iterations

from "Ruby Cookbook" O'Reilly
for detailed information, look this book


[1,2,3].each { |i| puts i }

[1,2,3].each do |i|
if i%2 == 0
puts "="
else
puts "!"
end
end

1.upto 3 do |x| # same 1.upto(3)

1.upto(3) { |x| ... } # ok
1.upto 3 { |x| ... } # error


log = lambda { |str| puts "[LOG] #{str}" }
log.call("A test log message.")


{1=>2, 2=>4}.each { |k,v| puts "Key #{k}, value #{v}" }


def times_n(n)
lambda { |x| x * n }
end

times_ten = times_n(10)
times_ten.call(5) # => 50

circumference = times_n(2*Math::PI)
[1, 2, 3].collect(&circumference)
# => [6.28318530717959, 12.5663706143592, 18.8495559215388]


Clousure:
Every Ruby block is also a closure.

ceiling = 50
[1, 10, 49, 50.1, 200].select { |x| x < ceiling }
# => [1, 10, 49]

the var celing is context depended.

Lambda and Proc


block = { |x| puts x } # error
block = lambda { |x| puts x } # ok
block.call "bla bla!"


def my_lambda(&aBlock) # a block in function parameters
aBlock
end

b = my_lambda { puts "Hello World My Way!" }
b.call
b.class # => Proc

aBlock = Proc.new { |x| puts x }
aBlock = proc { |x| puts x }
aBlock = lambda { |x| puts x }


add_lambda = lambda { |x,y| x + y }
add_lambda.call(4,5,6) # ArgumentError: wrong number of arguments (3 for 2)

add_procnew = Proc.new { |x,y| x + y }
add_procnew.call(4,5,6) # => 9



Yielding


def myfunc
puts "1"
yield
puts "2"
yield
end

myfunc("3")
# 1
# 3
# 2
# 3


def repeat(n)
if block_given?
n.times { yield }
else
raise ArgumentError.new("I can't repeat a block you don't give me!")
end
end

repeat(4) { puts "Hello." }
# Hello.
# Hello.
# Hello.
# Hello.

repeat(4)
# ArgumentError: I can't repeat a block you don't give me!

Donnerstag, 8. Oktober 2009

Challenge: pattern syslog

Challenge:

filtern some patern in file /var/log/syslog
and print it out


Shell:
fgrep usb /var/log/syslog


Java:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class JavaFilters {
public static void main(String[] args) {
String path="/var/log/syslog";
try {
BufferedReader input = new BufferedReader(new FileReader(path));

String line;
try {
Pattern p = Pattern.compile("dhclient");
while (( line = input.readLine()) != null){
Matcher m = p.matcher(line);
if(m.find())
System.out.println("found:: "+line);
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}


Scala:

import scala.io.Source
import java.util.regex._

var p = Pattern.compile("usb")

for ( line <- Source.fromFile("/var/log/syslog").getLines ){
var m = p.matcher(line);
if( m.find )
print("found::" + line)
}


Perl:

#!/usr/bin/perl

open(IN, "/var/log/syslog") or die "canot open file: $!";

@lines = <IN>;
close IN;

foreach $line(@lines) {
if( $line =~ /dhclient/) {
print "found:: $line";
}
}


Ruby:

#!/usr/bin/ruby

file = File.new("/var/log/syslog", "r") or die "canot open file"

while (line = file.gets)
if ( line =~ /usb/ )
print "found:: "+line
end
end


Python:

#!/usr/bin/python

try:
f = open('/var/log/syslog', 'r')

for line in f.readlines():
if( "usb" in line ):
print("found:: %s"%line)

except:
print "error in file"


PHP:

<?php
$file = "/var/log/syslog";
if (file_exists($file)) {
$lines = file($file);
foreach ($lines as $line_num => $line)
if( preg_match("/usb/", $line) )
echo "$line";
}
?>


C:

#include
#include
#include
#include
#include

FILE *file = NULL;

int main(int argc, char **argv)
{
char line[1024];
int err;
char err_str[200];
char results[500];

regex_t preg;
regmatch_t pmatch[100];

if( (regcomp(&preg, argv[1], 0)) != 0) {
printf("regcomp error\n");
return -1;
}

if((file = fopen("/var/log/syslog", "r")) != NULL) {
while (!feof(file)){
fgets(line, 1024, file);

if( regexec(&preg, line, preg.re_nsub, pmatch, 0) == 0) {
printf("found:: %s", line);
}
}
}

regfree(&preg);
return 0;
}

Montag, 23. Februar 2009

ruby infos

http://www.rubyinside.com/passenger-stack-quickly-install-a-full-ruby-and-passenger-stack-1533.html

Passenger-Stack is a set of scripts for the provisioning tool Sprinkle that make installing a full Ruby, Apache and Passenger-based stack almost a one-line task. It'll take almost any UNIX-y (correction: Ubuntu / Debian) server of your choice from a generic install through to deploying Rack-based apps (including those built with Rails, Merb, Sinatra, and Ramaze).


=>
Sprockets: A Ruby-powered JavaScript dependency library from 37signals

Building a Ruby Gem (From Scratch) Using Behavior Driven Development