Skip to content

Instantly share code, notes, and snippets.

<h2>My To Do list</h2>
<ul>
<li>
Go to the mall
</li>
<li>
Send a letter
</li>
<li>
Pay the bill
@kv109
kv109 / regexp_re_find_all.py
Last active August 29, 2015 13:58
Regexp with re.findall
regexp = '\w(\d)'
string = 'abc123!@#'
# 1st way - with #findall
occurances = re.findall(regexp, string)
print 'regexp:', regexp
print 'string:', string
print 'occurances:', occurances
@kv109
kv109 / files.py
Created April 9, 2014 18:11
Files
f = open('my_file', 'w')
f.write("writing to file, line 1\n")
f.write("writing to file, line 2\n")
f.close()
f = open('my_file', 'r')
for line in f.readlines():
print line
xboxdrv --trigger-as-button --wid 0 --led 2 --deadzone 4000 --silent &
sleep 1
xboxdrv --trigger-as-button --wid 1 --led 3 --deadzone 4000 --silent &
sleep 1
xboxdrv --trigger-as-button --wid 2 --led 4 --deadzone 4000 --silent &
sleep 1
xboxdrv --trigger-as-button --wid 3 --led 5 --deadzone 4000 --silent &
#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 / 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 / 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 / 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