Skip to content

Instantly share code, notes, and snippets.

View mlomnicki's full-sized avatar

Michał Łomnicki mlomnicki

View GitHub Profile
@mlomnicki
mlomnicki / rest_of_array.rb
Created April 20, 2010 00:16
Rest of array
a, *b = [1,2,3,4]
# a = 1
# b = [2,3,4]
@mlomnicki
mlomnicki / string_regex.rb
Created April 19, 2010 23:41
regex as string index
s = 'michal@example.com'
s[/^([^@]+)@(.*)/, 1] # => michal
s[/^([^@]+)@(.*)/, 2] # => example.com
s.match(/^([^@]+)@(.*)/)[1] # => michal
s.match(/^([^@]+)@(.*)/)[2] # => example.com
@mlomnicki
mlomnicki / data_and_end.rb
Created April 20, 2010 00:24
inline data
require 'yaml'
puts "Want some magic?"
y = YAML::load(DATA)
puts y.inspect
__END__
---
@mlomnicki
mlomnicki / check)if_inherits.rb
Created August 6, 2010 08:42
check if class inherits
# Check if class inherits from antoher class
if Fixnum < Numeric
puts "Fixnum is a numeric"
else
puts "Fixnum is not a numeric"
end
@mlomnicki
mlomnicki / arguments_count.rb
Created September 7, 2010 22:35
How to check how many arguments the method accepts
# How to check how many arguments the method accepts?
class Foo
def initialize
end
def bar(a,b)
# << operator
original = "foo"
copy = original
copy << "bar"
copy # => "foobar"
original # => "foobar"
# += operator
original = "foo"
@mlomnicki
mlomnicki / dont_forget_comma.rb
Created January 20, 2011 19:54
Comma vs implicit string concat
def foo(a = "hello", b = "stranger")
puts "#{a} #{b}"
end
foo("go" "away")
@mlomnicki
mlomnicki / splat.rb
Created January 21, 2011 18:39
splat_tricks
a = *"Hello" #=> ["Hello"]
@mlomnicki
mlomnicki / include_vs_extend.rb
Created February 7, 2011 23:48
include_vs_extend
module Foo
def bar
end
def baz
end
@mlomnicki
mlomnicki / gist:890485
Created March 28, 2011 13:48
node.js simple benchmark
var pg = require('pg');
var http = require('http');
var conString = "tcp://node:node@127.0.0.1/postgres";
var client = new pg.Client(conString);
client.connect();
http.createServer(function(req, res) {