Skip to content

Instantly share code, notes, and snippets.

View hynkle's full-sized avatar

Jonathan Hinkle hynkle

View GitHub Profile
@hynkle
hynkle / Readme.md
Last active December 11, 2015 22:09
Rails constant buffoonery

Given an otherwise empty Rails app with the following two models:

# app/models/cities/massachusetts/boston.rb

class Cities::Massachusetts::Boston

  def self.refer_to_denver
    begin
      Colorado::Denver

I'd always assumed that alias_method(new, existing) was equivalent to something like the following (plus support for passing arguments and a block along):

def new
  existing
end

But no, apparently it really does just give you another name with which you can refer to the same method body. Thus:

I do not like the way Ruby does its constant name resolution.

Suppose somewhere in your codebase you define a module Parent:

module Parent
  FOO = "foo"
end

And some other place, you add a method onto a module inside Parent called Child:

In my codebase, I've got something like the following, where Foo, Bar, and Baz are classes:

case o
when Foo then process!
when Bar then trash!
when Baz then flag!
else raise ArgumentError, "unexpected type #{o.class}"
end

Background:

  • 'frozen' in Ruby means 'immutable'.
  • Strings are not frozen by default. I.e., 'foo'.frozen? is false.

Question: {'foo' => true}.keys.first.frozen?
Answer: true

Okay. I can totally see why you don't want hash keys to be mutable. That's completely sensible.

Question: {[] => true}.keys.first.frozen?

@hynkle
hynkle / osascript.md
Last active January 29, 2025 02:31
Getting AppleScript to output multiple lines to shell when executed with `osascript` proved challenging.

I wanted to output a list of single-line strings, each item separated by a newline. I couldn't find a way to do that without combining them all into a single string. Fine.

Reasonable attempt:

osascript -e '
set myList to {"foo", "bar", "baz"}
set myString to ""
repeat with myItem in myList
 set myString to myString & myItem & return