Skip to content

Instantly share code, notes, and snippets.

@scudelletti
scudelletti / gist:4529250
Created January 14, 2013 10:50
Ruby Method Initialize
def initialize(opts)
opts.each do |opt,val|
instance_variable_set("@#{opt}", val.to_s) if respond_to? opt
end
end
@scudelletti
scudelletti / attr_accessor_for_class_variables.rb
Last active December 16, 2015 13:59
attr_accessor for Class Variables
module XptoModule
class << self
attr_accessor :classes_with_xpto_module
end
self.classes_with_xpto_module = []
def self.included(base)
self.classes_with_xpto_module << base
end
@scudelletti
scudelletti / function.sh
Created April 30, 2013 14:06
Simple Function with Condition(IF) in Shell Script
$VAR1=BlaBlaBla
$VAR1=PqPqPqPq
function export_ambient_variable_for() {
if [[ $1 = 'prod' ]]; then
echo 'Exporting ambient variables for Prod environment'
export ZZZZ=$VAR1
else
echo 'Exporting ambient variables for Dev environment'
export ZZZZ=$VAR2
class Model
def method(&block)
puts "START"
block.call
puts "END"
end
def second(&block)
puts "SECOND - START"
method(&block)
@scudelletti
scudelletti / exception.rb
Created August 22, 2013 19:29
Handling Exceptions
puts "Getting the Exception 1"
begin
eval("1+")
rescue Exception => e
puts "Exception Class => #{e.class} - Message: #{e}"
end
puts "Getting the Exception 2"
begin
eval("1+")
@scudelletti
scudelletti / gist:6708031
Last active December 23, 2015 23:09
Imperial March
// Source => http://www.adafruit.com/forums/viewtopic.php?f=25&t=34567
//code https://gist.github.com/1804108 20121125 @ 1339
//led for visualization (use 13 for built-in led)
int ledPin = 13;
//speaker connected to one of the pwm ports
int speakerPin = 9;
//tone frequencies http://home.mit.bme.hu/~bako/tonecalc/tonecalc.htm
@scudelletti
scudelletti / blocks.rb
Created July 11, 2014 16:57
Precedence between ruby blocks
def first_function(something, &block)
puts "First - Do I have a Block? #{block_given?}"
end
def second_function(&block)
puts "Second - Do I have a Block? #{block_given?}"
end
puts "USING: {}"
first_function second_function { 'YEAP' }
@scudelletti
scudelletti / chai_custom_matcher.js
Created July 24, 2014 20:35
Chai Custom Matcher
/*
Chai - Add CustomMatchers
usage:
var CustomMatchers = require('./support/friendly_news_path_matcher');
chai.use(CustomMatchers);
expect('/materia/FooBar').to.be.a.friendlyNewsPath();
*/
@scudelletti
scudelletti / callback_weather.js
Created September 1, 2014 01:47
[NODE] Javascript Promises using Q library simple examples
// Using callbacks to retrieve Weather Data
var RestClient = require('restler')
var getURL = function(city){
return 'http://api.openweathermap.org/data/2.5/weather?q=' + city;
};
var dateOne = new Date();
RestClient.get(getURL('London')).on('complete', function(data1){
@scudelletti
scudelletti / ruby_method_visibility.rb
Created November 3, 2014 23:57
Blog comment - Ruby method visibility
class Sample
attr_accessor :protected_attribute, :private_attribute
protected :protected_attribute=
private :private_attribute=
def initialize
self.protected_attribute = 'some protected value'
self.private_attribute = 'some private value'
end
end