- 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 newfirst 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 tailwindas an option on therails newcall 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 newwill 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 --devcontainerbut 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
| # config/initializers/phlex_template_handler.rb | |
| require "action_view" | |
| require "phlex" | |
| # Intercept unknown "capitalized" method calls (e.g., PageView(...)) in templates, | |
| # look up a Phlex component class, instantiate it, and render it. | |
| # Crucially, we re-bind the user’s block so that `self` is the component, not the ActionView context. | |
| module PhlexDynamicMethodCalls | |
| def method_missing(name, *args, **kwargs, &block) | |
| # Only intercept method calls starting with an uppercase letter (e.g. "PageView", "MyComponent", etc.) |
Let's say you want to use Ruby for the backend of a basic webapp but React on the frontend. Here's how.
(Note: All tested on January 13, 2025 with Ruby 3.3, Sinatra 4.1.1, and React 18.3. Configs may change over time.)
First, create the app folder and set up Sinatra:
mkdir my-sinatra-react-app| #!/usr/bin/env ruby | |
| require "bundler/inline" | |
| gemfile do | |
| source "https://rubygems.org" | |
| gem "typhoeus" | |
| end | |
| require "uri" |
| # frozen_string_literal: true | |
| # config/initializers/colorized_logger.rb | |
| # This initializer adds color to the Rails logger output. It's a nice way to | |
| # visually distinguish log levels. | |
| module ColorizedLogger | |
| COLOR_CODES = { | |
| debug: "\e[36m", # Cyan | |
| info: "\e[32m", # Green | |
| warn: "\e[33m", # Yellow |
| #!/usr/bin/env ruby | |
| # make sure you have Ruby installed, should be installed by default on macOS & Linux | |
| # install Minisky lib with: [sudo] gem install minisky | |
| # then run with: ruby follow_hashtag.rb somehashtag | |
| require 'minisky' | |
| if !File.exist?('config.yml') | |
| puts "Create a config.yml file with contents like this:" |
| require 'prism' | |
| require 'ripper' | |
| chars = %w[1 % = a b == * . ( ) , % ? ** & [ ] { } | ; : => ~ ! ^ || && '' .. ... < > + $a @a] + [' ', "\n", ' if ', ' and ',' rescue ', ' in '] | |
| p seed: seed = rand(10000) | |
| srand seed | |
| $VERBOSE=nil | |
| (3..10).each do |n| | |
| [chars.size**n, 100000].min.times do |i| | |
| code = n.times.map{chars.sample}.join | |
| p [n, i] if i % 1000 == 0 |
Extracting financial disclosure reports and police blotter narratives using OpenAI's Structured Output
tl;dr this demo shows how to call OpenAI's gpt-4o-mini model, provide it with URL of a screenshot of a document, and extract data that follows a schema you define. The results are pretty solid even with little effort in defining the data — and no effort doing data prep. OpenAI's API could be a cost-efficient tool for large scale data gathering projects involving public documents.
OpenAI announced Structured Outputs for its API, a feature that allows users to specify the fields and schema of extracted data, and guarantees that the JSON output will follow that specification.
For example, given a Congressional financial disclosure report, with assets defined in a table like this: