jquery, morris, raphael => graphs

<link rel="stylesheet" href="http://cdn.oesmith.co.uk/morris-0.5.1.css">
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>
<script src="http://cdn.oesmith.co.uk/morris-0.5.1.min.js"></script>


<div id="myfirstchart" style="height: 250px;"></div>

<script>
new Morris.Line({
// ID of the element in which to draw the chart.
element: 'myfirstchart',
// Chart data records -- each entry in this array corresponds to a point on
// the chart.
data: [
{ year: '2008', value: 20 },
{ year: '2009', value: 10 },
{ year: '2010', value: 5 },
{ year: '2011', value: 5 },
{ year: '2012', value: 20 }
],
// The name of the data record attribute that contains x-values.
xkey: 'year',
// A list of names of data record attributes that contain y-values.
ykeys: ['value'],
// Labels for the ykeys -- will be displayed when you hover over the
// chart.
labels: ['Value']
});
</script>

Read More

more lambda, more procs

p = Proc.new { |x| puts x*2 }
[1,2,3].each(&p)              

The ‘&’ tells ruby to turn the proc into a block

proc = Proc.new { puts "Hello World" }
proc.call                    

The body of the Proc object gets executed when called

lam = lambda { |x| puts x*2 }
[1,2,3].each(&lam)

lam = lambda { puts "Hello World" }
lam.call

blocks cant be called standalone while procs and lambdas can.

Also important to know is that both procs and lambdas are actually Procs

proc   # returns '#<Proc:0x007f96b1032d30@(irb):75>'
lam    # returns '<Proc:0x007f96b1b41938@(irb):76 (lambda)>'

Difference:

lam = lambda { |x| puts x }    # creates a lambda that takes 1 argument
lam.call(2)                    # prints out 2
lam.call                       # ArgumentError: wrong number of arguments (0 for 1)
lam.call(1,2,3)                # ArgumentError: wrong number of arguments (3 for 1)

whilst:

proc = Proc.new { |x| puts x } # creates a proc that takes 1 argument
proc.call(2)                   # prints out 2
proc.call                      # returns nil
proc.call(1,2,3)               # prints out 1 and forgets about the extra arguments

They also treat the return statement differently.. but i dont care ;/

one step further:

def calculation(a, b, operation)
  operation.call(a, b)
end

puts calculation(5, 6, lambda { |a, b| a + b }) # addition
puts calculation(5, 6, lambda { |a, b| a - b }) # subtraction

Read More

lambda procs and blocks

If you want to pass a code block to a existing method you can use Procs

Key here is the ampersand &.
If we have a Proc object in a
variable and we want to pass it to a method that is looking for a code block, we can
convert the Proc object back into a code block by sticking an ampersand in front of it:

class Report

  attr_reader :title, :text
  attr_accessor :formatter

  def initialize(&formatter)
    @title = 'Monthly Report'
    @text = [ 'Things are going', 'really, really well.' ]
    @formatter = formatter
  end

  def output_report
    @formatter.call( self )
  end
end

HTML_FORMATTER = lambda do |context|
  puts('<html>')
  puts(' <head>')
  puts("<title>#{context.title}</title>")
  puts(' </head>')
  puts(' <body>')
  context.text.each do |line|
  puts("<p>#{line}</p>")
end

puts(' </body>')
puts

With our new Proc -based formatters in hand, we are ready to create some
reports. Given that we have a Proc object and the Report constructor expects a code
block, we need to stick an ampersand in front of our Proc object when we create a
new Report instance:

report = Report.new &HTML_FORMATTER
report.output_report

Read More

yields and blocks

In Ruby, methods may receive a code block in order to perform arbitrary segments of code.

When a method expects a block, it invokes it by calling the yield function.

This comes in hand when you want to iterate over a list or provide an own algorithm

example

class Person 
  def initialize( name ) 
     @name = name
end

  def do_with_name 
     yield( @name ) 
  end 
end

When you now invoke the Person class and pass a block

person = Person.new("johnlol")
person.do_with_name do |name|
   puts "Hey, his name is #{name}"
end

This would results obviously in

Hey, his name is johnlol

another example:

palin = %w(Lagerregal Rotor Otto Rentner)
person = Person.new(palin)
person.do_with_name do |name|
   puts name.reverse
end

Palindromlol..

Read More

AUR

AUR Helper installieren (z.B. pacaur/yaourt/packer -> https://wiki.archlinux.org/index.php/AUR_helper)

1. tarball aus AUR laden

mkdir builds
curl -O https://aur.archlinux.org/packages/fo/foo/foo.tar.gz

2. tarball entpacken

tar -xvzf foo.tar.gz

3. Run makepkg in the directory where the files are saved
(makepkg -s will automatically resolve dependencies with pacman). This will download the code, compile it and pack it.

4. Generiertes Paket installieren

pacman -U /path/to/pkg.tar.xz

Read More

systemd services

https://wiki.archlinux.org/index.php/Systemd

systemd-befehle

systemctl status example.service
systemctl enable/disable example.service
systemctl start/stop example.service
journalctl

systemd-Service erstellen

Service in /etc/systemd/system/ erstellen (z.B. example.service)

[Unit]
Description=

Beschreibung/Name des Services

[Service]
WorkingDirectory=   

Verzeichnis des Service-Skripts

Type=

Start-Up Type (z.B. simple/forking/oneshot https://wiki.archlinux.org/index.php/Systemd#Type)

RemainAfterExit=yes

Service bleibt “active” falls Skript exitet

ExecStart=  

auszuf├╝hrendes Skript inkl. Parameter

[Install]
WantedBy=

target eintragen (verf├╝gbare anzeigen: systemctl list-units --type=target)


rauskopieren
/etc/systemd/system/example.service

[Unit]
Description=

[Service] 
WorkingDirectory= 
Type= 
RemainAfterExit=yes 
ExecStart=

[Install] 
WantedBy=

Read More