Thursday, February 23, 2017

Rendering ERB Like Rails Without Rails

While Rails 5 has the ability to render templates outside of controllers, and you can typically just use partials to share ERB between various parts of your Rails app (e.g. between a mailer's and controller's views), you can get pretty close on your own with just ERB:
require 'erb'

module YourModule
  class YourErbRenderer
    def self.order_info(some_value)
      tempate_context = binding
      tempate_context.local_variable_set(:some_variable, some_value)
      @@template ||= %q(
put ERB code here
)
      ::ERB.new(@@template, nil, '-').result(tempate_context)
    end
  end
end

Looks a bit nasty, but works and can be extended as needed. If you don't pass the binding in, it will create a new binding each time, and if you don't specify '-', it won't interpret the -%> tags as you'd expect in Rails ERB templates.

Wednesday, February 22, 2017

The Actual Bit Rate of Amazon Music MP3's

Ran through some of the albums I'd downloaded from Amazon to check the bitrate of their MP3's, and this is what I found:
$ find ~/Music/Amazon\ Music/ -name "*.mp3" -exec afinfo {} \; | grep "bit rate"
bit rate: 240613 bits per second
bit rate: 237507 bits per second
bit rate: 232091 bits per second
bit rate: 220799 bits per second
bit rate: 234027 bits per second
bit rate: 237728 bits per second
bit rate: 232630 bits per second
bit rate: 250386 bits per second
bit rate: 238442 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 245165 bits per second
bit rate: 246842 bits per second
bit rate: 247407 bits per second
bit rate: 246042 bits per second
bit rate: 246929 bits per second
bit rate: 246250 bits per second
bit rate: 248472 bits per second
bit rate: 243777 bits per second
bit rate: 255999 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 250288 bits per second
bit rate: 291410 bits per second
bit rate: 301090 bits per second
bit rate: 279325 bits per second
bit rate: 280980 bits per second
bit rate: 271870 bits per second
bit rate: 285021 bits per second
bit rate: 270496 bits per second
bit rate: 302363 bits per second
bit rate: 297474 bits per second
bit rate: 286422 bits per second
bit rate: 277760 bits per second
bit rate: 258546 bits per second
bit rate: 277269 bits per second
bit rate: 278519 bits per second
bit rate: 269591 bits per second
bit rate: 277380 bits per second
bit rate: 261386 bits per second
bit rate: 273975 bits per second
bit rate: 282590 bits per second
bit rate: 275151 bits per second
bit rate: 276253 bits per second
bit rate: 265291 bits per second
bit rate: 279540 bits per second
bit rate: 296026 bits per second
bit rate: 272390 bits per second
bit rate: 270808 bits per second
bit rate: 284846 bits per second
bit rate: 274630 bits per second
bit rate: 286973 bits per second
bit rate: 282399 bits per second
bit rate: 281068 bits per second
bit rate: 279751 bits per second
bit rate: 268826 bits per second
bit rate: 268781 bits per second
bit rate: 278829 bits per second
bit rate: 273351 bits per second
bit rate: 278739 bits per second
bit rate: 280021 bits per second
bit rate: 284984 bits per second
bit rate: 266144 bits per second
bit rate: 244760 bits per second
bit rate: 236637 bits per second
bit rate: 246256 bits per second
bit rate: 235838 bits per second
bit rate: 240540 bits per second
bit rate: 249121 bits per second
bit rate: 246689 bits per second
bit rate: 247949 bits per second
bit rate: 241641 bits per second
bit rate: 242181 bits per second
bit rate: 242710 bits per second
bit rate: 243902 bits per second
bit rate: 243433 bits per second
bit rate: 245102 bits per second
bit rate: 258377 bits per second
bit rate: 268417 bits per second
bit rate: 265230 bits per second
bit rate: 259459 bits per second
bit rate: 265979 bits per second
bit rate: 272965 bits per second
bit rate: 268951 bits per second
bit rate: 264826 bits per second
bit rate: 266127 bits per second
bit rate: 264319 bits per second
bit rate: 263733 bits per second
bit rate: 251633 bits per second
bit rate: 272682 bits per second
bit rate: 257620 bits per second
bit rate: 273825 bits per second
bit rate: 258421 bits per second
bit rate: 255999 bits per second
bit rate: 256000 bits per second
bit rate: 255999 bits per second
bit rate: 255999 bits per second
bit rate: 255999 bits per second
bit rate: 256000 bits per second
bit rate: 255999 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 255999 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 255999 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 232623 bits per second
bit rate: 254226 bits per second
bit rate: 229350 bits per second
bit rate: 254723 bits per second
bit rate: 260315 bits per second
bit rate: 240389 bits per second
bit rate: 269637 bits per second
bit rate: 231010 bits per second
bit rate: 280247 bits per second
bit rate: 237397 bits per second
bit rate: 242784 bits per second
bit rate: 252249 bits per second
bit rate: 222670 bits per second
bit rate: 235474 bits per second
bit rate: 223702 bits per second
bit rate: 237320 bits per second
bit rate: 249579 bits per second
bit rate: 239724 bits per second
bit rate: 241570 bits per second
bit rate: 230993 bits per second
bit rate: 239151 bits per second
bit rate: 219546 bits per second
bit rate: 220433 bits per second
bit rate: 233534 bits per second
bit rate: 202524 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 255999 bits per second
bit rate: 270809 bits per second
bit rate: 277614 bits per second
bit rate: 266682 bits per second
bit rate: 279490 bits per second
bit rate: 252332 bits per second
bit rate: 266943 bits per second
bit rate: 258596 bits per second
bit rate: 241047 bits per second
bit rate: 287744 bits per second
bit rate: 250387 bits per second
bit rate: 276334 bits per second
bit rate: 260500 bits per second
bit rate: 263284 bits per second
bit rate: 266930 bits per second
bit rate: 246918 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 238355 bits per second
bit rate: 280957 bits per second
bit rate: 292291 bits per second
bit rate: 265664 bits per second
bit rate: 265548 bits per second
bit rate: 245118 bits per second
bit rate: 240111 bits per second
bit rate: 264827 bits per second
bit rate: 268366 bits per second
bit rate: 277872 bits per second
bit rate: 256000 bits per second
bit rate: 255999 bits per second
bit rate: 256000 bits per second
bit rate: 255999 bits per second
bit rate: 255999 bits per second
bit rate: 256000 bits per second
bit rate: 255999 bits per second
bit rate: 256000 bits per second
bit rate: 255999 bits per second
bit rate: 255999 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 250504 bits per second
bit rate: 260498 bits per second
bit rate: 265215 bits per second
bit rate: 266910 bits per second
bit rate: 249822 bits per second
bit rate: 277456 bits per second
bit rate: 267289 bits per second
bit rate: 255731 bits per second
bit rate: 256118 bits per second
bit rate: 260953 bits per second
bit rate: 267562 bits per second
bit rate: 254396 bits per second
bit rate: 261400 bits per second
bit rate: 248648 bits per second
bit rate: 255076 bits per second
bit rate: 242206 bits per second
bit rate: 263990 bits per second
bit rate: 243265 bits per second
bit rate: 241001 bits per second
bit rate: 253779 bits per second
bit rate: 254448 bits per second
bit rate: 265804 bits per second
bit rate: 238417 bits per second
bit rate: 252808 bits per second
bit rate: 248005 bits per second
bit rate: 237089 bits per second
bit rate: 256389 bits per second
bit rate: 246437 bits per second
bit rate: 255334 bits per second
bit rate: 264892 bits per second
bit rate: 270004 bits per second
bit rate: 292971 bits per second
bit rate: 248628 bits per second
bit rate: 259831 bits per second
bit rate: 269107 bits per second
bit rate: 269528 bits per second
bit rate: 265441 bits per second
bit rate: 251832 bits per second
bit rate: 253204 bits per second
bit rate: 252768 bits per second
bit rate: 255563 bits per second
bit rate: 253801 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 256000 bits per second
bit rate: 252637 bits per second
bit rate: 251750 bits per second
bit rate: 250924 bits per second
bit rate: 254063 bits per second
bit rate: 242433 bits per second
bit rate: 244887 bits per second
bit rate: 251419 bits per second
bit rate: 245235 bits per second
bit rate: 264922 bits per second
bit rate: 247827 bits per second
bit rate: 265629 bits per second
bit rate: 260638 bits per second
bit rate: 257425 bits per second
bit rate: 251850 bits per second
bit rate: 267637 bits per second
bit rate: 260830 bits per second
bit rate: 262022 bits per second
bit rate: 254673 bits per second
bit rate: 261607 bits per second
bit rate: 268397 bits per second
bit rate: 276511 bits per second
bit rate: 258044 bits per second
As you can see, some of the albums were all 256000 but many were lower or higher. The average of this sample of 357 songs was 257295.07 kbps with ~31% (111/357) files being lower than 256000 kbps.

This maps up to what Amazon says in About Media Formats:

Bit Rate
Where possible, we encode our MP3 files using variable bit rates for optimal audio quality and file sizes, aiming at an average of 256 kilobits per second (kbps). Using a variable bit rate allows us to allocate a higher bit rate to the more complex sections of music files while using a smaller bit rate for the less complex sections. The average of these rates is then calculated to produce an average bit rate for the entire file that represents the overall sound quality. Some of our content is encoded using a constant bit rate of 256 kbps. This content will have the same excellent audio quality at a slightly larger file size.

Friday, February 3, 2017

A warning about schema_cache.clear! and reset_column_information in some versions of ActiveRecord

A tip today about reloading column information and schema cache in Rails:

Be careful relying on YourModel.connection.schema_cache.clear! and YourModel.reset_column_information. We noticed some strange errors while calling it a lot under load. Instead, if you can avoid using it (best option) or do a rolling restart of Puma via its tmp_restart plugin (works great and handles requests in process like a champ, but delays requests and could be a horrible idea under load), either of those options might be better choices.

Other options/suggestions:

  • Try not to change column names (and possibly table names) in any DB referred to directly by Rails.
  • Be careful with any gem or code that will search through all columns in an ActiveRecord model.
  • In Rails 5, you could be diligent about adding ignored_columns to models any columns you plan to rename or delete.
  • You could patch your model/define it's columns so that it accurately reflects columns you plan to delete or want to ignore, such that it disregards them.

Thursday, December 15, 2016

Chrome on macOS's "You cannot visit (some host) right now because the website uses HSTS" Error

Do the following at your own risk, and only if you are absolutely sure that the root CA and certificates you delete and add are what you need.

If you get this error in Chrome in macOS:

You cannot visit (some host) right now because the website uses HSTS. Network errors and attacks are usually temporary, so this page will probably work later. Learn more.

and you know that the cert was self-signed under an untrusted root CA, then go to Keychain Access in macOS, click on certificates, and delete any old/incorrect certificates. Then open Safari to a website that has the untrusted root CA and trust it. You should then be able to open up the site in Chrome. Note: sometimes for whatever reason the cert might be on its own and you might trust it, but you need to trust its signing CA as well. Be sure that that you have a ancestry of trust.

Sunday, December 11, 2016

Fusion Power from Stellarator vs. Tokamak

In discussion on HN about the stellarator vs. tokamak this morning about Confirmation of the topology of the Wendelstein 7-X magnetic field to better than 1:100,000, published November 30, 2016 (see the whitepaper describing the work or the Wikipedia article), ars summarizes the difference in layman's terms as the following:

If you squish hydrogen atoms together hard enough, and hot enough, they release energy.

But it needs to be really really hot - so hot that anything you made it out of would melt.

So what do you do?

You use a magnet. The magnet squishes the really hot hydrogen without actually touching it.

But if you squish one side, the hydrogen will want to go out of the other side. So you have to squish all sides exactly the same amount.

It turns out, it's impossible to make a magnet in the shape of a ball that squishes on all sides equally.

But, it is possible to make one in the shape of a doughnut! That's a tokamak. They are complicated because you also have to use the hydrogen inside the tokamak to help make the magnet work and keep the hydrogen inside.

Another shape that works is a kind of twisted doughnut, this is called a stellarator, if you do that, you don't need to also use the hydrogen inside as a magnet, and this makes it easier. But the twisted shape means it's harder to build because you have to put the magnets in exactly the right place.

The name "tokamak" comes from a sentence in Russian describing the machine, and the name "stellarator" comes from a word that means "sun".

stevep98 says the impossibility of the magnet in the shape of a ball is described by the Hairy Ball Theorem.

snarfy then describes a difference of the Tokamak (donut/toroidal shaped) vs. Stellarator:

The main difference is the plasma circulates.

With the donut, the problem is similar to trying to inflate a balloon that has a weak point in it. Blowing causes the weak point to inflate instead of the balloon.

Plasmas are electrically charged and have their own magnetic field. Trying to squeeze down on the plasma inside the donut shape to cause fusion causes the plasma's own magnetic strength to increase and counter the field being applied to it.

The stellarator works with the plasma's magnetic field instead of against it by spiraling it around in circles.

visarga writes:

Is it true that this reactor doesn't use the latest REBCO superconductors? I watched a talk where they claim they can make reactors 10x smaller now, because they increased the strength of the magnetic field (can go up to 10-20T, instead of 3T). And the superconducting tape is cheap, much more flexible and only needs to be cooled to 100K instead of 4K.

If that's true and I'm not mistaken, the MIT ARC reactor would be much better posed to win the race than Wendelstein 7-X, especially that W7X doesn't aim to generate surplus energy. A commenter above was wondering at the expensive diamond window they had to use. This project is going to be too expensive and with dated technology. We could do it cheaper now.

mtgx mentions Lockheed is using the Tokamak design in the ITER and XorNot notes that the Wendelstein 7-X was not designed to have a net generation of energy.

Saturday, December 10, 2016

Reverse Engineering Tools

Some reverse engineering tools mentioned on HN:
  • IDA w/hexrays decompiler - best, very expensive for single developer, more difficult to learn
  • Radare - steep learning curve
  • Binary Ninja - easier
  • Hopper - easier, can do some assembly to C conversion