- You MUST NOT try and generate a Rails app from scratch on your own by generating each file. For a NEW app you MUST use
rails new
first to generate all of the boilerplate files necessary. - Create an app in the current directory with
rails new .
- Use Tailwind CSS for styling. Use
--css tailwind
as an option on therails new
call to do this automatically. - Use Ruby 3.2+ and Rails 8.0+ practices.
- Use the default Minitest approach for testing, do not use RSpec.
- Default to using SQLite in development.
rails new
will do this automatically but take care if you write any custom SQL that it is SQLite compatible. - An app can be built with a devcontainer such as
rails new myapp --devcontainer
but only do this if requested directly. - Rails apps have a lot of directories to consider, such as app, config, db, etc.
- Adhere to MVC conventions: singular model names (e.g., Product) map to plural tables (products); controllers are plural.
- Guard against incapable browsers accessing controllers with `allo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Tree | |
attr_reader :val, :left, :right | |
def self.empty | |
EmptyTree.new | |
end | |
def self.leaf(val) | |
new(val, empty, empty) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# need to "pin" the variable, but this only works with the lambda because | |
# pattern matching falls back to case matchint (=== on any object in ruby, | |
# which is NOT "identical object" as in JS) | |
def matches1(pat,x) | |
case x | |
in ^pat | |
puts true | |
else | |
puts false | |
end |
This is a compiled list of falsehoods programmers tend to believe about working with time.
Don't re-invent a date time library yourself. If you think you understand everything about time, you're probably doing it wrong.
- There are always 24 hours in a day.
- February is always 28 days long.
- Any 24-hour period will always begin and end in the same day (or week, or month).
This is a very quick gist I'm throwing together to get this info and discussion onto google in one place.
For years, people have been using jemalloc with ruby. There are various benchmarks and discussions. Some people say Jemalloc 5 doesn't work as well as Jemalloc 3.
Then, one day, hope appeared on the horizon. Someone offered a config for Jemalloc 5.
The recipe would be something like this (shown with official docker ruby image). I didn't try this yet, don't know if it will build!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "openssl" | |
require "digest" | |
def aes128_cbc_encrypt(key, data, iv) | |
key = Digest::MD5.digest(key) if(key.kind_of?(String) && 16 != key.bytesize) | |
iv = Digest::MD5.digest(iv) if(iv.kind_of?(String) && 16 != iv.bytesize) | |
aes = OpenSSL::Cipher.new('AES-128-CBC') | |
aes.encrypt | |
aes.key = key | |
aes.iv = iv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'openssl' | |
require 'base64' | |
require 'rexml/document' | |
class PKeyRSAConverter | |
def initialize(from_pem: nil, from_xml: nil) | |
@from_pem = from_pem | |
@from_xml = from_xml | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
require 'openssl' | |
TYPES = { | |
6 => {magic: 'RSA1', private_key: false}, | |
7 => {magic: 'RSA2', private_key: true} | |
} | |
data = ARGV[0] ? File.binread(ARGV[0]) : $stdin.binmode.read | |
type, version, algo, magic, bits, rest = data.unpack('ccx2l<a4l<a*') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# the new and improved one-file rails app -- now including | |
require "action_controller/railtie" | |
class Tester < Rails::Application | |
config.session_store :cookie_store, :key => '_rails_session' | |
config.secret_token = '095f674153982a9ce59914b561f4522a' | |
end | |
class UsersController < ActionController::Base |
NewerOlder