Skip to content

Instantly share code, notes, and snippets.

@agnellvj
agnellvj / a.rb
Created May 20, 2014 19:04 — forked from ahoward/a.rb
# fork it
#
# make it print all true with only ONE LINE OF CODE
class A
def A.foo
@foo ||= (
if self == A
'42.0'
else
$(document).ready(function(){
$('.other_submit').click(function(){
this.form.action = this.getAttribute('data-action')
})
})
@agnellvj
agnellvj / Gemfile
Created January 11, 2011 16:06
load specific gems for specific platforms.
source :gemcutter
eval File.read(File.expand_path("../config/bundler/general.gemfile", __FILE__))
unless RUBY_PLATFORM[/mswin/] || RUBY_PLATFORM[/mingw/]
eval File.read(File.expand_path("../config/bundler/unix.gemfile", __FILE__))
end
$ git clone github:lenary/guides.git
Cloning into guides...
remote: Counting objects: 255, done.
remote: Compressing objects: 100% (216/216), done.
remote: Total 255 (delta 111), reused 163 (delta 35)
Receiving objects: 100% (255/255), 1.49 MiB | 564 KiB/s, done.
Resolving deltas: 100% (111/111), done.
$ cd guides
$ git remote -v
curl localhost:9200/_search -d '{
"query" : {
"filtered" : {
"query" : {"match_all" : {}},
"filter" : {
"and" : [
{
"range" : {
"file_size" : {"from" : 10, "to" : 10000}
}
data = ["1", "11", "111", "2", "22", "222", "3", "4", "5"]
y = (1..9).inject({}) { |memo, data| memo[data.to_s] = 0; memo }
data.each { |z| y[z.chr] = y.fetch(z.chr, 0) + 1 }
puts y
y = {};(1..9).map { |x| y[x.to_s] = 0 }
data.each { |z| y[z.chr] = y.fetch(z.chr, 0) + 1 }
puts y
@agnellvj
agnellvj / tire_http_clients_benchmark.rb
Created September 9, 2011 01:13 — forked from karmi/tire_http_clients_benchmark.rb
Benchmark Tire gem with RestClient and Curb HTTP Clients
#
# A basic, synthetic benchmark of the impact HTTP client has
# on the speed of talking to ElasticSearch in the Tire gem.
#
# In general, Curb seems to be more then two times faster the RestClient, in some cases it's three
# to five times faster. I wonder if keep-alive has anything to do with it, but it probably does.
#
# Run me with:
# $ git clone git://github.com/karmi/tire.git
# $ cd tire
@agnellvj
agnellvj / api.markdown
Created September 12, 2011 20:16 — forked from jcasimir/api.markdown
Exposing an API in Rails 3

Exposing an API

APIs are becoming an essential feature of modern web applications. Rails does a good job of helping your application provide an API using the same MVC structure you're accustomed to.

In the Controller

Let's work with the following example controller:

class ArticlesController < ApplicationController

Using jQuery

jQuery has become the most popular JavaScript library both inside and outside the Rails community. Let's look at how to take advantage of the library with our applications.

Setup

The setup process differs depending on whether your app is running on a version of Rails before 3.1 or greater than 3.1.

Before Rails 3.1: jquery-rails