Skip to content

Instantly share code, notes, and snippets.

View jah2488's full-sized avatar
:fishsticks:

Justin Herrick jah2488

:fishsticks:
View GitHub Profile
@jah2488
jah2488 / constructors.rb
Created February 11, 2013 18:38
Totally valid ruby syntax under 1.9.2-p318 and p290 Infinite arguments! Pass anything (and everything) you'd like.
class TakeNoArgs
def say_hi
puts "hello"
end
end
TakeNoArgs.new.say_hi #=> hello
TakeNoArgs.new( ['how are you?'] ).say_hi #=> hello
TakeNoArgs.new( {:args => 13}, 12, "hello" ).say_hi #=> hello
@jah2488
jah2488 / is_this_a_reservered_word.rb
Created February 13, 2013 20:37
ruby is_this_a_reserved_word.rb Array #=> True
puts %W(
ADDITIONAL_LOAD_PATHS
ARGF
ARGV
ActionController
ActionView
ActiveRecord
ArgumentError
Array
BasicSocket
@jah2488
jah2488 / tf.rb
Last active December 14, 2015 20:39
Check True or False of values in ruby.
[1,2,3,4,5,6,7,8,9,0, #numbers
'',"",'hi',"hello", #strings
:sym, /hi/, 1.324, #symbol, regex, float
[nil],[],[1],[''],['hi'], #arrays
{},{:hi => ''},{'hi' => 'hello'}, #hashes
true,false,nil ].map { |item| !!item }
@jah2488
jah2488 / ps_to_csv.sh
Created March 24, 2013 03:16
Turn ps output into a csv format. This only prints to STDOUT, could easily be redirected to a file.
> ps | ruby -e 'require "csv"; puts CSV.generate { |csv| ARGF.each_line { |line| csv << line.split(/\s+/).reject(&:empty?) }}'
PID,TTY,TIME,CMD
62541,ttys000,0:00.71,-zsh
59313,ttys004,0:01.23,-zsh
63830,ttys004,0:00.00,ruby,-e,require,"""csv"";",puts,CSV.generate,{,|csv|,ARGF.each_line,{,|line|,csv,<<,line.split(/\s+/).reject(&:empty?),}}
@jah2488
jah2488 / h_store_properties.rb
Last active December 15, 2015 20:59 — forked from hotgazpacho/h_store_properties.rb
I had to comment out the scope and attr_accessible as I did this sans rails, but it defines the methods correctly now on the instances of the Pump class.
module HStoreProperties
extend self
def boolean_h_store_fields_for(property_names)
property_names.each do |key|
attr_accessible key
scope "has_#{key}", lambda { |value| where('properties @> (? => ?)', key, value) }
define_method(key) do
properties && properties[key].to_bool
end
records = MyArray.new
LocalEvaluation.for(id).map { |evaluation| records << evaluation }
OfficialEvaluation.for(id).map { |evaluation| records << evaluation }
return sort(records) || []
sort(MyArray.new(
LocalEvaluation.for(id).concat(OfficialEvaluation.for(id))
)) || []
@jah2488
jah2488 / group_by_class.rb
Last active December 16, 2015 02:49
An internal debate I've been having as to where is the best place to put a function call that defines additional methods in ruby. Both have pros and cons.
class Paper
Scopes.define_for_last_year(self, :due_date)
end
class Book
Scopes.define_for_last_year(self, :release_date)
end
class Publication
Scopes.define_for_last_year(self, :pub_date)
@jah2488
jah2488 / String.rb
Last active December 16, 2015 08:19
Fun with ruby blocks
class String
def to_proc
Proc.new { self }
end
end
say &"Hello, World!"
# => Hello, World!
@jah2488
jah2488 / examples.js
Last active December 16, 2015 17:19
Taking javascript code and porting it to ruby
function example() {
var myVar = 1;
if(myVar === 1) {
var myVar = 2;
}
return myVar;
}
function declared() {
return "declared function";
<?xml version="1.0" encoding="utf-8"?>
<project>
<!-- <meta />
Use meta nodes to set metadata for your application. The description is ignored
on most targets, but is useful for packaging like Chrome Apps or Opera Widgets.
For compatibility with Android and webOS, the package name must include at least