Skip to content

Instantly share code, notes, and snippets.

View everton's full-sized avatar

Everton J. Carpes everton

  • http://www.nutrebem.com.br
  • Rio de Janeiro, RJ - Brasil
View GitHub Profile
@everton
everton / Bhaskara
Created April 6, 2011 13:45
Weird way to say Bhaskara in Ruby (or "((-b).± √ Δ(a, b, c))" / (2 * a) as a valid Ruby expression)
#!/usr/bin/env ruby
#-*- coding: utf-8 -*-
def √(n)
Math::sqrt(n)
end
class Numeric
def ±(n)
r = [self + n, self - n]
@everton
everton / gist:1080407
Created July 13, 2011 14:35
jRuby bug with encoding
#-*- coding: utf-8 -*-
str = "<h1>Bugs#index</h1>
<%= @user %>
<p>á</p>"
str =~ /(?m-ix:<%(=+|-|\#|%)?(.*?)([-=])?%>([ \t]*\r?\n)?)/
#!/usr/bin/env ruby
project_source_dir = ARGV.first
project_source_dir += '/' unless project_source_dir[-1] == '/'
raise 'Not a Git repository' unless File.exists? "#{project_source_dir}.git"
# TODO: raise 'Not a Rails application' if ???
def pending_git_tasks?
@everton
everton / gist:91a2cb2aed4258218ad9
Created May 30, 2014 02:56
rails-4.1.2.rc2 before_create bug report
# Activate the gem you are reporting the issue against.
gem 'activerecord', '4.1.2.rc1'
require 'active_record'
require 'minitest/autorun'
require 'logger'
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
# This connection will do for database-independent bug reports.
@everton
everton / rails-issue-20633.rb rails-4.2.3.rc1-unscope-bug.rb
Last active August 29, 2015 14:23
blongs_to unscope not respected in includes - rails 4.2.3
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
# Activate the gem you are reporting the issue against.
@everton
everton / minitest_twice_fork.rb
Last active February 4, 2016 20:58
Minitest running twice when dealing with forks
require 'minitest/autorun'
class TestSuite
def self.before_run
pid = fork do
puts "on fork"
exit 0
end
Process.detach pid
@everton
everton / tasks_test_helper.rb
Created September 15, 2017 12:26
IntegrationTest specialization to test rake tasks
require 'rake'
class TasksTest < ActionDispatch::IntegrationTest
include ActionMailer::TestHelper
private
def self.task(t = nil)
return @task unless t
t = t.to_s
@everton
everton / table-to-mobile.html
Created December 23, 2018 15:20
Mobile Table + CSS Scroll Snap Points
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Mobile Table + CSS Scroll Snap Points</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style type="text/css" media="screen">
import java.lang.invoke.MethodHandles;
class Base {
public static class CurrentClassGetter extends SecurityManager {
public Class<?>[] getClassContext() {
return super.getClassContext();
}
}
public static void find(int id) {
@everton
everton / form_builder.rb
Last active March 31, 2020 12:08
Add "required" for input tags of fields with "presence" validation and "minlength" / "maxlength" of fields with "length" validation
# config/initializers/form_builder.rb
module InputTagsMarkedAsRequiredOrWithMinMaxLengthInHTMLIfValidatedForPresence
def initialize(object_name, method_name, template_object, options = {})
return super unless options[:object] # it will be nil when called from #form_with
validators = options[:object].class.validators_on(method_name)
unless options.has_key? :required # check using has_key? to allow "required: nil"
if validators.any? ActiveRecord::Validations::PresenceValidator