-
-
Save minhajuddin/d6fce8c4a0e199bf6857 to your computer and use it in GitHub Desktop.
ruby code demonstration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#6 lines of ugly code | |
i = 0 | |
tasks = list.tasks | |
while(i < tasks.length - 2) | |
tasks[i].priority.should >= tasks[i + 1].priority | |
i += 1 | |
end | |
#3 lines of elegant functional code | |
list.tasks.each_cons(2).each do |t1, t2| | |
t1.priority.should >= t2.priority | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace MathEvaluator { | |
public class Evaluator { | |
public decimal Evaluate(string expression) { | |
return Reduce(expression, | |
'+', | |
x => Reduce(x, | |
'-', | |
y => Reduce(y, | |
'*', z => Reduce(z, | |
'/', s => decimal.Parse(s))))); | |
} | |
private decimal Reduce(string expression, char operation, Func<string, decimal> furtherReduction) { | |
var tokens = expression.Split(operation); | |
Operand operand = furtherReduction(tokens[0]); | |
for (int i = 1; i < tokens.Length; i++) { | |
operand = new Operand(operand, furtherReduction(tokens[i]), operation); | |
} | |
return operand; | |
} | |
} | |
} | |
using System; | |
using System.Collections.Generic; | |
namespace MathEvaluator { | |
public class Operand { | |
static readonly Dictionary<char, Func<Operand, Operand, decimal>> _operationDefinitions = | |
new Dictionary<char, Func<Operand, Operand, decimal>> | |
{ | |
{'+', (first, second) => first.Value + second.Value}, | |
{'-', (first, second) => first.Value - second.Value}, | |
{'*', (first, second) => first.Value*second.Value}, | |
{'/', (first, second) => first.Value/second.Value} | |
}; | |
public static implicit operator Operand(decimal value) { | |
return new Operand(value); | |
} | |
public static implicit operator Decimal(Operand operand) { | |
return operand.Value; | |
} | |
public decimal Value { get; private set; } | |
public Operand(decimal value) { | |
Value = value; | |
} | |
public Operand(Operand firstOperand, Operand secondOperand, char operation) { | |
Value = secondOperand == null | |
? firstOperand.Value | |
: _operationDefinitions[operation](firstOperand, secondOperand); | |
} | |
} | |
} | |
using Xunit; | |
namespace MathEvaluator.Tests { | |
public class EvaluatorTests { | |
readonly Evaluator _eval; | |
public EvaluatorTests() { | |
_eval = new Evaluator(); | |
} | |
[Fact] | |
public void Evaluate_Returns8_For5Plus3() { | |
var result = _eval.Evaluate("3+5"); | |
Assert.True(result == 8); | |
} | |
[Fact] | |
public void Evaluate_ReturnsValidResult_ForFourAdditions() { | |
var result = _eval.Evaluate("5+3+2+5"); | |
Assert.True(result == 15); | |
} | |
[Fact] | |
public void Evaluate_ReturnsValidResult_ForSimpleSubtraction() { | |
var result = _eval.Evaluate("5-2"); | |
Assert.True(result == 3); | |
} | |
[Fact] | |
public void Evaluate_ReturnsValidResult_ForComplexSubtraction() { | |
var result = _eval.Evaluate("5-2-1.1+2.2-2.4"); | |
Assert.True(result == 1.7M); | |
} | |
[Fact] | |
public void Evaluate_ReturnsValidResult_ForComplexMultiplication() { | |
var result = _eval.Evaluate("5*2-1.1*2.2-2.4"); | |
Assert.True(result == 5.18M); | |
} | |
[Fact] | |
public void Evaluate_ReturnsValidResult_ForComplexDivision() { | |
var result = _eval.Evaluate("5*2-1.1*2.2-2.4+5/2+5*3/2+5/2*3"); | |
Assert.True(result == 22.68M); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
#{{{ Infrastructure code | |
def slot(title) | |
now = Time.now | |
hours = now.hour * 1.0 + now.min * 1.0/60 | |
if(yield(hours)) | |
puts "<#{title}>" | |
end | |
end | |
class Float | |
def between?(first, second) | |
self > first && self < second | |
end | |
end | |
#}}} | |
#configuration | |
slot("sleep time") {|h| h.between?(0,5) } | |
slot("namaz & quran") {|h| h.between?(5,6) } | |
slot("quran learn/read") {|h| h.between?(6,6.30) } | |
slot("quran dev") {|h| h.between?(6.30,7) } | |
slot("check email") {|h| h.between?(7,7.25) } | |
slot("bid for jobs") {|h| h.between?(7.25,7.75) } | |
slot("learning slot") {|h| h.between?(7.75,9) } | |
slot("write code") {|h| h.between?(9,10) } | |
slot("review tasks") {|h| h.between?(10,10.25) } | |
slot("assign tasks") {|h| h.between?(10.25,10.5) } | |
slot("review code") {|h| h.between?(10.5,11.5) } | |
slot("client communication") {|h| h.between?(11.5,12.5) } | |
slot("lunch") {|h| h.between?(12.5,13) } | |
slot("nap time") {|h| h.between?(13,13.5) } | |
slot("review slot") {|h| h.between?(13.5,14) } | |
slot("open slot") {|h| h.between?(13.5,16) } | |
slot("checkout sw scene") {|h| h.between?(16,16.5) } | |
slot("badminton break") {|h| h.between?(16.5,18.5) } | |
slot("session prep") {|h| h.between?(18.5,19.25) } | |
slot("session") {|h| h.between?(19.25,20.5) } | |
slot("sleep time") {|h| h.between?(20.5,23.99) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'nokogiri' | |
require 'httparty' | |
url = ARGV.first | |
i = 0 | |
`mkdir /tmp/book -p` | |
pages = [] | |
while true | |
i += 1 | |
puts "dowloading '#{url}'" | |
page = HTTParty.get(url) | |
doc = Nokogiri::HTML(page.body) | |
file = "/tmp/book/book-#{i}.html" | |
pages << file | |
File.open(file, 'w') {|f| f.write page.body } | |
next_url = doc.css("a").find{|x| x.text == "next"} | |
break unless next_url | |
url = URI.parse(url).merge(next_url.attr('href')).to_s | |
end | |
puts 'generating pdf' | |
`wkhtmltopdf #{pages.join(' ')} /tmp/book/output.pdf` | |
puts 'done.' | |
#wget the first page and parse the next link | |
#get all the pages this way | |
#send the html through wkhtmltopdf | |
#TODO:email to your kindle account |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# ======================================================= # | |
# __ __ _ _ ____ _ _ # | |
# | \/ (_)_ __ __| | _ / ___|| |_ __ _ ___| | __ # | |
# | |\/| | | '_ \ / _` | (_) \___ \| __/ _` |/ __| |/ / # | |
# | | | | | | | | (_| | _ ___) | || (_| | (__| < # | |
# |_| |_|_|_| |_|\__,_| (_) |____/ \__\__,_|\___|_|\_\ # | |
# # | |
# ======================================================= # | |
title=("$@") | |
unset title[0] | |
title_string=${title[*]} | |
space=" " | |
#setup | |
data_dir=~/.mind_stack | |
data_file=~/.mind_stack/data | |
[ -d $data_dir ] || mkdir -p $data_dir #create a directory if not present | |
[ -f $data_file ] || touch $data_file #create a file if not present | |
s_ts(){ | |
echo $(date "+%Y%m%d%H%M%S") | |
} | |
append(){ | |
echo "$(s_ts)" "$title_string" > $data_file.tmp && cat $data_file >> $data_file.tmp && mv $data_file.tmp $data_file | |
} | |
push(){ | |
echo "$(s_ts)" "$title_string" >> $data_file | |
} | |
list(){ | |
tac ~/.mind_stack/data | |
} | |
peek(){ | |
echo $( tail -1 $data_file | sed "s/^[0-9]*//g" ) | |
} | |
pop(){ | |
peek | |
head -n-1 $data_file > $data_file.tmp && mv $data_file.tmp $data_file | |
} | |
reset(){ | |
mv $data_file $data_file.$(s_ts) | |
} | |
if [ $# -eq 0 ] | |
then | |
list | |
exit | |
fi | |
case $1 in | |
push) | |
push | |
exit | |
;; | |
pop) | |
pop | |
exit | |
;; | |
peek) | |
peek | |
exit | |
;; | |
ls) | |
list | |
exit | |
;; | |
edit) | |
tac $data_file > $data_file.tmp | |
vi $data_file.tmp | |
tac $data_file.tmp > $data_file | |
exit | |
;; | |
top) | |
str=$(list | head -2 | sed -e 's/^[0-9]*//g' | ruby -e 'print ARGF.readlines.map(&:chomp).map(&:strip).join(" : ")') | |
count=$( grep -vE '^[0-9]+ \*' $data_file| wc -l |cut -d' ' -f1 ) | |
echo "<fc=#ff4400>($count)</fc> <fc=#ffff00>$str</fc>" | |
exit | |
;; | |
append) | |
append | |
exit | |
;; | |
reset) | |
reset | |
exit | |
;; | |
help) | |
echo 'Mind :: Stack usage:' | |
echo '==========================================================' | |
echo 's [push] some stuff #to add something to the top of stack' | |
echo 's pop #to pop something from the top of the stack' | |
echo 's peek #to peek at something on the top of the stack' | |
echo 's ls #to list the whole stack' | |
echo 's append #to append a task to the end of the stack' | |
echo 's reset #to reset the stack' | |
echo 's edit #to edit the whole list in vi' | |
echo 's help #to print this usage/help' | |
echo '==========================================================' | |
exit | |
;; | |
*) | |
title_string=$1$space$title_string | |
push | |
esac |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lib/role_constraint.rb | |
class RoleConstraint | |
def initialize(*roles) | |
@roles = roles | |
end | |
def matches?(request) | |
@roles.include? request.env['warden'].user.try(:role) | |
end | |
end | |
#config/routes.rb | |
root :to => 'admin#index', :constraints => RoleConstraint.new(:admin) #matches this route when the current user is an admin | |
root :to => 'sites#index', :constraints => RoleConstraint.new(:user) #matches this route when the current user is an user | |
root :to => 'home#index' #matches this route when the above two matches don't pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################################## | |
#mongodbbak | |
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'aws/s3' | |
require 'pony' | |
#run export | |
Dir.chdir("#{ENV['HOME']}/archives/mongodb/") | |
puts 'dumping..' | |
`mongodump` | |
#zip | |
puts 'compressing..' | |
hostname = `hostname`.chomp | |
file = "mongodb.#{hostname}.#{Time.now.strftime "%Y%m%d%H%M%S"}.tar.gz" | |
md5file = "#{file}.md5sum" | |
`tar cf - dump --remove-files| gzip > #{file}` | |
`md5sum #{file} > #{md5file}` | |
#copy | |
puts "copying to #{file} s3.." | |
AWS::S3::Base.establish_connection!( | |
:access_key_id => ENV['AMAZON_ACCESS_KEY_ID'], | |
:secret_access_key => ENV['AMAZON_SECRET_ACCESS_KEY'] | |
) | |
AWS::S3::S3Object.store(file, open(file), 'cvbak') | |
puts "#{file} uploaded" | |
#create md5checksum | |
AWS::S3::S3Object.store(md5file, open(md5file), 'cvbak') | |
puts "#{md5file} uploaded" | |
#message | |
Pony.mail(:to => 'min@mailinator.com', :from => 'sysadmin@mailinator.com.com', :subject => "[sys] db on #{hostname} backed up to #{file}", :body => "mongodb database on #{hostname} has been successfully backed up to #{file}") | |
puts 'done' | |
################################################## | |
#crontab -l | |
@daily /bin/bash -i -l -c '/home/ubuntu/repos/server_config/scripts/mongodbbak' >> /tmp/mongobak.log 2>&1 | |
################################################## | |
#~/.bashrc | |
export AMAZON_ACCESS_KEY_ID='mykey' | |
export AMAZON_SECRET_ACCESS_KEY='mysecret' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment