Skip to content

Instantly share code, notes, and snippets.

#example.rb
class Example
def initialize(method_name)
send method_name
end
end
#example_test.rb
require 'mocha'
<span
onmousedown='document.getElementById("display").innerHTML += "mousedown<br />"'
onclick='document.getElementById("display").innerHTML += "click<br />"'
onmouseup='document.getElementById("display").innerHTML += "mouseup<br />"'>
Click me!
</span>
<br />
<span id='display'>
</span>
@kv109
kv109 / predefining_method_variables.rb
Created October 13, 2010 14:29
Defining variable in methods declaration
def method(var = 123)
var+=1
end
def another_method(var)
var ||= 123
var+=1
end
method(nil) #first method raises <<undefined method `+' for nil:NilClass>>
@kv109
kv109 / last_month_day
Created October 13, 2010 14:30
How to get the last day of the month
class Time
def last_month_day
Date.new(self.year, self.mon, -1).day
end
end
@kv109
kv109 / ampersand_converts_symbol_to_proc.rb
Created October 13, 2010 14:34
'&' create a proc object
(1..5).map{|n|n.succ}
(1..5).map(&:succ)
@kv109
kv109 / variables_into_methods.rb
Created October 14, 2010 09:28
Local and global variable in method's body
def method(arg)
#<<arg>> is not a local copy of global variable - <<string>> and <<arg>> are the same object.
arg.upcase!
arg.object_id #We will return object_id to see if it's true
end
string = 'John'
id = string.object_id
id2 = method(string)
id == id2 #=> true
@kv109
kv109 / group_by.rb
Created October 25, 2010 13:01
Using group_by on arrays
class Post
attr_reader :date, :text
def initialize(text)
@date = "2010-10-#{rand(2)+1}"
@text = text
end
end
@kv109
kv109 / each_vs_map.rb
Created October 29, 2010 12:06
Don't use each when you can use map
class Argument
attr_reader :weight
def initialize(weight)
@weight = weight
end
end
arguments = []
4.times { |i| arguments << Argument.new(i) }
@kv109
kv109 / my_app_index.php
Created February 14, 2011 17:22
progblog screencast 1
<?php
//my_app/index.php
session_start();
//uncomment line below to make your app safer
//session_regenerate_id();
echo session_id();
if( isset($_GET['login']) ) {
@kv109
kv109 / index.php
Created February 14, 2011 17:24
progblog screencast 1
<?php
//bad_cookie/index.php
setcookie('PHPSESSID', 'kv109ti635kntdjkph2ajh47a7', time()+3600*24*30, '/');
header('Location: http://your_domain/my_app');