Skip to content

Instantly share code, notes, and snippets.

@ffloyd
ffloyd / mongoid_direct_upsert.rb
Created July 11, 2013 18:46
Interesting mongoind "direct upsert"
MongoidClass.collection.find(param1: 'blablab', param2: 'blablabla').upsert("$set" => hash_with_additional_opts)
=Navigating=
visit('/projects')
visit(post_comments_path(post))
=Clicking links and buttons=
click_link('id-of-link')
click_link('Link Text')
click_button('Save')
click('Link Text') # Click either a link or a button
click('Button Value')
i_need_name :matrix_name do
:m1 + :m2[+1,-1] + (:m3 * :m4)[+1, 0]
end
@ffloyd
ffloyd / error.rb
Created April 20, 2014 08:44
Example of runtime class generation
module MetaCL
module Error
class MetaCLError < StandardError; end
{
# matrices errors
MatrixUnknownElementType: 'Cannot define matrix: unknown element type',
MatrixInvalidSizeParams: 'Cannot define matrix: invalid size params',
MatrixNameDuplication: 'Cannot define matrix: matrix with same name already exists',
MatrixNotFound: 'Cannot find matrix with given name', # TODO: what name?
@ffloyd
ffloyd / result.c
Created April 20, 2014 16:05
metacl result
#include <stdlib.h>
#include <stdio.h>
int main()
{
float *a = malloc(100 * sizeof(float)); // N = 10, M = 10
for (int i = 0; i < 100; i++) {
a[i] = 1;
}
@ffloyd
ffloyd / test_code.rb
Created April 20, 2014 16:05
metacl source
configure do
lang :c
end
create_matrix :a, :float, 10, 10, fill_with: 1
create_matrix :b, :float, 10, 10, fill_with: 2
create_matrix :c, :float, 10, 10, fill_with: 3
create_matrix :d, :float, 10, 10, fill_with: 4
create_matrix :result, :float, 10, 10
<%= simple_form_for @product do |f| %>
<%= f.simple_fields_for :attributes do |d| %>
<% f.object.attributes.try(:each) do |key, value| %>
<%= d.input key, :input_html => {:value => value } %>
<% end %>
<% end %>
<% end %>
@ffloyd
ffloyd / Problem1001.scala
Created August 1, 2014 22:41
time limit bullshit
object Problem1001 extends App {
val numbersStrings = io.Source.stdin.getLines()
for {
x <- numbersStrings.flatMap {
_.split(" ").filterNot { _.isEmpty }.map { x => math.sqrt(x.toDouble) }
}.toList.reverse
} yield println(f"$x%.04f".replace(',', '.'))
}
object Problem1001 {
def main(args: Array[String]) {
val numbersStrings = io.Source.stdin.getLines()
val squares = numbersStrings.flatMap {
_.split(" ").filterNot { _.isEmpty }.map { x => math.sqrt(x.toLong) }
}.toArray.reverse
println(squares.mkString("\n"))
}
}
@ffloyd
ffloyd / double_method.rb
Last active August 29, 2015 14:06
double_method
require 'benchmark'
require 'active_support'
module Hello
extend ActiveSupport::Concern
BENCH_ITERS = 1_000_000
included do
double_method :hello_double, :hello