Skip to content

Instantly share code, notes, and snippets.

require 'rubygems'
require 'mongo'
require 'pry'
require 'win32ole'

db = Mongo::Connection.new.db("test")
x = db['text']
@1dolinski
1dolinski / if.rb
Last active December 16, 2015 01:19
excel opener
require 'win32ole'
#check if there is an excel application open
#yes
#check through the open excel applications, see if one of the tab names is the same as the one specified in the parameter
#yes
#set that workbook and that sheet equal to the variables @wb and @ws1
#no
#create a new workbook, delete all of the pages except "Sheet1", rename "Sheet1" to "openxls"
#no
require 'win32ole'
#check if there is an excel application open
#yes
#check through the open excel applications, see if one of the tab names is the same as the one specified in the parameter
#yes
#set that workbook and that sheet equal to the variables @wb and @ws1
#no
#create a new workbook, delete all of the pages except "Sheet1", rename "Sheet1" to "openxls"
#no
require 'win32ole'
#check if there is an excel application open
#yes
#check through the open excel applications, see if one of the tab names is the same as the one specified in the parameter
#yes
#set that workbook and that sheet equal to the variables @wb and @ws1
#no
#create a new workbook, delete all of the pages except "Sheet1", rename "Sheet1" to "openxls"
#no
require 'win32ole'
#check if there is an excel application open
#yes
#check through the open excel applications, see if one of the tab names is the same as the one specified in the parameter
#yes
#set that workbook and that sheet equal to the variables @wb and @ws1
#no
#create a new workbook, delete all of the pages except "Sheet1", rename "Sheet1" to "openxls"
#no
<%= form_tag("/search", :method => "get") do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:q) %>
<%= submit_tag("Search") %>
<% end %>
This will generate the following HTML:
<form accept-charset="UTF-8" action="/search" method="get">
<label for="q">Search for:</label>
<input id="q" name="q" type="text" />
require 'string_score/ext/string'
"JAMES DOWNY".downcase.score("Downy, James".downcase)
#=> 0
"JAMES DOWNY".downcase.score("James Downy".downcase)
#=> 1
w = [[2],[1],[3]]
#=> [[2], [1], [3]]
x = w.dup
#=> [[2], [1], [3]]
x.each do |a|
a << 1
end

Now you know the fundamental requirement for thread-safe code: mutation of shared state must be done atomically. Any time you change a variable that is shared by many threads, it needs to be done atomically. Unfortunately Ruby and most other mainstream languages only give you one tool to do this: the lock aka the mutex.

Mutex is short for “mutual exclusion” as in “only one thread can be executing this code at a time”. Usage is simple:

@mutex = Mutex.new
@mutex.synchronize do
  i += 1
end
y = 3
if y != 2 || y != 4
puts "ok"
end
# => "ok"
# Is there a way to shorten this?