TIL that we can disable gems when loading a Ruby script through --disable=gems.

$ ruby --disable=gems -e ''

TIL that we can get optimized assembly output from a Cargo project by running the following command:

cargo rustc --release -- --emit asm

This will output x86 by default, but say we wanted the intel syntax?

cargo rustc -- --emit asm -C "llvm-args=-x86-asm-syntax=intel"

TIL that Rust closures creates an anonymous struct in order to represent their environment. As an example:

let a = 42;
let b = 100;
let f = |v: i32| v + a + b;

would be represented as something like:

struct __anonymous_e3b0105<'a> {
    a: &'a i32,
    b: &'a i32,
}

TIL about recursive macros in vim. As the name implies, it consists of calling your macro within the macro your are building. Pre-requisites:

  1. Having a clear register (qa -> q to clear register a)
  2. Having a break point through t or f if we don’t want to run on all lines

Suppose we have the following file:

 1  abc,123
 2  bcd,4
 3  cde,54321
 4  def,88

And we want the end result to be:

hash["abc"] = 123
hash["bcd"] = 4
hash["cde"] = 54321
hash["def"] = 88

In order to build our recursive macro:

Command     
qa Start macro
02cw Go to beginning of line and change two words
hash"[<esc>  Insert hash[" characters and exit normal mode
f, Find the next , character
i"] = <esc> Insert "] = characters and exit normal mode
lx Move left and delete a character (the , in this case)
j Go to the line below
@a Call your macro. This creates the recursion
q Finish macro

After that calling @a will change every line.

TIL about the flock system call which applies or removes an advisory lock on an open file. The thing to pay attention is the word advisory, other processes can ignore this lock if they wish to do so. This syscall will lock the entire file, which can be pretty bad for highly concurrent systems.

For more information: man 2 flock

For a more fine grained locking mechanism: man 2 fcntl

TIL about the FromStr trait in Rust that is implicitly used through str’s parse method.

We could have a struct called Period that parses a string like: “10 days” into Period { value: 10, timeframe: "days" }. The way to use it would be:

let period = "10 days".parse::<Period>();

TIL about the term cookie tossing, an attack leverages the feature that a subdomain can put a key=value pair in a cookie which can then also be read by the domain above it. For example, todayilearned.dev doesn’t know if this key comes from it or from cookie.todayilearned.dev.

This becomes problematic when a website allows untrusted people to host subdomains under its domain. They can then abuse this feature to write manipulated cookie information.

TIL about Count-Min Sketch, an algorithm used to answer questions like:

  • What’s the frequency of our samples?
  • What’s our most frequent samples?

Similar to bloom filters, it uses k distinct hash functions. Every observed value serves as an input for these functions and the output for each function is a number corresponding to a bucket.After finding the buckets we just need to increment a counter in each of these buckets.

In order to retrieve the frequency of an observed value we find the buckets for this value and return the minimum counter among all the buckets: min(bucket_x, bucket_y, bucket_z)

A short version the paper can be found here.

TIL about :scriptnames in order to get a list of scripts that vim has loaded. It’s pretty useful since we can infer if any of our scripts are being overridden by another one. We can also run this command as scr

Copied to clipboard