Javscript Regex Cheatsheet

I always forget how to do this:

r = /^[^0-9]+/


string = '1a'

me = 'b2'

/* this returns false */

string.match(r);

/* this returns true */

me.match(r);

then how about capture groups – you can’t iterate over them in one go, that’s the problem, you have to do it via exec() which you run multiple times on the same str variable, weirdly enough. The return value is an array with always just two elements: the entire match and the capture group match.

let str = '<h1>Hello, world!</h1> <h1>Hello1, world!</h1> <h1>Hello3, world!</h1>';
 
 let reg = /<h1>(.*?)<\/h1>/g;
 
 let match;
 
 while (match = reg.exec(str)) {
 console.log(match);
 }

voila – you get first the 2 elements, “<h1>Hello, world!</h1>” and “Hello, world!” and then the two elements, “<h1>Hello1, world!</h1>” and “Hello1, world!” and so on

How Complex Is It To ‘Re-skin’ WordPress Pages

Someone asked me this recently, and further explained themselves:

I have a WordPress template that I’m using and I want to completely re-design a couple of pages (visual changes only). I can create the front-end layouts, but I’m interested in learning about how complex/lengthy the process can be to implement said front-end layout, or if it’s a fairly minimal task.

Here’s what I said back: It depends on how familiar with frameworks where the front-end is generated on the server-side (HAML with Ruby, say, as another example.) WordPress uses PHP based server-side rendering – Continue reading “How Complex Is It To ‘Re-skin’ WordPress Pages”

Docker Compose Steps

Quick note to remind myself how to re-run my Docker containers:

  1. Is my Docker Compose part of a network? Make sure all required services are running elsewhere.
  2. Did I change anything in the build steps? (E.g., with Rails, did I change the Gemfile?) – run docker-compose build --no-cache
  3. Clean things up: Run an rm step that can remove exited containers.

Continue reading “Docker Compose Steps”

Books for Beginner Coders

Someone emailed me and asked what good books to read for beginning-level coders.

I usually recommend that if you are starting to learn how to code, you should first extend your research by asking the same question (“What’s a good beginners’ book for…?”) for a list of popular programming languages – here’s a highly selective list I got Continue reading “Books for Beginner Coders”

Android Fundamentals for Programmers

This post documents some fundamentals of Android programming, for someone who’s already been programming in other languages. Here are things you should know:

Rage and Rant

The Android layout XML markup basically re-invents CSS. Fuck you, Google. Why can’t you have just extended open standards, and made it easier on all of us? “Do the Right Thing,” my sainted ass.

XML attribute to make your view be as big as its parent? match_parent. Heard of divs, Google? What’s wrong with fucking HTML divs?

Bah. Anyway, smoke on that a bit first, and now let’s go on.

Classes

  • Activity is a class in the Android SDK. An activity is responsible for managing user interaction with a screen of information.
  • Every widget is an instance of the View class or one of its subclasses (such as TextView or Button.)
  • ViewGroup is a View subclass that contains and arranges other widgets.
    • Other ViewGroup subclasses are FrameLayout, TableLayout, and RelativeLayout.
    • FrameLayout is the simplest ViewGroup and does not arrange its children in any particular manner. The arrangement will use the android:layout_gravity attribute, instead of the ViewGroup’s pre-determined layout.
  • Sometimes, you want your widgets to have “resource IDs.” To generate a resource ID for a widget, you include an android:id attribute in the widget’s definition.
  • There is a + sign in the values for attributes that you are creating and not merely referencing.
  • Some class names are food-themed. Why?

MVC

  • Everything on screen is a “view”; that is, a View. So to create an interaction, you have to get the view via its ID (that you set in a corresponding resource XML file), and then call a method on the appropriately-casted View object that sets an event listener. This means you have to learn a whole new vocabulary of event listener methods, instead of finding pleasantly that the methods are named the same as, oh I don’t know, some library that already implements event-based actions for a visual interface, now I wonder where one could find such a language…

The View

  • Pulling things into the view involves specifying them in your content_*.xml file. These things are pulled in either as strings (android:text=”@string/…) or images (android:drawableLeft=”@drawable/…) I guess there are other possible “drawable” values you can set to put your images in different parts of a given widget. At least one of them is drawableEnd, which I am guessing intelligently moves the right-most image object to the left-most spot, for right-to-left screens.
  • The type of view used depends on the device configuration and other factors. The resources corresponding to each of these is in its own, appropriately-named, folder. There is a list of configuration qualifiers that Android recognizes and the pieces of the device configuration that they refer to.

Logging

  • The Log.d(String, String, Throwable) signature logs the entire stack trace at the point the Log.d function was called, if the third argument is simply a “blank” instance of Exception (i.e., new Exception())
  • One can debug using “exception breakpoints” which is nice – and is in fact a feature of Byebug too, one I wasn’t aware of.

The IDE

I’ll say this about statically-typed languages, though – it’s nice to have an IDE that can do fairly complex code analysis. Knowing that static fields can be converted to local variables? Gold.

Some of the IDE actions could equally be done in dynamically interpreted languages – finding the library that defines a symbol name, say, or knowing that a defined variable has never been used (which is in fact something Perl will tell you in warnings mode, so it’s a pity other languages aren’t as good about it. (I’m looking at you, Ruby.))

Java

  • Oh yay. Java won’t apply the modulus operator to negative numbers to return a positive value. Which is exactly how none of the dynamically interpreted languages do it. Fun.

The Emulator

  • To rotate, hit Fn-Ctrl-F12

Resources

Some of these resources will probably come in handy later:

 

Some Common Tricks with Ruby on Rails

Today we will use Ruby on Rails along with some Javascript libraries to code a few common patterns –

  • Autocomplete in a text box
  • Uploading files by dragging and dropping them into an area inside a browser.

Autocomplete

The Javascript library, jQuery.token-input, is great for quickly building a Javascript-based autocomplete solution. You have to do only three things to get started: Continue reading “Some Common Tricks with Ruby on Rails”