Skip to content

Instantly share code, notes, and snippets.

View patshaughnessy's full-sized avatar

Pat Shaughnessy patshaughnessy

View GitHub Profile
// Code from: http://patshaughnessy.net/2020/1/20/downloading-100000-files-using-async-rust
//
// Cargo.toml:
// [dependencies]
// tokio = { version = "0.2", features = ["full"] }
// reqwest = { version = "0.10", features = ["json"] }
// futures = "0.3"
use std::io::prelude::*;
use std::fs::File;
@patshaughnessy
patshaughnessy / find_string_example.rs
Last active November 22, 2020 23:39
Rust Vec<String> find example
fn main() {
let needle = "list".to_string();
let haystack = ["some".to_string(), "long".to_string(), "list".to_string(), "of".to_string(), "strings".to_string()].to_vec();
if let Some(str) = haystack.iter().find(|&s| *s == needle) {
println!("{}", needle);
} else {
println!("Nothing there...");
}
}
@patshaughnessy
patshaughnessy / main.rs
Created June 9, 2018 14:42
Execute a simple SQL report using Rust and Diesel
#[macro_use]
extern crate diesel;
use diesel::prelude::*;
table! {
users (id) {
id -> Int4,
first_name -> Nullable<Varchar>,
last_name -> Nullable<Varchar>,
@patshaughnessy
patshaughnessy / ruby-book-club-questions.md
Last active August 18, 2017 19:38
Ruby Book Club Questions - Episodes 7 and 8

Hi guys, So in episode 7 you were asking about where and how the puts method connects to the computer’s display and writes the output. And in episode 8 you were asking about the times method, how that worked and why it wasn’t implemented with YARV instructions. In both cases, I didn’t go into detail about this in the book because it would have distracted you from the topic at hand, which is how YARV executes your code. (Or in this case, my example Ruby code from the book.)

Both the puts and times methods were written by the Ruby code team, and not by you or me. Both of them are implemented in C code. So when you’re writing a Ruby program, some of the methods you write yourself, but you get many other methods for free because they are part of the Ruby language. Many of the these built in methods are part of the standard library, which means they are Ruby code written by the Ruby core team, while other built in methods are written directly in C code by the Ruby team.

As you know, the puts method tak

# Prepare a new stack frame (save the old base pointer, and set
the new base pointer to the current stack pointer)
pushq %rbp
movq %rsp, %rbp
# Get the address of your string (lea = load effective address)
# and put it into the rdi register as a parameter to printf.
leaq L_.str(%rip), %rdi
# Not sure why this is needed - I believe it sets eax to zero.
@patshaughnessy
patshaughnessy / gist:70519495343412504686
Last active April 28, 2024 01:19
How to Debug Postgres using LLDB on a Mac
This note explains how to build Postgres from source and setup to debug it using LLDB on a Mac. I used this technique to research this article:
http://patshaughnessy.net/2014/10/13/following-a-select-statement-through-postgres-internals
1. Shut down existing postgres if necessary - you don’t want to mess up your existing DB or work :)
$ ps aux | grep postgres
pat 456 0.0 0.0 2503812 828 ?? Ss Sun10AM 0:11.59 postgres: stats collector process
pat 455 0.0 0.0 2649692 2536 ?? Ss Sun10AM 0:05.00 postgres: autovacuum launcher process
pat 454 0.0 0.0 2640476 304 ?? Ss Sun10AM 0:00.74 postgres: wal writer process
pat 453 0.0 0.0 2640476 336 ?? Ss Sun10AM 0:00.76 postgres: writer process
@patshaughnessy
patshaughnessy / gist:81b40188214259678541
Last active August 29, 2015 14:21
A quick look at when ActiveRecord instantiates Ruby objects
With regard to this twitter conversation:
https://twitter.com/PericlesTheo/status/601745115074420736
Hi Pericles,
This morning I took a quick look at how ActiveRecord converts the query result
back into Ruby objects.
Here’s where it happens:
@patshaughnessy
patshaughnessy / gist:a6b2c53a56d288f8dbf6
Last active August 29, 2015 14:01
Teaching Kids Programming Discussion
This weekend Ashley Williams (@ag_dubs), Jeff Casimir (@j3) and I had a brief
discussion on Twitter about teaching programming to kids. We thought this would
be a topic that many other people might be interested in too, so I setup this
page in case anyone else wants to join the discussion.
We're not sure where this will lead - as a next step we could setup a Google
group, Discourse site, Basecamp or some other forum for a longer discussion.
Here's the original Twitter conversation:
Wow Justin, I really loved this! My only criticism is that it’s too bad you
used CoffeeScript instead of Ruby :)
I especially loved the top-down approach you used in your example. I’ve started
to use this all the time myself. I actually wrote about this recently:
http://patshaughnessy.net/2014/2/10/use-an-ask-dont-tell-policy-with-ruby
For me, the biggest benefit of the top-down (“Ask, Don’t Tell” - apologies to
Dave Thomas) style is how it encourages a functional coding style. There are
just so many benefits of doing this. Since I’ve been doing this at a micro
after :: String -> [String] -> [String]
after _ [] = []
after target (x:xs)
| isInfixOf target x = [x] ++ xs
| otherwise = after target xs
main = do
contents <- readFile "the-lake-isle-of-innisfree.txt"
putStr . unlines . after "glimmer" . lines $ contents