Skip to content

Instantly share code, notes, and snippets.

@toddlers
toddlers / check_nagios_config.sh
Created August 7, 2013 10:13
Checking the nagios configuration syntax
#!/bin/bash
NAGIOS='/usr/sbin/nagios3'
NAGIOS_CONFIG='/etc/nagios3/nagios.cfg'
$NAGIOS -v $NAGIOS_CONFIG > /dev/null
result=$?
if [ $result -eq 0 ]

The following is an explanation of Ruby blocks and yield by another Bloc mentor who was trying to explain it to one of his students.

On my very first day programming, if someone asked me for "the sum of the numbers from 1 to 10", I'd have written:

puts 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10

Easy enough.

@toddlers
toddlers / apt.txt
Created September 6, 2013 11:42
APT Quick Reference
Debian's apt system truely revolutionized linux package management. It inspired other distros to port apt, or build similar functionality such as the urpmi system. For many people, it is the main reason they run Debain.
Below is a quick reference to cover some of the most commonly needed apt uses.
Download and install package:
apt-get install package
Update apt's list of available packages:
apt-get update
@toddlers
toddlers / apt.txt
Created September 6, 2013 11:43
APT Quick Reference
Debian's apt system truely revolutionized linux package management. It inspired other distros to port apt, or build similar functionality such as the urpmi system. For many people, it is the main reason they run Debain.
Below is a quick reference to cover some of the most commonly needed apt uses.
Download and install package:
apt-get install package
Update apt's list of available packages:
apt-get update
@toddlers
toddlers / modules.rb
Created September 16, 2013 11:05
include() mixes methods into the instances of the base object while extend() mixes methods into the base object itself.
[2] pry(main)> class SomeClass
[2] pry(main)* include Greeter
[2] pry(main)* end
=> SomeClass
[3] pry(main)> SomeClass.new.hello
=> "hi"
[4] pry(main)> class AnotherClass
[4] pry(main)* extend Greeter
[4] pry(main)* end
=> AnotherClass
@toddlers
toddlers / bash_params.txt
Last active December 23, 2015 06:29
executing remote bash scripts with arguments.
I know how to execute remote bash script, via these syntaxes:
curl http://foo.com/script.sh | bash
or
bash < <( curl http://foo.com/script.sh )
which give the same result.
But what if I need to pass arguments to the bash script ? It's possible when the script is saved locally:
@toddlers
toddlers / regex.rb
Created September 19, 2013 15:01
regex matching in ruby
#!/usr/bin/env ruby
require 'pp'
def show_regexp(a,re)
if a =~ re
"#{$`} << #{$&} >> #{$'}"
else
"no match"
end
end
@toddlers
toddlers / block_with_ampersand.rb
Created September 20, 2013 07:35
if the last parameter in a method definition is prefixed with an ampersand, any associated block is converted to a Proc object, and that object is assigned to the parameter.
#!/usr/bin/env ruby
require 'pp'
class TaxCalculator
def initialize(name, &block)
@name, @block = name, block
end
def get_tax(amount)
"#@name on #{amount} = #{ @block.call(amount)}"
@toddlers
toddlers / dynamic_blocks.rb
Created September 20, 2013 11:28
If the last argument to a method is preceded by an ampersand, Ruby assumes that it is a Proc object. It removes it from the parameter list, converts the Proc object into a block, and associates it with the method.
print "(t)imes or (p)lus: "
times = gets
print "number: "
number = Integer(gets)
if times =~ /^t/
calc = lambda {|n| n*number}
else
calc = lambda { |n| n+number }
end
puts ((1..10).collect(&calc).join(","))
@toddlers
toddlers / links.md
Created September 23, 2013 06:18 — forked from igrigorik/links.md