Sunday, January 11, 2015

Flashing a microSDHC in OS X 10.10 Yosemite for the Raspberry Pi Model B+

First, check out the Quick-start if you're totally new or the Installing Operating System Images on Mac OS documentation which hopefully should be up-to-date and more comprehensive. I'm just documenting this here from what I did a few weeks ago so that I don't lose it! :) I'm going to write this though for the non-tech savvy, just in case someone gets a Raspberry Pi B+ and really doesn't know what to do.

DO THE FOLLOWING AT YOUR OWN RISK. DOING THIS INCORRECTLY COULD LOSE ALL OR PART OF THE DATA ON YOUR COMPUTER. BE WARNED! I AM NOT RESPONSIBLE FOR YOUR ACTIONS.

First, if you didn't get one already, you'll need a microSDHC card and probably a MicroSDHC to SD Adapter.

Now download a disk image, probably from the Raspberry Pi Downloads page. This may take a long time.

In spotlight (click the magnifying glass to the upper-right), type terminal and enter.

In terminal, type:

diskutil list

This is the incredibly important part (and I'm not kidding). There is something on the left of each section that is output that tells you the identifier for each disk device, like /dev/disk0, /dev/disk1, /dev/disk2. Probably the first few will have things like Apple_CoreStorage, Apple_Boot, or Apple_HFS somewhere next to them. Those are NOT the ones you want. You need to look for the SD card, under that. It may not be obvious what it is, so be careful and don't accidentally erase an external drive, etc. In my case, it was /dev/disk2. Write that down!

Now, to attempt to unmount the card if it has been mounted, do this, replacing /dev/disk2 in the command with whatever disk is your SD:

sudo diskutil unmountDisk /dev/disk2

To flash your SD/microSDHC card, do this, replacing the part after if= with the pathname of your image and replacing the part after of= with whatever disk is your SD:

sudo dd bs=1m if=~/Downloads/2014-12-24-wheezy-raspbian.img of=/dev/disk2

That will probably also take a long time, and it just sits there, so don't assume it's locked up. After it's complete, remove the card and the microSDHC from that card and put the microSDHC into your Pi (it's a non-descript port on the side of the Pi), then just power it on after connecting a monitor or TV to the HDMI port. At the very least, you'll also want to plug a USB keyboard into it. When it comes on, it will go into setup. Note that after you setup and reboot, the default user is pi and default password is raspberry if you didn't change it.

Last thing: LATER if you want to erase the SD you flashed and make it a normal blank FAT32 formatted SD for use in a camera, etc., you'll need to erase the MBR (master boot record). To do that, use diskutil again to get the right disk number and do this command, replacing the part after of= with whatever disk is your SD:

sudo diskutil eraseDisk FAT32 SDCARD MBRFormat /dev/disk2

Monday, December 29, 2014

Casting Floats to BigDecimal in Ruby or Ruby on Rails

Rails requires 'bigdecimal' by default it seems, so only in (plain) Ruby (off Rails) do you need:

2.1.2 :001 > require 'bigdecimal'

In both Ruby and Ruby on Rails (though the stack is different):

2.1.2 :002 > BigDecimal.new(0.2342334)
ArgumentError: can't omit precision for a Float.
...

Some might be inclined to specify the number of significant digits as nil/0, such that the number of significant digits is determined from the initial value. But that leads to the following in Rails Console:

2.1.1 :006 > BigDecimal.new(0.2342334, 0)
 => #<BigDecimal:10bad84f8,'0.2342334000 0000000826 7520001936 645713E0',36(54)>
2.1.1 :008 > BigDecimal.new(0.2342334, 0).to_s
 => "0.234233400000000008267520001936645713"

Note: in irb/vanilla Ruby, to_s includes "E0" at the end:

2.1.2 :003 > BigDecimal.new(0.2342334, 0).to_s
 => "0.234233400000000008267520001936645713E0"

A workaround is to:

2.1.1 :007 > (BigDecimal.new(1) * 0.2342334).to_s
 => "0.2342334"

I've not looked into this, but I think what is going on is that to_s on Float in Ruby tosses some of the digits in this case that might be including the floating-point inaccuracy, and that when you multiply BigDecimal it may be using to_s and using that string representation of the Float as the value for the BigDecimal constructor.

Monday, December 22, 2014

A Few Sublime Text Settings

This morning Tommy shared that disabling atomic_save in Sublime Text 2 helps to resolve some issues we were having.

To disable, just "command-," then add "atomic_save": false to the json doc describing the config.

See issue #379 where people are asking to disable it by default.

Here are that and my other settings, which are fairly minimal:

{
 "color_scheme": "Packages/User/Espresso Soda.tmTheme",
 "font_size": 12.0,
 "ignored_packages":
 [
  "JSLint",
  "Vintage"
 ],
 "tab_size": 2,
 "theme": "Soda Light.sublime-theme",
 "translate_tabs_to_spaces": true,
 "atomic_save": false
}

Friday, December 19, 2014

Crashing Outlook: _WTFCrash and the WTF Namespace

Copy/pasted a long terminal session into an Outlook 14.4.6 (OS X Office 2011) message, and it crashed. Best part are the _WTFCrash and WTF namespace Microsoft uses:


Microsoft Error Reporting log version: 2.0

Error Signature:
Exception: EXC_BAD_ACCESS
Date/Time: 2014-12-19 17:55:49 +0000
Application Name: Microsoft Outlook
Application Bundle ID: com.microsoft.Outlook
Application Signature: OPIM
Application Version: 14.4.6.141106
Crashed Module Name: JavaScriptCore
Crashed Module Version: 9537.78.1
Crashed Module Offset: 0x0027ab23
Blame Module Name: Microsoft Outlook
Blame Module Version: 14.4.6.141106
Blame Module Offset: 0x000611ad
Application LCID: 1033
Extra app info: Reg=en Loc=0x0409
Crashed thread: 0


Thread 0 crashed:

#  1  0x9670eb23 in _WTFCrash + 0x00000043 (JavaScriptCore + 0x0027ab23)
#  2  0x96495ef1 in WTF::fastMalloc(unsigned long) + 0x00000701 (JavaScriptCore + 0x00001ef1)
#  3  0x9649d90d in WTF::StringImpl::create(unsigned short const*, unsigned int) + 0x0000003D (JavaScriptCore + 0x0000990d)
#  4  0x967dd035 in WTF::String::String(NSString*) + 0x000001C5 (WebCore + 0x00002035)
#  5  0x96e39d0f in -[DOMHTMLElement setInnerHTML:] + 0x0000003F (WebCore + 0x0065ed0f)
#  6  0x9b8a91e1 in -[NSHTMLWriter readDocumentFragment:] + 0x00000231 (AppKit + 0x005b21e1)
#  7  0x9b8a9286 in -[NSHTMLWriter documentFragmentForDocument:] + 0x00000042 (AppKit + 0x005b2286)
#  8  0x9b6f2943 in -[NSAttributedString(NSAttributedStringKitAdditions) _documentFromRange:document:documentAttributes:subresources:] + 0x000000C0 (AppKit + 0x003fb943)
#  9  0x9899c571 in WebEditorClient::documentFragmentFromAttributedString(NSAttributedString*, WTF::Vector<WTF::RefPtr<WebCore::ArchiveResource>, 0ul, WTF::CrashOnOverflow>&) + 0x00000211 (WebKit + 0x0005a571)
# 10  0x97359a9e in WebCore::documentFragmentWithRTF(WebCore::Frame*, NSString*, WTF::String const&) + 0x0000029E (WebCore + 0x00b7ea9e)
# 11  0x97358536 in WebCore::Pasteboard::documentFragment(WebCore::Frame*, WTF::PassRefPtr<WebCore::Range>, bool, bool&) + 0x00000E96 (WebCore + 0x00b7d536)
# 12  0x96e8c496 in WebCore::Editor::pasteWithPasteboard(WebCore::Pasteboard*, bool) + 0x000000C6 (WebCore + 0x006b1496)
# 13  0x96e81873 in WebCore::Editor::paste() + 0x000000A3 (WebCore + 0x006a6873)
# 14  0x96e8a88e in WebCore::executePaste(WebCore::Frame*, WebCore::Event*, WebCore::EditorCommandSource, WTF::String const&) + 0x0000003E (WebCore + 0x006af88e)
# 15  0x96bec7c5 in WebCore::Editor::Command::execute(WTF::String const&, WebCore::Event*) const + 0x000000C5 (WebCore + 0x004117c5)
# 16  0x9897314f in -[WebHTMLView executeCoreCommandBySelector:] + 0x0000008F (WebKit + 0x0003114f)
# 17  0x989738d8 in -[WebHTMLView paste:] + 0x00000028 (WebKit + 0x000318d8)
# 18  0x9549a2af in -[NSObject performSelector:withObject:] + 0x00000046 (libobjc.A.dylib + 0x0001a2af)
# 19  0x9b54b8a5 in -[NSApplication sendAction:to:from:] + 0x000001B6 (AppKit + 0x002548a5)
# 20  0x9b567c65 in -[NSMenuItem _corePerformAction] + 0x000001E6 (AppKit + 0x00270c65)
# 21  0x9b56796a in -[NSCarbonMenuImpl performActionWithHighlightingForItemAtIndex:] + 0x000000A3 (AppKit + 0x0027096a)
# 22  0x9b567021 in -[NSMenu _performActionWithHighlightingForItemAtIndex:sendAccessibilityNotification:] + 0x0000004F (AppKit + 0x00270021)
# 23  0x9b566fcd in -[NSMenu _performActionWithHighlightingForItemAtIndex:] + 0x00000030 (AppKit + 0x0026ffcd)
# 24  0x9b566972 in -[NSMenu performKeyEquivalent:] + 0x00000126 (AppKit + 0x0026f972)
# 25  0x9b563095 in -[NSApplication _handleKeyEquivalent:] + 0x000003AA (AppKit + 0x0026c095)
# 26  0x9b4cc8a2 in -[NSApplication sendEvent:] + 0x00000F47 (AppKit + 0x001d58a2)
# 27  0x000b81ad in long OPF::CopyProperty<OpaqueSavedSpotlightSearch>(OpaqueContext*, OpaqueSavedSpotlightSearch const*, PropertyAccessor<CUnicodeString, OPF::Traits<OpaqueSavedSpotlightSearch>::SharableType>::GetProp, char const*, __CFString const**) + 0x0001A543 (Microsoft Outlook + 0x000611ad)
# 28  0x9b3111bc in -[NSApplication run] + 0x00000337 (AppKit + 0x0001a1bc)
# 29  0x9b2f9ff8 in _NSApplicationMain + 0x0000048D (AppKit + 0x00002ff8)
# 30  0x0005ae0e in CSharablePtr<CNetHostOption>::Set(CNetHostOption*) + 0x000001E0 (Microsoft Outlook + 0x00003e0e)

X86 Thread State:
 eax: 0x00000000  ebx: 0x964957fe  ecx: 0x039aa300  edx:0x000b2260
 edi: 0x00000000  esi: 0xa0b3dbb0  ebp: 0xbffa7fa8  esp:0xbffa7f10
  ss: 0x00000023  eip: 0x9670eb23   cs: 0x0000001b   ds:0x00000023
  es: 0x00000023   fs: 0x00000000   gs: 0x0000000f  eflags:0x00010286


Thread 1:

#  1  0x98304992 in _kevent64 + 0x0000000A (libsystem_kernel.dylib + 0x00018992)
#  2  0x97c37532 in __dispatch_mgr_init + 0x00000000 (libdispatch.dylib + 0x00003532)

X86 Thread State:
 eax: 0x00000171  ebx: 0xb00926a0  ecx: 0xb009267c  edx:0x98304992
 edi: 0xb00926a0  esi: 0x97c377b9  ebp: 0xb00926f8  esp:0xb009267c
  ss: 0x00000023  eip: 0x98304992   cs: 0x0000000b   ds:0x00000023
  es: 0x00000023   fs: 0x00000023   gs: 0x0000000f  eflags:0x00000286


Thread 2:

#  1  0x98303b76 in ___semwait_signal + 0x0000000A (libsystem_kernel.dylib + 0x00017b76)
#  2  0x9105ddc2 in _sleep$UNIX2003 + 0x0000002D (libsystem_c.dylib + 0x0007bdc2)
#  3  0x028e5992 in _MerpCreateSession + 0x00000B3F (merp + 0x00003992)
#  4  0x028e505e in _MerpCreateSession + 0x0000020B (merp + 0x0000305e)
#  5  0x028e4fe9 in _MerpCreateSession + 0x00000196 (merp + 0x00002fe9)
#  6  0x028e6586 in _MerpCreateSession + 0x00001733 (merp + 0x00004586)
#  7  0x980055fb in __pthread_body + 0x00000090 (libsystem_pthread.dylib + 0x000015fb)
#  8  0x98005485 in __pthread_struct_init + 0x00000000 (libsystem_pthread.dylib + 0x00001485)
#  9  0x9800acf2 in _thread_start + 0x00000022 (libsystem_pthread.dylib + 0x00006cf2)

X86 Thread State:
 eax: 0x0000014e  ebx: 0xb0218ad8  ecx: 0xb0218a6c  edx:0x98303b76
 edi: 0x9105deea  esi: 0xb0218ad0  ebp: 0xb0218ab8  esp:0xb0218a6c
  ss: 0x00000023  eip: 0x98303b76   cs: 0x0000000b   ds:0x00000023
  es: 0x00000023   fs: 0x00000023   gs: 0x0000000f  eflags:0x00000246


Thread 3:

#  1  0x982fef7a in _mach_msg_trap + 0x0000000A (libsystem_kernel.dylib + 0x00012f7a)
#  2  0x9a7bcbf9 in ___CFRunLoopServiceMachPort + 0x000000A9 (CoreFoundation + 0x00076bf9)
#  3  0x9a7bc1d1 in ___CFRunLoopRun + 0x00000571 (CoreFoundation + 0x000761d1)
#  4  0x9a7bb9ea in _CFRunLoopRunSpecific + 0x0000018A (CoreFoundation + 0x000759ea)
#  5  0x9a7bb84b in _CFRunLoopRunInMode + 0x0000007B (CoreFoundation + 0x0007584b)
#  6  0x9b4c8b88 in __NSEventThread + 0x0000011B (AppKit + 0x001d1b88)
#  7  0x980055fb in __pthread_body + 0x00000090 (libsystem_pthread.dylib + 0x000015fb)
#  8  0x98005485 in __pthread_struct_init + 0x00000000 (libsystem_pthread.dylib + 0x00001485)
#  9  0x9800acf2 in _thread_start + 0x00000022 (libsystem_pthread.dylib + 0x00006cf2)

X86 Thread State:
 eax: 0xffffffe1  ebx: 0xb031c048  ecx: 0xb031bf2c  edx:0x982fef7a
 edi: 0x00000c00  esi: 0x07000006  ebp: 0xb031bf68  esp:0xb031bf2c
  ss: 0x00000023  eip: 0x982fef7a   cs: 0x0000000b   ds:0x00000023
  es: 0x00000023   fs: 0x00000023   gs: 0x0000000f  eflags:0x00000206


Thread 4:

#  1  0x983037ca in ___psynch_cvwait + 0x0000000A (libsystem_kernel.dylib + 0x000177ca)
#  2  0x98009bd9 in _pthread_cond_wait$UNIX2003 + 0x00000047 (libsystem_pthread.dylib + 0x00005bd9)
#  3  0x964a5ae4 in JSC::BlockAllocator::blockFreeingThreadMain() + 0x00000114 (JavaScriptCore + 0x00011ae4)
#  4  0x9649ce1c in WTF::threadEntryPoint(void*) + 0x0000004C (JavaScriptCore + 0x00008e1c)
#  5  0x9649cdb0 in WTF::wtfThreadEntryPoint(void*) + 0x00000010 (JavaScriptCore + 0x00008db0)
#  6  0x980055fb in __pthread_body + 0x00000090 (libsystem_pthread.dylib + 0x000015fb)
#  7  0x98005485 in __pthread_struct_init + 0x00000000 (libsystem_pthread.dylib + 0x00001485)
#  8  0x9800acf2 in _thread_start + 0x00000022 (libsystem_pthread.dylib + 0x00006cf2)

X86 Thread State:
 eax: 0x00000131  ebx: 0x00000060  ecx: 0xb0420e6c  edx:0x983037ca
 edi: 0x00000800  esi: 0x00000701  ebp: 0xb0420ef8  esp:0xb0420e6c
  ss: 0x00000023  eip: 0x983037ca   cs: 0x0000000b   ds:0x00000023
  es: 0x00000023   fs: 0x00000023   gs: 0x0000000f  eflags:0x00000246


Thread 5:

#  1  0x983037ca in ___psynch_cvwait + 0x0000000A (libsystem_kernel.dylib + 0x000177ca)
#  2  0x98009bd9 in _pthread_cond_wait$UNIX2003 + 0x00000047 (libsystem_pthread.dylib + 0x00005bd9)
#  3  0x964a64eb in JSC::GCThread::waitForNextPhase() + 0x0000007B (JavaScriptCore + 0x000124eb)
#  4  0x964a6378 in JSC::GCThread::gcThreadMain() + 0x00000058 (JavaScriptCore + 0x00012378)
#  5  0x9649ce1c in WTF::threadEntryPoint(void*) + 0x0000004C (JavaScriptCore + 0x00008e1c)
#  6  0x9649cdb0 in WTF::wtfThreadEntryPoint(void*) + 0x00000010 (JavaScriptCore + 0x00008db0)
#  7  0x980055fb in __pthread_body + 0x00000090 (libsystem_pthread.dylib + 0x000015fb)
#  8  0x98005485 in __pthread_struct_init + 0x00000000 (libsystem_pthread.dylib + 0x00001485)
#  9  0x9800acf2 in _thread_start + 0x00000022 (libsystem_pthread.dylib + 0x00006cf2)

X86 Thread State:
 eax: 0x00000131  ebx: 0x00000860  ecx: 0xb04a2e5c  edx:0x983037ca
 edi: 0x00000e00  esi: 0x00000c00  ebp: 0xb04a2ee8  esp:0xb04a2e5c
  ss: 0x00000023  eip: 0x983037ca   cs: 0x0000000b   ds:0x00000023
  es: 0x00000023   fs: 0x00000023   gs: 0x0000000f  eflags:0x00000246


Thread 6:

#  1  0x983037ca in ___psynch_cvwait + 0x0000000A (libsystem_kernel.dylib + 0x000177ca)
#  2  0x98009bd9 in _pthread_cond_wait$UNIX2003 + 0x00000047 (libsystem_pthread.dylib + 0x00005bd9)
#  3  0x964a64eb in JSC::GCThread::waitForNextPhase() + 0x0000007B (JavaScriptCore + 0x000124eb)
#  4  0x964a6378 in JSC::GCThread::gcThreadMain() + 0x00000058 (JavaScriptCore + 0x00012378)
#  5  0x9649ce1c in WTF::threadEntryPoint(void*) + 0x0000004C (JavaScriptCore + 0x00008e1c)
#  6  0x9649cdb0 in WTF::wtfThreadEntryPoint(void*) + 0x00000010 (JavaScriptCore + 0x00008db0)
#  7  0x980055fb in __pthread_body + 0x00000090 (libsystem_pthread.dylib + 0x000015fb)
#  8  0x98005485 in __pthread_struct_init + 0x00000000 (libsystem_pthread.dylib + 0x00001485)
#  9  0x9800acf2 in _thread_start + 0x00000022 (libsystem_pthread.dylib + 0x00006cf2)

X86 Thread State:
 eax: 0x00000131  ebx: 0x00000860  ecx: 0xb0524e5c  edx:0x983037ca
 edi: 0x00000f00  esi: 0x00000c00  ebp: 0xb0524ee8  esp:0xb0524e5c
  ss: 0x00000023  eip: 0x983037ca   cs: 0x0000000b   ds:0x00000023
  es: 0x00000023   fs: 0x00000023   gs: 0x0000000f  eflags:0x00000246


Thread 7:

#  1  0x983037ca in ___psynch_cvwait + 0x0000000A (libsystem_kernel.dylib + 0x000177ca)
#  2  0x98009bd9 in _pthread_cond_wait$UNIX2003 + 0x00000047 (libsystem_pthread.dylib + 0x00005bd9)
#  3  0x964a64eb in JSC::GCThread::waitForNextPhase() + 0x0000007B (JavaScriptCore + 0x000124eb)
#  4  0x964a6378 in JSC::GCThread::gcThreadMain() + 0x00000058 (JavaScriptCore + 0x00012378)
#  5  0x9649ce1c in WTF::threadEntryPoint(void*) + 0x0000004C (JavaScriptCore + 0x00008e1c)
#  6  0x9649cdb0 in WTF::wtfThreadEntryPoint(void*) + 0x00000010 (JavaScriptCore + 0x00008db0)
#  7  0x980055fb in __pthread_body + 0x00000090 (libsystem_pthread.dylib + 0x000015fb)
#  8  0x98005485 in __pthread_struct_init + 0x00000000 (libsystem_pthread.dylib + 0x00001485)
#  9  0x9800acf2 in _thread_start + 0x00000022 (libsystem_pthread.dylib + 0x00006cf2)

X86 Thread State:
 eax: 0x00000131  ebx: 0x00000860  ecx: 0xb05a6e5c  edx:0x983037ca
 edi: 0x00001200  esi: 0x00000c00  ebp: 0xb05a6ee8  esp:0xb05a6e5c
  ss: 0x00000023  eip: 0x983037ca   cs: 0x0000000b   ds:0x00000023
  es: 0x00000023   fs: 0x00000023   gs: 0x0000000f  eflags:0x00000246


Thread 8:

#  1  0x983037ca in ___psynch_cvwait + 0x0000000A (libsystem_kernel.dylib + 0x000177ca)
#  2  0x98009bd9 in _pthread_cond_wait$UNIX2003 + 0x00000047 (libsystem_pthread.dylib + 0x00005bd9)
#  3  0x964a64eb in JSC::GCThread::waitForNextPhase() + 0x0000007B (JavaScriptCore + 0x000124eb)
#  4  0x964a6378 in JSC::GCThread::gcThreadMain() + 0x00000058 (JavaScriptCore + 0x00012378)
#  5  0x9649ce1c in WTF::threadEntryPoint(void*) + 0x0000004C (JavaScriptCore + 0x00008e1c)
#  6  0x9649cdb0 in WTF::wtfThreadEntryPoint(void*) + 0x00000010 (JavaScriptCore + 0x00008db0)
#  7  0x980055fb in __pthread_body + 0x00000090 (libsystem_pthread.dylib + 0x000015fb)
#  8  0x98005485 in __pthread_struct_init + 0x00000000 (libsystem_pthread.dylib + 0x00001485)
#  9  0x9800acf2 in _thread_start + 0x00000022 (libsystem_pthread.dylib + 0x00006cf2)

X86 Thread State:
 eax: 0x00000131  ebx: 0x00000860  ecx: 0xb0628e5c  edx:0x983037ca
 edi: 0x00001000  esi: 0x00000c00  ebp: 0xb0628ee8  esp:0xb0628e5c
  ss: 0x00000023  eip: 0x983037ca   cs: 0x0000000b   ds:0x00000023
  es: 0x00000023   fs: 0x00000023   gs: 0x0000000f  eflags:0x00000246


Thread 9:

#  1  0x983037ca in ___psynch_cvwait + 0x0000000A (libsystem_kernel.dylib + 0x000177ca)
#  2  0x98009bd9 in _pthread_cond_wait$UNIX2003 + 0x00000047 (libsystem_pthread.dylib + 0x00005bd9)
#  3  0x964a64eb in JSC::GCThread::waitForNextPhase() + 0x0000007B (JavaScriptCore + 0x000124eb)
#  4  0x964a6378 in JSC::GCThread::gcThreadMain() + 0x00000058 (JavaScriptCore + 0x00012378)
#  5  0x9649ce1c in WTF::threadEntryPoint(void*) + 0x0000004C (JavaScriptCore + 0x00008e1c)
#  6  0x9649cdb0 in WTF::wtfThreadEntryPoint(void*) + 0x00000010 (JavaScriptCore + 0x00008db0)
#  7  0x980055fb in __pthread_body + 0x00000090 (libsystem_pthread.dylib + 0x000015fb)
#  8  0x98005485 in __pthread_struct_init + 0x00000000 (libsystem_pthread.dylib + 0x00001485)
#  9  0x9800acf2 in _thread_start + 0x00000022 (libsystem_pthread.dylib + 0x00006cf2)

X86 Thread State:
 eax: 0x00000131  ebx: 0x00000860  ecx: 0xb06aae5c  edx:0x983037ca
 edi: 0x00001100  esi: 0x00000c00  ebp: 0xb06aaee8  esp:0xb06aae5c
  ss: 0x00000023  eip: 0x983037ca   cs: 0x0000000b   ds:0x00000023
  es: 0x00000023   fs: 0x00000023   gs: 0x0000000f  eflags:0x00000246


Thread 10:

#  1  0x983037ca in ___psynch_cvwait + 0x0000000A (libsystem_kernel.dylib + 0x000177ca)
#  2  0x98009bd9 in _pthread_cond_wait$UNIX2003 + 0x00000047 (libsystem_pthread.dylib + 0x00005bd9)
#  3  0x964a64eb in JSC::GCThread::waitForNextPhase() + 0x0000007B (JavaScriptCore + 0x000124eb)
#  4  0x964a6378 in JSC::GCThread::gcThreadMain() + 0x00000058 (JavaScriptCore + 0x00012378)
#  5  0x9649ce1c in WTF::threadEntryPoint(void*) + 0x0000004C (JavaScriptCore + 0x00008e1c)
#  6  0x9649cdb0 in WTF::wtfThreadEntryPoint(void*) + 0x00000010 (JavaScriptCore + 0x00008db0)
#  7  0x980055fb in __pthread_body + 0x00000090 (libsystem_pthread.dylib + 0x000015fb)
#  8  0x98005485 in __pthread_struct_init + 0x00000000 (libsystem_pthread.dylib + 0x00001485)
#  9  0x9800acf2 in _thread_start + 0x00000022 (libsystem_pthread.dylib + 0x00006cf2)

X86 Thread State:
 eax: 0x00000131  ebx: 0x00000860  ecx: 0xb072ce5c  edx:0x983037ca
 edi: 0x00000d00  esi: 0x00000c01  ebp: 0xb072cee8  esp:0xb072ce5c
  ss: 0x00000023  eip: 0x983037ca   cs: 0x0000000b   ds:0x00000023
  es: 0x00000023   fs: 0x00000023   gs: 0x0000000f  eflags:0x00000246


Thread 11:

#  1  0x982fef7a in _mach_msg_trap + 0x0000000A (libsystem_kernel.dylib + 0x00012f7a)
#  2  0x9a430186 in _YieldToThread + 0x00000146 (CarbonCore + 0x000bd186)
#  3  0x028ff412 in LThread::Yield(LThread const*) + 0x0000004C (PowerPlantCore + 0x00005412)
#  4  0x028fffb9 in LThread::YieldConsideringPriority() + 0x0000002F (PowerPlantCore + 0x00005fb9)
#  5  0x000d98b2 in  + 0x00004023 (Microsoft Outlook + 0x000828b2)
#  6  0x000d9863 in  + 0x00003FD4 (Microsoft Outlook + 0x00082863)
#  7  0x000d12d7 in  + 0x000006A2 (Microsoft Outlook + 0x0007a2d7)
#  8  0x028fff52 in LThread::DoEntry(void*) + 0x0000001E (PowerPlantCore + 0x00005f52)
#  9  0x9a42fe1c in _CooperativeThread + 0x00000131 (CarbonCore + 0x000bce1c)
# 10  0x980055fb in __pthread_body + 0x00000090 (libsystem_pthread.dylib + 0x000015fb)
# 11  0x98005485 in __pthread_struct_init + 0x00000000 (libsystem_pthread.dylib + 0x00001485)
# 12  0x9800acf2 in _thread_start + 0x00000022 (libsystem_pthread.dylib + 0x00006cf2)

X86 Thread State:
 eax: 0xffffffe1  ebx: 0xb039ede8  ecx: 0xb039ed7c  edx:0x982fef7a
 edi: 0x00000020  esi: 0x00000003  ebp: 0xb039edb8  esp:0xb039ed7c
  ss: 0x00000023  eip: 0x982fef7a   cs: 0x0000000b   ds:0x00000023
  es: 0x00000023   fs: 0x00000023   gs: 0x0000000f  eflags:0x00000206


Thread 12:

#  1  0x982fef7a in _mach_msg_trap + 0x0000000A (libsystem_kernel.dylib + 0x00012f7a)
#  2  0x9a430186 in _YieldToThread + 0x00000146 (CarbonCore + 0x000bd186)
#  3  0x028ff412 in LThread::Yield(LThread const*) + 0x0000004C (PowerPlantCore + 0x00005412)
#  4  0x028fffb9 in LThread::YieldConsideringPriority() + 0x0000002F (PowerPlantCore + 0x00005fb9)
#  5  0x000d98b2 in  + 0x00004023 (Microsoft Outlook + 0x000828b2)
#  6  0x000d9863 in  + 0x00003FD4 (Microsoft Outlook + 0x00082863)
#  7  0x000d12d7 in  + 0x000006A2 (Microsoft Outlook + 0x0007a2d7)
#  8  0x028fff52 in LThread::DoEntry(void*) + 0x0000001E (PowerPlantCore + 0x00005f52)
#  9  0x9a42fe1c in _CooperativeThread + 0x00000131 (CarbonCore + 0x000bce1c)
# 10  0x980055fb in __pthread_body + 0x00000090 (libsystem_pthread.dylib + 0x000015fb)
# 11  0x98005485 in __pthread_struct_init + 0x00000000 (libsystem_pthread.dylib + 0x00001485)
# 12  0x9800acf2 in _thread_start + 0x00000022 (libsystem_pthread.dylib + 0x00006cf2)

X86 Thread State:
 eax: 0xffffffe1  ebx: 0xb0934de8  ecx: 0xb0934d7c  edx:0x982fef7a
 edi: 0x00000020  esi: 0x00000003  ebp: 0xb0934db8  esp:0xb0934d7c
  ss: 0x00000023  eip: 0x982fef7a   cs: 0x0000000b   ds:0x00000023
  es: 0x00000023   fs: 0x00000023   gs: 0x0000000f  eflags:0x00000206


Thread 13:

#  1  0x982fef7a in _mach_msg_trap + 0x0000000A (libsystem_kernel.dylib + 0x00012f7a)
#  2  0x9a430186 in _YieldToThread + 0x00000146 (CarbonCore + 0x000bd186)
#  3  0x028ff412 in LThread::Yield(LThread const*) + 0x0000004C (PowerPlantCore + 0x00005412)
#  4  0x028fffb9 in LThread::YieldConsideringPriority() + 0x0000002F (PowerPlantCore + 0x00005fb9)
#  5  0x000d98b2 in  + 0x00004023 (Microsoft Outlook + 0x000828b2)
#  6  0x000d9863 in  + 0x00003FD4 (Microsoft Outlook + 0x00082863)
#  7  0x000d12d7 in  + 0x000006A2 (Microsoft Outlook + 0x0007a2d7)
#  8  0x028fff52 in LThread::DoEntry(void*) + 0x0000001E (PowerPlantCore + 0x00005f52)
#  9  0x9a42fe1c in _CooperativeThread + 0x00000131 (CarbonCore + 0x000bce1c)
# 10  0x980055fb in __pthread_body + 0x00000090 (libsystem_pthread.dylib + 0x000015fb)
# 11  0x98005485 in __pthread_struct_init + 0x00000000 (libsystem_pthread.dylib + 0x00001485)
# 12  0x9800acf2 in _thread_start + 0x00000022 (libsystem_pthread.dylib + 0x00006cf2)

X86 Thread State:
 eax: 0xffffffe1  ebx: 0xb09b6de8  ecx: 0xb09b6d7c  edx:0x982fef7a
 edi: 0x00000020  esi: 0x00000003  ebp: 0xb09b6db8  esp:0xb09b6d7c
  ss: 0x00000023  eip: 0x982fef7a   cs: 0x0000000b   ds:0x00000023
  es: 0x00000023   fs: 0x00000023   gs: 0x0000000f  eflags:0x00000206


Thread 14:

#  1  0x982fef7a in _mach_msg_trap + 0x0000000A (libsystem_kernel.dylib + 0x00012f7a)
#  2  0x9a430186 in _YieldToThread + 0x00000146 (CarbonCore + 0x000bd186)
#  3  0x028ff412 in LThread::Yield(LThread const*) + 0x0000004C (PowerPlantCore + 0x00005412)
#  4  0x028fffb9 in LThread::YieldConsideringPriority() + 0x0000002F (PowerPlantCore + 0x00005fb9)
#  5  0x000d98b2 in  + 0x00004023 (Microsoft Outlook + 0x000828b2)
#  6  0x000d9863 in  + 0x00003FD4 (Microsoft Outlook + 0x00082863)
#  7  0x000d12d7 in  + 0x000006A2 (Microsoft Outlook + 0x0007a2d7)
#  8  0x028fff52 in LThread::DoEntry(void*) + 0x0000001E (PowerPlantCore + 0x00005f52)
#  9  0x9a42fe1c in _CooperativeThread + 0x00000131 (CarbonCore + 0x000bce1c)
# 10  0x980055fb in __pthread_body + 0x00000090 (libsystem_pthread.dylib + 0x000015fb)
# 11  0x98005485 in __pthread_struct_init + 0x00000000 (libsystem_pthread.dylib + 0x00001485)
# 12  0x9800acf2 in _thread_start + 0x00000022 (libsystem_pthread.dylib + 0x00006cf2)

X86 Thread State:
 eax: 0xffffffe1  ebx: 0xb0a38de8  ecx: 0xb0a38d7c  edx:0x982fef7a
 edi: 0x00000020  esi: 0x00000003  ebp: 0xb0a38db8  esp:0xb0a38d7c
  ss: 0x00000023  eip: 0x982fef7a   cs: 0x0000000b   ds:0x00000023
  es: 0x00000023   fs: 0x00000023   gs: 0x0000000f  eflags:0x00000206


Thread 15:

#  1  0x982fef7a in _mach_msg_trap + 0x0000000A (libsystem_kernel.dylib + 0x00012f7a)
#  2  0x9a7bcbf9 in ___CFRunLoopServiceMachPort + 0x000000A9 (CoreFoundation + 0x00076bf9)
#  3  0x9a7bc1d1 in ___CFRunLoopRun + 0x00000571 (CoreFoundation + 0x000761d1)
#  4  0x9a7bb9ea in _CFRunLoopRunSpecific + 0x0000018A (CoreFoundation + 0x000759ea)
#  5  0x9a7bb84b in _CFRunLoopRunInMode + 0x0000007B (CoreFoundation + 0x0007584b)
#  6  0x961d1355 in +[NSURLConnection(Loader) _resourceLoadLoop:] + 0x0000017D (Foundation + 0x0006d355)
#  7  0x961d11ce in -[NSThread main] + 0x0000002D (Foundation + 0x0006d1ce)
#  8  0x961d1126 in ___NSThread__main__ + 0x00000592 (Foundation + 0x0006d126)
#  9  0x980055fb in __pthread_body + 0x00000090 (libsystem_pthread.dylib + 0x000015fb)
# 10  0x98005485 in __pthread_struct_init + 0x00000000 (libsystem_pthread.dylib + 0x00001485)
# 11  0x9800acf2 in _thread_start + 0x00000022 (libsystem_pthread.dylib + 0x00006cf2)

X86 Thread State:
 eax: 0xffffffe1  ebx: 0xb0bbdf18  ecx: 0xb0bbddfc  edx:0x982fef7a
 edi: 0x00000c00  esi: 0x07000006  ebp: 0xb0bbde38  esp:0xb0bbddfc
  ss: 0x00000023  eip: 0x982fef7a   cs: 0x0000000b   ds:0x00000023
  es: 0x00000023   fs: 0x00000023   gs: 0x0000000f  eflags:0x00000206


Thread 16:

#  1  0x98304046 in ___workq_kernreturn + 0x0000000A (libsystem_kernel.dylib + 0x00018046)
#  2  0x9800acce in _start_wqthread + 0x0000001E (libsystem_pthread.dylib + 0x00006cce)

X86 Thread State:
 eax: 0x00100170  ebx: 0x00012637  ecx: 0xb0080f9c  edx:0x98304046
 edi: 0x98006c69  esi: 0xb0081000  ebp: 0xb0080fc8  esp:0xb0080f9c
  ss: 0x00000023  eip: 0x98304046   cs: 0x0000000b   ds:0x00000023
  es: 0x00000023   fs: 0x00000023   gs: 0x0000000f  eflags:0x00000282


Thread 17:

#  1  0x98304046 in ___workq_kernreturn + 0x0000000A (libsystem_kernel.dylib + 0x00018046)
#  2  0x9800acce in _start_wqthread + 0x0000001E (libsystem_pthread.dylib + 0x00006cce)

X86 Thread State:
 eax: 0x00100170  ebx: 0xa14e6190  ecx: 0xb0114f9c  edx:0x98304046
 edi: 0x98006c69  esi: 0xb0115000  ebp: 0xb0114fc8  esp:0xb0114f9c
  ss: 0x00000023  eip: 0x98304046   cs: 0x0000000b   ds:0x00000023
  es: 0x00000023   fs: 0x00000023   gs: 0x0000000f  eflags:0x00000282


Loaded modules:

0: Microsoft Outlook (14.4.6.141106 Reg=en Loc=0x0409: /Applications/Microsoft Office 2011/Microsoft Outlook.app/Contents/MacOS/Microsoft Outlook
...

Git Completion and Git Flow Completion

Just updated git this morning from because of a security issue and happy that what I have in my .bashrc still working. A lot trimmed out, but here is the gist of it:

# ensure git from gitscm will be used
export PATH=/usr/local/git/bin:/usr/local/bin:/usr/local/sbin:$PATH
source /usr/local/git/contrib/completion/git-completion.bash
# if fails, do find /usr/local/Cellar -name git-flow-completion.bash to find path and set it here
source /usr/local/Cellar/git-flow-avh/1.8.0/etc/bash_completion.d/git-flow-completion.bash
source /usr/local/git/contrib/completion/git-prompt.sh
PS1='\h:\W$(__git_ps1 " (%s)") \u\$ ';

Updated: Jan 2015

That assumes that you have latest git from git-scm.com and are using git flow and git flow completion via brew install git-flow-avh.

Middle Truncate in Ruby

Here's a way to truncate the middle of a String, shortening it to the desired length, in Ruby:

class String
  def middle_truncate(len)
    return self if len >= size
    return self[0...len] unless len > 4
    half = len / 2.0
    (result = dup)[(half - 1.5).floor...(1.5 - half).floor] = '...'
    result
  end
end

This has the added benefit of just truncating if string size < 5.


e.g. for an even-sized string:

    2.1.1 :001 > s = "12345678901234567890"
     => "12345678901234567890" 
    2.1.1 :002 > s.middle_truncate 21
     => "12345678901234567890" 
    2.1.1 :003 > s.middle_truncate 20
     => "12345678901234567890" 
    2.1.1 :004 > s.middle_truncate 19
     => "12345678...34567890" 
    2.1.1 :005 > s.middle_truncate 18
     => "1234567...34567890" 
    2.1.1 :006 > s.middle_truncate 5
     => "1...0" 
    2.1.1 :007 > s.middle_truncate 4
     => "1234" 

and for an odd-sized string:

    2.1.1 :012 > s = "123456789012345678901"
     => "123456789012345678901" 
    2.1.1 :013 > s.middle_truncate 22
     => "123456789012345678901" 
    2.1.1 :014 > s.middle_truncate 21
     => "123456789012345678901" 
    2.1.1 :015 > s.middle_truncate 20
     => "12345678...345678901" 
    2.1.1 :016 > s.middle_truncate 19
     => "12345678...45678901" 
    2.1.1 :017 > s.middle_truncate 5
     => "1...1" 
    2.1.1 :018 > s.middle_truncate 4
     => "1234" 

Shared on StackOverflow

Monday, December 1, 2014

Quick-and-Dirty Median in SQL

It's happened a few times where I want a median in postgres for something, but don't feel strongly enough about it to define a function temporarily or permanently.

So, here's the quick-and-dirty SQL for getting a median that I write in those times:

select some_column from some_table order by some_column offset (select count(*) from some_table) / 2 limit 1;

the problem with that way is that you have to specify where clauses in two places, but it is quick-and-dirty. E.g.:

select some_column from some_table where created_at > '2014-01-01' order by some_column offset (select count(*) from some_table where created_at > '2014-01-01') / 2 limit 1;

Change * to id for better performance.

The subsequent min, avg, max are just selecting min(some_column), avg(some_column), and max(some_column).

There are more accurate ways to do it, but that is ok for a reality check.