Skip to content

Instantly share code, notes, and snippets.

View mtungusov's full-sized avatar

Mikhail Tungusov mtungusov

  • Serbia, Belgrade
View GitHub Profile
@mtungusov
mtungusov / add_route.sh
Last active October 26, 2015 12:30
Add routes on OSX
#!/bin/sh
ADD="sudo route add -net"
DEL="sudo route delete -net"
# Interfaces -> gateway
WIFI=192.168.2.1
THUN=192.168.1.1
# Internal networks
def fizzbuzz(x)
result = "#{'Fizz' if x % 3 == 0}#{'Buzz' if x % 5 == 0}"
result.empty? ? "#{x}" : result
end
def print_range(x)
(1..x).each { |el| puts fizzbuzz(el) }
end
@mtungusov
mtungusov / gist:14416457ab8b03448646
Created May 12, 2014 08:45
Struct to Hash & Hash to Struct conversion
# Struct to Hash
class Struct
def to_hash
Hash[*members.map(&:to_s).zip(values).flatten]
end
end
# Hash to Struct
class Hash
def to_struct(name)
@mtungusov
mtungusov / fibonacci.go
Created January 15, 2014 13:27
Fibonacci in Go
package main
import "fmt"
func fib(n int) (r int) {
switch n {
case 0:
r = 0
case 1:
r = 1
@mtungusov
mtungusov / date_epoch.coffee
Created December 1, 2013 17:52
Date From To Epoch
@DateToEpoch = (date) ->
parseInt(date.getTime()/1000)
@DateFromEpoch = (epochTime) ->
tmp = new Date 0
tmp.setUTCSeconds epochTime
@DateToStr = (date) ->
y = "#{date.getFullYear()}"
m = "#{date.getMonth() + 1}"
@mtungusov
mtungusov / bash_rvm.sh
Created October 22, 2013 08:05
Bash. PS, rvm and git info
ESC="\033" # This is the escape sequence
NO_COLOR="$ESC[0m"
IRED="$ESC[1;31m" # ANSI color code for intense/bold red
IGRN="$ESC[1;32m" # ANSI color code for intense/bold green
# From http://railstips.org/blog/archives/2009/02/02/bedazzle-your-bash-prompt-with-git-info/
# I had to change 'git-symbolic-ref' to 'git symbolic-ref'
function parse_git_branch {
ref=$(git symbolic-ref HEAD 2> /dev/null) || return
echo " ["${ref#refs/heads/}"]" # I wanted my branch wrapped in [], use () or <> or whatever
@mtungusov
mtungusov / print_duration.rb
Created October 18, 2013 08:49
print duration, exec time
module PrintDuration
def print_duration(name)
start = Time.now
result = yield
total = (Time.now - start) * 1000
puts "#{name}: #{total.to_i}ms"
result
end
alias_method :some_method_orig, :some_method
require 'redis'
require 'em-websocket'
SOCKETS = []
@redis = Redis.new(:host => '127.0.0.1', :post => 6379)
# Creating a thread for the EM event loop
Thread.new do
EventMachine.run do
# Creates a websocket listener
@mtungusov
mtungusov / xmpp_client.js.coffee
Created September 20, 2013 12:13
xmpp js client
this.xmpp ?= {}
class XmppClient
constructor: ->
@interlocutors = []
@currentUserId = window.currentUserId
# @dispatcher = new WebSocketRails "xmpp.dev.improva.com/websocket"
# @dispatcher = new WebSocketRails "localhost:3000/websocket"
@dispatcher = new WebSocketRails window.websocketPath
@channel = @dispatcher.subscribe "messages"
@mtungusov
mtungusov / xmpp_extension.rb
Created September 20, 2013 12:03
XMPP client
require 'xmpp4r'
require 'xmpp4r/roster'
module XmppExtension
# class Jabber::JID
# # generate node@domain
# def to_short_s
# s = []
# s << "#@node@" if @node