Skip to content

Instantly share code, notes, and snippets.

View hynkle's full-sized avatar

Jonathan Hinkle hynkle

View GitHub Profile
@hynkle
hynkle / osascript.md
Last active December 17, 2023 11:25
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

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?

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

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:

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:

@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
@hynkle
hynkle / twitter_regex.js
Created December 14, 2012 15:33
Sweet regex found in Twitter's javascript.
/^[a-z0-9_À-ÖØ-öø-ÿĀ-ɏɓ-ɔɖ-ɗəɛɣɨɯɲʉʋʻ̀-ͯḀ-ỿЀ-ӿԀ-ԧⷠ-ⷿꙀ-֑ꚟ-ֿׁ-ׂׄ-ׇׅא-תװ-״﬒-ﬨשׁ-זּטּ-לּמּנּ-סּףּ-פּצּ-ﭏؐ-ؚؠ-ٟٮ-ۓە-ۜ۞-۪ۨ-ۯۺ-ۼۿݐ-ݿࢠࢢ-ࢬࣤ-ࣾﭐ-ﮱﯓ-ﴽﵐ-ﶏﶒ-ﷇﷰ-ﷻﹰ-ﹴﹶ-ﻼ‌ก-ฺเ-๎ᄀ-ᇿ㄰-ㆅꥠ-꥿가-힯ힰ-퟿ᄀ-ᅵァ-ヺー-ヾヲ-゚ー0-9A-Za-zぁ-ゖ゙-ゞ㐀-䶿一-鿿꜀-뜿띀-렟-﨟〃々〻]*[a-z_À-ÖØ-öø-ÿĀ-ɏɓ-ɔɖ-ɗəɛɣɨɯɲʉʋʻ̀-ͯḀ-ỿЀ-ӿԀ-ԧⷠ-ⷿꙀ-֑ꚟ-ֿׁ-ׂׄ-ׇׅא-תװ-״﬒-ﬨשׁ-זּטּ-לּמּנּ-סּףּ-פּצּ-ﭏؐ-ؚؠ-ٟٮ-ۓە-ۜ۞-۪ۨ-ۯۺ-ۼۿݐ-ݿࢠࢢ-ࢬࣤ-ࣾﭐ-ﮱﯓ-ﴽﵐ-ﶏﶒ-ﷇﷰ-ﷻﹰ-ﹴﹶ-ﻼ‌ก-ฺเ-๎ᄀ-ᇿ㄰-ㆅꥠ-꥿가-힯ힰ-퟿ᄀ-ᅵァ-ヺー-ヾヲ-゚ー0-9A-Za-zぁ-ゖ゙-ゞ㐀-䶿一-鿿꜀-뜿띀-렟-﨟〃々〻][a-z0-9_À-ÖØ-öø-ÿĀ-ɏɓ-ɔɖ-ɗəɛɣɨɯɲʉʋʻ̀-ͯḀ-ỿЀ-ӿԀ-ԧⷠ-ⷿꙀ-֑ꚟ-ֿׁ-ׂׄ-ׇׅא-תװ-״﬒-ﬨשׁ-זּטּ-לּמּנּ-סּףּ-פּצּ-ﭏؐ-ؚؠ-ٟٮ-ۓە-ۜ۞-۪ۨ-ۯۺ-ۼۿݐ-ݿࢠࢢ-ࢬࣤ-ࣾﭐ-ﮱﯓ-ﴽﵐ-ﶏﶒ-ﷇﷰ-ﷻﹰ-ﹴﹶ-ﻼ‌ก-ฺเ-๎ᄀ-ᇿ㄰-ㆅꥠ-꥿가-힯ힰ-퟿ᄀ-ᅵァ-ヺー-ヾヲ-゚ー0-9A-Za-zぁ-ゖ゙-ゞ㐀-䶿一-鿿꜀-뜿띀-렟-﨟〃々〻]+$/
@hynkle
hynkle / ppjson.js
Created December 13, 2012 19:41
Reads JSON from stdin; pretty prints it to stdout.
#!/usr/bin/env node
var flatJSON = '';
process.stdin.on('data', function(data) {
flatJSON = flatJSON + data;
});
process.stdin.on('end', function() {
prettyJSON = JSON.stringify( JSON.parse(flatJSON), null, 2 )
console.log(prettyJSON);
@hynkle
hynkle / raise_as.rb
Created October 31, 2012 14:07
Raise As
# for those times you want to bubble an exception up, but want to put it in a different exception class
module Kernel
def raise_as(exception_class)
raise if $!.nil?
raise exception_class, $!.message, $!.backtrace
end
end
error: refusing to create funny ref 'refs/master' remotely