Skip to content

Instantly share code, notes, and snippets.

View rinaldifonseca's full-sized avatar

Rinaldi Fonseca rinaldifonseca

View GitHub Profile
require 'net/ftp'
CONTENT_SERVER_DOMAIN_NAME = "one-of-the-ftp-server.thought-sauce.com.hk"
CONTENT_SERVER_FTP_LOGIN = "saucy-ftp-server-login"
CONTENT_SERVER_FTP_PASSWORD = "saucy-ftp-server-password"
# LOGIN and LIST available files at default home directory
Net::FTP.open(CONTENT_SERVER_DOMAIN_NAME, CONTENT_SERVER_FTP_LOGIN, CONTENT_SERVER_FTP_PASSWORD) do |ftp|
files = ftp.list
@rinaldifonseca
rinaldifonseca / publisher.rb
Created September 30, 2014 20:55
Publisher
#Version 1
class Publisher
def initialize(page)
@page = page
end
def publish
# do things that publish @page
end
end
require 'resolv'
class EmailValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
if Resolv::DNS.new.getresources(value.split("@").last, Resolv::DNS::Resource::IN::MX).empty?
record.errors[attribute] << (options[:message] || "does not have a valid domain")
end
rescue Resolv::ResolvError, Resolv::ResolvTimeout
record.errors[attribute] << (options[:message] || "does not have a valid domain")
end
if [ "$(date +%d)" = 31 ] || [ "$(date +%d)" = 30 ]; then rake routes; fi
require 'rubygems'
require 'open-uri'
require 'csv'
#remote
def read(url)
CSV.new(open(url), :headers => :first_row).each do |line|
puts line
puts line[0].force_encoding('iso-8859-1').encode('utf-8')
puts line['FEB11']
mode = ENV["SPEC"] || "full"
require_relative "spec_helper_#{mode}"

Using ActiveSupport::Dependencies outside of rails seems just as fast as doing manual requires. I've found this out by replacing hundreds of requires in a unit tested code base with ActiveSupport::Dependencies without any change to the time it takes to run the test suite.

Using ActiveSupport::Dependencies also allows you to have circular depedencies like class Foo::Bar in foo/bar.rb and class Foo in foo.rb that points to Foo::Bar in it's class definition (like in a rails validation).

The classical way of solving this without ActiveSupport::Dependencies is to do:

# foo/bar.rb
class Foo
 class Bar
require './interface'
puts to_strings(->(limit) {
->(lst) {
->(f) {
->(f) {
->(g) {
->(n) {
f.(g.(g)).(n)
}
}.(->(g) {
def collection_for(field)
field.fetch(:options){[]}.map{|option| [option["name"], option["name"]]}
end
erl -noshell -s helloworld start -s init stop