Thursday, February 07, 2019

Why Ruby MatchData Returns Empty String

This one took us a little while to discover, and I've long had a rule that if I have a problem, chances are someone else might have that problem, so if I find the solution, I should share it.

We have a situation where there's a bunch of company names we want to disqualify from a certain feature.  Pruned way down, it ends up looking like this:

filtered_companies = %r{ups|


if company.match(filtered_companies)
    ...bounce the user elsewhere...

And we found that a case like this was bouncing the user.

Therefore, the result of the company match was returning a truthy value.

Looking closer we saw that it was returning "", the empty string.

But that's not right -- a failed match is supposed to return nil.  So what the heck was going on?

I'm wondering if anybody reading has already spotted it.

Look at the definition of our regexp at the top.  See that trailing pipe character?  That's the culprit. What that ends up saying in the creation of the regexp is, "empty strings are allowed."  So the match conditional isn't saying "I didn't find anything," it's saying, "You told me that an empty string is a valid match, so I'm telling you, the string that you gave me contains empty strings."  Of course it does - any string will contain an empty string :)

Take out the trailing pipe and magically the code above returns nil like it's supposed to, failing to match filtered companies and proceeding forward without bouncing the user.

That's ugly code but what I expect happened is that the list was manually edited at some point and somebody deleted some bottom lines, leaving the trailing pipe behind.  Easy mistake to make.  Your code may not look like that at all, but the underlying cause is still possible -- check to see if the regexp that you created is allowing empty strings to sneak through unexpectedly.

Wednesday, January 03, 2018

Back From The Dead

Hi there.  If you're suddenly seeing a burst of posts from this blog, it's only fair that you get an explanation.  I haven't posted here since 2015.  Honestly I thought I'd de-activated the thing, but I still occasionally get email telling me that someone has commented.

But then it occurred to me - I actually read a lot of books.  I have a 2 hour commute to work, and with the power of audio books on fast speed I've finished over 50 books this year.  Many but not all of them are of a technical nature.  To date all I've been doing is posting my thoughts on the company slack channel.  So it occurred to me, why not share those thoughts here as well? Might as well take advantage, the work's already done.  If more people get value of out it, all the better.

The only question is whether to focus entirely on the technical ones (such as the samples I just dropped) or basically post about everything I read, which ranges from science fiction to self help.  If anybody's reading enough and cares enough to comment, I'll be paying attention.

Book Review - Hit Refresh: The Quest to Rediscover Microsoft's Soul and Imagine a Better Future for Everyone by Satya Nadella

Nearing the end of Hit Refresh by Satya Nadella, CEO of Microsoft.  I don’t generally like Microsoft, and didn’t expect to like this, I grabbed it mostly because I hadn’t done much business/tech reading lately.  I’m very happy to report I’m enjoying it very much.  Gone are the days of “Microsoft Over All” and crushing the competition through all means necessary.  This guy’s all about “Here’s the mistakes we made, we know that.  Here’s how we’re trying to fix things.  Past rivals are now partners because the end goal should be benefit to the consumer (somebody tell that to Google and Amazon fighting over YouTube), and companies should work to find common ground, not fight it out in the courtroom.

He comes off as much more likable than you might think, readily owning up to sometimes big mistakes - like when he very publicly said that women should not ask for a raise but rather put their faith in the system.  (

Or how buying Nokia seemed like a good idea at the time, promptly got them sued by Samsung, and ended up as a giant write off anyway.

I don’t think Microsoft will ever again be the power they were in the 80's and 90's, and I’m ok with that.  It makes it easier to pay attention to what they’re doing in technology and innovation without worrying about whether they will cheat their way to the top through any means necessary.  If it should happen that their tech really does float to the top on its own merits, maybe all the old hatchets will indeed be buried.  After all, it was under this guy that you started seeing “Microsoft Loves Linux” marketing, and Microsoft apps running on iPhones.

Book Review - Life 3.0: Being Human in the Age of Artificial Intelligence by Max Tegmark

Finished Life 3.0 over the weekend.  Hmmm.  Let’s put it this way - I ran to this book entirely because of its connection to Elon Musk.  If I described Elon Musk as a billionaire genius who believed that for the good of the human race we need to escape the planet, he might sound like something out of a Robert Heinlein novel. But then you read his biography and you think, “Damn, everything he’s doing makes sense, that could actually work.”  So when I saw his name attached to Life 3.0 I thought that I might get a similar look at the future of machine learning - sure we’re talking about moonshots but let’s break it down to incremental, believable steps….

This book does that a little bit, at least in theory, and at least in the beginning.  He started with some very grounded examples of how machine learning is evolving, and how quickly and unexpectedly (take for example the solution to Go, which was not expected for a long long time).  But before you know it he has jumped to galaxy sized super intelligences warring with each other via doomsday devices that will take out entire planets.  Not kidding. Then he turns around and argues that he personally doesn’t believe there’s any other life in the universe.  I’m not sure how he can reconcile the one with the other, but anyway.

The book is very similar in philosophy to the “Well, the sun’s going to burn out someday, therefore it’s our duty to figure out how to get out of this solar system” argument.  Only he’s arguing that one option for the future of humanity is through AI, the digitization of consciousness, and other stuff like that which you’d normally leave to science fiction.  There’s sections on Dyson Spheres, quantum drives, all kinds of great stuff you’ve read about, as if it’s just a foregone conclusion that we should assume will exist one day, now let’s talk about the best way to use it.

At the end he finally comes back around to reality and how all he has wanted to do is start some conversation among the world’s smartest thinkers that an “AI apocalypse” could happen, in theory, and that therefore avoiding it should be on the table in modern discussion.  He spends a lot of time upset about how every time he gets media coverage it’s always “Elon Musk and Stephen Hawking predict killer robots!”  Which I can see.  I’ve read many of those articles.  He’s not a big “singularity” guy, he’s not big on robots.  He argues persuasively that it’s a more gradual thing that you’ll never even realize is coming, until it’s too late to stop it.  Still, though, it’s hard to make some of the leaps he makes.  There’s that Terminator-esque “it decided that it wanted to survive, and the best way to do that …” leap in the logic where it’s hard to follow where he leads.  That just flies in the face of today’s focus on supervised learning.  If the computer says, “I think the best option here is to kill all the humans”, it’s the humans job to put that one in the “No” category and let it go back-propagate itself.  To get around that you have to assume that we’ve reached the point where it’s all unsupervised, which means we’ve reached the point where our trust in the system is so great that we expect it to never make mistakes, and have built in no safeguards, and I just don’t believe that such a day is coming any time soon.

Book Review - The Master Algorithm How the Quest for the Ultimate Learning Machine Will Remake Our World by Pedro Domingos

Finished The Master Algorithm by Pedro Domingos, a book I was so excited to get to that I went with ebook->text-to-speech because I couldn’t locate an audiobook copy.  Loved it.  Highly recommended as a modern introduction to what’s up in the world of machine learning.  It’s not a history book that tries to say “Here’s how it all got started 70 years ago”, but it does dip backward in time to provide context when necessary.  The authors got no problem stating his opinions, not afraid to say “And here’s why he’s completely wrong” about men like Marvin Minsky, Doug Lenat and Ray Kurzweil.

The book does get pretty heavy into the computer science at points (especially after glossing over some of the earlier schools of thought and digging deeply into Bayesian networks), so it’s hard to follow in audio and I’m not sure how quickly somebody without a math and/or CS background will get lost in the details.  Suffice to say he doesn’t hand wave over anything and say “This technique kind of works like this.”  He shows examples, and the math for how those examples work.

Perhaps not surprisingly, given how I pick these books, he eventually overlaps with Life 3.0, talking about the potential AI apocalypse, the rise of the machines, singularity, unemployment, basic income and all those other modern issues that show up in the headlines these days.  But it’s not the premise of the book, like with Life 3.0.  This one is rooted in implementation.  He even provides links to his own “master algorithm” project (called Alchemy) with encouragement to go off and build your own.

Friday, May 01, 2015

Review : Fitbit Charge HR

Even though I don't always get 10,000 steps a day I love my Fitbit One. I'm a firm believer in the psychology of "if you measure something, you will naturally be motivated to improve your score."  My biggest problem with the Fitbit One is that it goes in my pocket, and then I forget about it for the day until I take it out at night. I never stop during the day to wonder how many steps I'm at. If it gets to be 11pm and I take it out for the night and I'm at 8000 steps, I don't really have an option.

What I *do* do is spend the day wondering if I've accidentally lost it.  If I've got my car keys or something in my pocket, then every time I take my keys out I'm wondering if my One came out with them and fell on the ground. That's annoying.

The other problem with the One is that there's plenty of activity beyond just steps. You start to feel like you're cheating yourself when you realize you're not getting credit for stuff like pushups, situps, various other cardio exercises that aren't distance related.

So when Fitbit announced the Charge HR I knew I wanted one. The Charge HR has two things going for it right out of the box. First it goes around your wrist, so that takes care of my "never look at it" and "afraid I'm going to lose it" worried.  I have no interest or plans in getting an Apple Watch anytime soon so that's not a consideration at all.   Second, the watch actually has a pulse monitor built in so, at least in theory, not only are you tracking your steps during the day but you're tracking your activity level. So if you're not walking anywhere but you're still getting your heart rate up where it needs to be, you get credit for that.

Mine arrived last week.  Here's my review.

Right off the bat, don't make the same mistake we did - this device comes in sizes.  Don't just walk into the store and grab one off the shelf like my wife did, you'll end up with a Small that will barely fit your kid.  We had to return mine (a birthday present), only to discover that the Large size was sold out. Had to get it online.  When the Large arrived I'm still pretty close to the end of the band, but it's not worth the effort to go get an XL (not even sure if it comes in an XL).

The band takes some getting used to. It's rubber, not leather, so not only will there be no "It starts to wear down into a groove where I keep it buckled," but there's a texture there that's actively working against you when you try to wrap it around your wrist. Since it's a device you'll be taking off every time you shower, you're going to have to get used to that.

The standard functions are all there - steps, distance, calories. I'm not even going to bother reviewing those, because they work the same as the rest of the Fitbit devices. What's interesting to me is what else it does.

The pulse monitor works by this LED technique that apparently is measuring the constriction of blood vessels. Pretty cool. I was worried about the accuracy, though, based on what I'd read ahead of time. The instructions for properly positioning the device are a little off putting as well -- you're told to keep the band loose, but to wear it several finger lengths above your wrist bone. Do they not realize that, since the natural position of your arms is straight down, that gravity is going to pull the band down to your wrist bone most of the time?  The good news, though, is that I have access to actual medical devices to measure my pulse, so I did a side by side comparison. I'm happy to report that I got a perfect 1:1 match.

An interesting side effect to this feature is that you've got a pulsing LED on your wrist, which means that in a dark room if you bend your wrist the right way and the sensor does not sit flat against your skin, suddenly you're Cyclops when somebody rips off his visor, with laser beams shooting in random directions.

The device has a few interesting features as well. It vibrates, which turns out to be quite useful. When you hit your step goal for the day, it'll vibrate to let you know. You can also, with the help of the dashboard, use it as a silent alarm.  I'm not sure if I trust a little buzzing on my wrist to be my primary alarm, but it works very well for my particularly situation. My wife and I wake up at 5:30am during school days, and then watch the news for about half an hour until we have to get the kids up. And depending on how sleepy we are, afraid that we'll fall back to sleep, I end up having to turn over to look at the clock every 5 minutes to make sure that we're not oversleeping.  Well, not anymore. My Fitbit is set to buzz Mon-Fri at 6am. Done.

Which brings up a good point that I haven't mentioned, there's an actual clock/calendar in the thing too. So it doesn't just look like a watch, it's an actual watch.

Apparently it will also do caller ID, though I have not yet tested that feature. Perhaps as a way to keep a little more competitive with the encroaching smart watch market, the device is already synced to your phone so why not take advantage? When someone calls, your fitbit will tell you who it is. You can't answer/deny the call, but at least you can decide whether to take it.

So far I love it. The HR monitor is working like I expected. It's comfortable. I'm more aware of the device on my wrist, checking it throughout the day, than I ever was with the one I had to pull out of my pocket.

Wednesday, April 29, 2015

Why Does My Google Cloud Print Printer Keep Going Offline?

When we got a Chromebook for the house I had no idea what "Google cloud print" even was.  It's turned out to be more of a pain in the neck than anything else and I'd gladly go back to the old way if I could. I've never had to temporarily share a printer with my neighbor, or print something at home when I was at work, or any of the other situations it is supposed to make easier.

Worst of all, the printer regularly goes "offline".  This is the most frustrating error message in the world, because your printer is on, it is connected, it prints test sheets fine, you may even  have some real computers available that do it the old fashioned way, and those can print fine. But cloud print says it's offline and offers no suggestions for how to fix it, other than to go messing with your firewall  - something that no doubt most of the folks opting for Chromebooks know exactly how to do, right?  This became such a problem that we bought a new, "cloud print ready" printer thinking that it was our old "classic" printer's problem.  Wasn't.

Here's a solution to the problem that may work for you. When you initially register printers with the cloud print service, they are associated with a device. That device has to be online to see the printer!  I learned this the hard way when my daughter got a Chromebook and I registered it using my laptop, which only made sense because that's where I do 99% of my computer work.  The problem was that whenever I closed the laptop and went off to work?  Printer stopped working.

After figuring that out we re-registered the printers using our server (a Mac mini) which is up all the time, and the problem basically went away.

In fact, it went away so well and for so long that when my brand new printer went offline this week (and claimed it had been offline for days) I was furious.  I started the whole process all over again.

Then I remembered that over the weekend my son had friends over and one of them had accidentally unplugged the server.  Even though I'd plugged it back in I had not powered it back up.

Went downstairs, powered it up, and look at that, my printer's back online.

Tuesday, February 17, 2015

Android : File exists but canRead() returns false

I'm working my way through this developer document about capturing and saving photos and ran into a problem. My photo was saved properly, I could call up the ADB shell and verify it. But when I tried to read it back so that I could display it on the screen, this code would fail:

File imgFile = new  File(mCurrentPhotoPath);
if(imgFile.exists()) {
    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
    ImageButton btn = (ImageButton)findViewById(;

Sound familiar? I had requested the WRITE_EXTERNAL_STORAGE permission properly (which implicitly gets the READ_EXTERNAL_STORAGE permission as well), so that wasn't the problem.

It turns out that the file does indeed exist, but it was failing on canRead().  Since canRead()==false, exists() was also returning false.

Ok, then, why can't I read it?

The problem lies in the photoPath, and whether to use a URI or a regular file path descriptor.  The code that we're talking about ends up structuring a unique image filename that looks like this:


which worked fine to save the file. But on a hunch I tried a pure file path:


and guess what? That worked.

Wednesday, January 07, 2015

Android APK Can Not Press Install Button

There's lots of reasons why you might want to load an APK file directly onto your Android device. In my case I'm developing an app and didn't want to always have a USB connection setup, so instead I save the APK to my Dropbox directory, and then it appears nicely on my device.

But a problem appeared recently. I get a dialog asking to confirm the install, with CANCEL and INSTALL buttons. And the Install button doesn't work.  This is weird.  It's not disabled. It shows no interaction like it even realizes I'm pushing it. The Cancel button continues to work. But Install, no matter what I do, will not work.  I long press it, I do everything I can think of. Nothing.

I do have "Allow unknown sources" checked, of course. Just to get that out of the way.

I google around and find references to disabling any "screen dimmer" apps that might be running. Why? I have no idea. But I do run Twilight, which alters the colors of my screen at night so it doesn't glare in my face.

I turn that app off, and look at that! The install button works all of a sudden.

How To Record XBox 360 HDMI To Composite Video

My 8yr old son currently idolizes somebody named Stampy Longnose, who is a character that makes YouTube videos of himself and his friends wandering around Minecraft. For months my son asked for "recording material" so that he, too, could develop a following like Stampy.  We kept saying "Maybe for Christmas, maybe for Christmas." Well Christmas came, and he did not forget, it was at the top of the list.  So his grandparents got this magical recording material for him. Since they had no idea what it is, of course, now it's up to dear old Dad to make it work.  It took me a lot longer than I thought it would, so I'm documenting the process for those other dads that find themselves in a similar situation.

My son has an XBox 360 E. I can only speak for these instructions relative to that particular console. I discovered that this is a big deal, because it appears that our old pal Microsoft has been changing the video output port pretty much every time.

There are many results on Amazon for xbox recording device (I don't know why my son insists on calling it "recording material"), ranging significantly in price. Although we've got our device connected via HDMI I was not prepared to get what seems like the most popular device, the Elgato HD Capture.  After all I have no idea if he's going to get bored with this in a week once it actually works. Instead we went with the Elgato Video Capture, which is almost half the price. I say we, because although this is the gift my in-laws wanted to give him, they had no idea what it is so I was tasked with doing the research and picking the item :)

Christmas came and went, my boy was excited to finally have the device and we went about setting it up ... and I discovered my first problem.  Our device has an HDMI port.  This recording device has composite cables, otherwise known as the classic Red/Yellow/White combination. Do not confuse this with "component cables", which have 5 separate heads including a green one.

This is not a problem. I get back on Amazon and search for "XBox composite cable", getting plenty of results for around $10 or less.  I bought one. DO NOT DO THIS.  I'm deliberately not linking it.

We wait a few Amazon Prime days, cable arrives and ... it doesn't fit. The particular cable I got has this big fat port on one end, bigger than an HDMI port, and my device simply does not have that.  "Aha," I think, "I must have mistakenly gotten the cable for an older XBox model, not for the 360.  My bad."  I return to Amazon, search for "XBox 360 hdmi composite cable," and thinking I see one that has an HDMI port on the end, get that one instead.

Nope.  Same stupid cable with the big fat end.  Now I'm upset.  My device simply does not have that port.  All I have is an HDMI port, and a little 3.5mm jack next to it that I have assumed is a standard audio-out jack.

It's not.  I finally found this document from Turtle Beach that set me straight:
As you can see above, the trapezoidal A/V (Audio/Visual) port from older Xbox 360 models has been removed and replaced with a single 3.5 mm output jack. The Xbox 360 E includes a 3.5mm Composite A/V Cable (yellow, white, and red), used for connecting to standard-definition TVs.
Oh! That explains both my problems.  First, it is the "E" model that got rid of the older A/V port, which explains why I keep getting the wrong cable.  Second, that 3.5mm jack is actually going to spit out both audio and video.  Nice.

What I needed was this cable . Your XBox came with this cable, but if you're like me and immediately plugged into the HDMI port of your television you may no longer know where you put this original cable.  I actually lucked out, I found it gathering dust in a drawer under our television.

Now we're cooking.  Plug the 3.5mm jack into the Xbox, plug the Red/Yellow/White into the recorder, what.  There's a USB port on the other end of the recorder, so we plug that into my Mac, download the software, and it goes into setup mode asking me to plug in the yellow cable and proceed when I see video.  I don't see video. What's weird is that I'm getting audio.  I wonder what the odds might be that this brand new cable is somehow broken, and decide that can't be the case.

I'll fast forward here. Two things.  First, you can't have your HDMI and your A/V both plugged in at the same time. If you do, the video will only go through HDMI, and go to your television rather than your recorder. So unplug your HDMI.  Yes, this means that when you want to record your Xbox you're going to have to get behind your console, unplug it from the tv, and only watch it on the computer. I did not realize this would be the case and I don't think my son did either, but it's not the end of the world.

Second, you can only do this when the power is off.  If you are looking at the "No Video" screen in the Elgato software and unplug the HDMI cable, nothing will happen. Power cycle your XBox and you should now get your video signal!

Once connected, everything else works identically. My son watched me fire up Minecraft and almost get killed in about 3 seconds before he just grabbed the controller and rescued me. But the important part was that it still works identically to the XBox. Nothing about keyboard controls or anything weird.  You're just playing on the smaller screen.

Almost done. Do you want to play XBox Live?  More specifically, do you have one of those headset microphones that allows you to chat with your friends and basically narrate your game playing experience? My son's got the Turtle Beach XL1 which like much of this other stuff was a good entry level item to see whether he's going to stick with it. Something that might be common in the videogame world, that was completely bizarre to me, is that to get the chat working there's actually a special cable that comes off the headphones - and plugs into the XBox controller. Why? I have no idea. But if you want the other people playing the game to hear you, this is a necessary step.

Did I mention that my son's best friend has a similar setup, along with the recording material? When they learned that we'd gotten one for Christmas, the father told me, "You'll have to let me know if you get the chat working, we can't figure it out."

And there's the problem. How do you get that audio to go through the recorder? Turns out, you don't. Here's the word, straight from Elgato:

All gaming headsets we've encountered so far do not down-mix the live chat from the microphone to the audio output of the PS3 or Xbox 360. 

This means that Game Chat audio from the local gamer will not be recorded by Elgato Game Capture HD. 
You will still hear audio from other players during gameplay, and in your recording. 
You will still hear your voice during gameplay, but not in your recording.
That stinks. That might even be a deal breaker, depending on what you were hoping to accomplish. But! I wasn't done yet.  We've come this far and a father hates to disappoint his son.

Elgato points out that their Elgato - Game Capture HD version (you know, the more expensive one I chose not to get?) comes with a software option to do Live Commentary. I can't really say more about that because I don't have it, but perhaps that alone is enough to make you get the more expensive device, if you haven't already made your purchase. I'm not quite ready to go there.

I had one last option. It's not that hard to add voiceover / narration to videos, especially on a Mac. I tried it briefly with iMovie but didn't like the experience, so I ended up with Wondershare Video Editor. This software seems like the last piece of the puzzle.  My son will make his videos, then when he plays them back he can narrate what he did and why he did it. Might take him a little while to get used to that. And I'm not yet sure how that's going to work when he has other friends playing, because remember - the recorder will pick up *them*, but it won't pick up him.

Anyway, that's what we did to make it work. Hope it helps all you dads out there get the next generation of Stampy Longnoses producing their videos!

Saturday, November 15, 2014

Ready, Aim, Pop! How My Son Built His First iOS App

A few weeks ago my son, who is 8, figured out what I do for a living.  He discovered that I know how to put an app in the iOS App Store.

So naturally he asked, "If I make a game, could you make it so people could get my game on their phones?"  I said sure.

Thus began our father/son project for the next couple of weeks. We decided to go with the programming environment Game Salad, which had one serious advantage over the many others -- it could actually produce appstore-ready code, without really doing any serious programming.

My son had pitched an idea that went something like this : "What if these colored balls were falling down the screen and you had to tap all of them except the white one?"  I suggested, "What if instead of falling down, it was balloons going up the screen? And same thing, you have to tap them before they get to the top? The tapping could be like popping them."  He liked that idea, and we were off. Somewhere along the line the "don't tap the white ones" rule morphed into "the clouds are going to start getting in your way but if you tap on one you lose points".

He did all the graphics (ok, with a little help from his sisters) and played all versions of the game as we improved it.  Too fast.  Too slow.  Too many balloons, not enough balloons.  Balloons are too big, too small. Finally we got it just right.

Getting into the app store has a bunch of baggage that very much does not keep the attention of an excited 8yr old boy, like writing descriptions and filling out legal disclaimers and making screen shots in exactly the right size (seriously - if you're one pixel off, it won't accept you). Luckily I had experience with that, as noted, and cranked through the boring details while he was sleeping.

Try #1 we got rejected, because I'd clicked the "Made for Kids" option.  After all, I had no reason to think that it's not for kids, there's no violence or anything like that and it was in the 4+ age category.  But I hadn't considered ... the basic version of GameSalad that we're using puts in advertisements, and I do not control those. That's what got us rejected, the content of those ads.

Try #2 I turned off that option and held my breath. It takes about a week every time you submit an app for consideration so we were heading into two weeks since the last thing my son could actually help with, and I was afraid he was getting bored.

But, success! I got the note at maybe 9pm a few days ago, shortly after he'd gone to bed. So I went upstairs with the laptop, nudged him back awake, and said "Hey, read this for me."  He read, "The following app has been approved and the app status has changed to Ready for Sale....we got in? It's on the store?" I confirmed that it was indeed on the store and he immediately rolled into, "How many downloads do we have?"

I told him to be patient and go back to sleep. :)

I hope you get a chance to download the game. He asks me every morning what his numbers are (he calls them likes, and I haven't seen the need to tell him different :)).  If you think it's worth the extra time maybe throw him a rating/review. It's great to see how proud he is of his accomplishment, and I want to do everything I can to make this a positive experience for him. Maybe he'll want to make more!