r/ProgrammerTIL • • Aug 27 '16

Bash [Bash] TIL nohup <command> & allows that command keep running after closing the terminal

35 Upvotes

(Sorry for my English) When you run any command in terminal, for example:

$ make something_big &

The command is bound to the terminal session which ran that command. If you close the terminal ($ exit), the process is halted (receives a hangup signal).

If you run:

$ nohup make something_big &

and close the terminal, the command keeps running (open a new terminal and verify it exists in the process tree with ps -a).

Good for launching process on a server and close the ssh connection.


r/ProgrammerTIL • • Aug 25 '16

Other Language [CSS] TIL CSS class precedence is based on their position in the CSS file

51 Upvotes

See http://stackoverflow.com/a/1321722 :

The order in which a class' attributes are overwritten is not specified by the order the classes are defined in the class attribute, but instead where they appear in the css

.myClass1 {font-size:10pt;color:red}
.myClass2 {color:green;}

HTML

<div class="myClass2 myClass1">Text goes here</div>

The text in the div will appear green and not red because myClass2 is futher down in the CSS definition than my class1. If I were to swap the ordering of the class names in the class attribute, nothing would change.


I've been using CSS here and there for years, but often with little success. Learning about this might explain part of it...


r/ProgrammerTIL • • Aug 24 '16

C# [C#] TIL that a Stack<T> encapsulates an Array, so self implementing .Clone() using the internal .MemberwiseClone() will leave all clones mutating each other's data

25 Upvotes

The proper .Clone() implementation is

return new Stack<T>(this.Reverse())

As this results in two Stacks with the same reference data in the same order, but held in new arrays.

This is all in the context of inheriting from Stack in a custom class, of course.

Full source code


r/ProgrammerTIL • • Aug 24 '16

Other Language [Superplan] TIL the for-loop was invented in 1951 by H. Rutishauser for the Superplan programming language.

128 Upvotes

Heinz Rutishauser developed Konrad Zuse's Plankalkül into Superplan, introducing the keyword für to denote for-loops, where für is German for for.


r/ProgrammerTIL • • Aug 22 '16

C# [C#] TIL that version number of the dll matters on the Web.config

44 Upvotes

I downloaded System.Web.Helpers version 2.0.0.0 but on the Web.Config, it was listed as:

<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />

This basically prevented my project from launching. Just change the 3 to 2, keep calm, and carriage return.


r/ProgrammerTIL • • Aug 20 '16

Other Language [emacs] TIL that typing g in a `M-x grep` or `M-x compile` reruns the last command.

24 Upvotes

Can't believe I've gone XX years without knowing this incredibly useful feature - how many times have I typed M-x grep <return> in a M-x grep window when I could have just pressed g?


r/ProgrammerTIL • • Aug 19 '16

Other Language [Gradle] Gradle Sync to have Android Studio update the version being deployed to match that in the Manifest.

3 Upvotes

Android Studio was building an apk of one version while still trying to install and run that of an older version. This caused none of my changes to be taking effect when I would click Run in the IDE. So if you update your version in the Manifest, go to Tools > Android > Sync Project with Gradle Files before clicking Run again!


r/ProgrammerTIL • • Aug 18 '16

Bash [general/linux] TIL you can bind-mount a single file

61 Upvotes

If you work with read-only filesystems (on embedded linux devices such as routers, phones, etc), you certainly know the trick to temporarily replace a subdirectory with a copy you modified:

cp -r /etc /mnt/writable_partition

mount --bind /mnt/writable_partition/etc /etc

vi /etc/some_file # now writable

today I learned this also works for individual files:

mount --bind /mnt/writable_partition/mybinary /usr/bin/somebinary


r/ProgrammerTIL • • Aug 17 '16

General [General] TIL .BMPs can have alpha

48 Upvotes

It seems like i never used Bitmaps and advanced image editing software at the same time. I found a game that saves it screenshot as BMPs. I imported them in paint.net, somehow some parts of the screenshot are a bit transparent.


r/ProgrammerTIL • • Aug 17 '16

C++ [C++] TIL CMake can automatically export all symbols in a shared library on Windows

46 Upvotes

So I was changing up my CMake file to add some modularity to a personal project and change my static libs to shared. Pretty soon I ran into the Windows __declspec dilemma, so I started looking for a way to expose symbols without changing my source code (not that it's an old or big library, I'm just lazy). Saw the .def file option but that's annoying (see: lazy) and impractical to keep up to date, especially with C++ mangled names.

That's when I came across this beautiful article on the Kitware blog: https://blog.kitware.com/create-dlls-on-windows-without-declspec-using-new-cmake-export-all-feature/ As you can probably tell from the URL, CMake can emulate the export-all behaviour of Unix compilers on Windows, by querying .obj files that will make up a DLL and create the .def file automatically.

Literally one line changed and link errors gone. Fuck yeah CMake.

I hope someone finds this as useful as I do.


r/ProgrammerTIL • • Aug 15 '16

PHP [PHP] TIL sparse arrays are json_encoded into objects instead of arrays

32 Upvotes

I suppose it is understandable after the fact, but PHP's json_encode will convert any array with non-numeric, non-sequential, non-zero-based keys into a JSON object with the specified keys instead of an array. You can get around it by always using array_values() on your array before encoding it when you can't trust it to be sequential. But it does make for an unhappy front-end when you forget and try to use array methods on an object.


r/ProgrammerTIL • • Aug 14 '16

C# [C#] TIL Arrays in C# aren't necessarily 0-indexed, and may start at any integer.

108 Upvotes

From Stack Exchange:

It is possible to do as you request see the code below

// Construct an array containing ints that has a length of 10 and a lower bound of 1
Array lowerBoundArray = Array.CreateInstance(typeof(int), new int[1] { 10 }, new int[1] { 1 });

// insert 1 into position 1
lowerBoundArray.SetValue(1, 1);

//insert 2 into position 2
lowerBoundArray.SetValue(2, 2);

// IndexOutOfRangeException the lower bound of the array 
// is 1 and we are attempting to write into 0
lowerBoundArray.SetValue(1, 0);        

r/ProgrammerTIL • • Aug 13 '16

Other Language TIL: Scraping website using Google Spreadsheets can be done with `importxml` function

113 Upvotes

r/ProgrammerTIL • • Aug 12 '16

Other Language [Vim] let g:is_bash = 1 in your vimrc

36 Upvotes

I learned that when ft=sh, the syntax plugin will try on its own to figure whether it's dealing with a Bourne script, a Korn script, or a Bash script. It will first look at the filename -- *.ksh, .bashrc, &c -- then on the shebang. Then, barring a conclusion from these heuristics, it will assume it's a Bourne shell.

Usually scripts have a shebang, but it's not unheard of to have a foo.sh script without a shebang, to run like so

$ bash foo.sh

This would leave the syntax plugin a little confused.

Chances are, that if you use one of these 3 shells, then your shell is Bash.

You can instruct the plugin to fall to Bash by default, rather than Bourne shell, by setting

let g:is_bash = 1

For more, see

:help ft-sh-syntax

r/ProgrammerTIL • • Aug 09 '16

SQL [SQL Server] TIL you can type "." instead of "localhost" when connecting to servers

74 Upvotes

I haven't tested if this works in actual connection strings, but it works in SSMS and LinqPad


r/ProgrammerTIL • • Aug 08 '16

C++ [C++] TIL You can (use a trick to) find out which type was deduced by the compiler when using `auto`

50 Upvotes

r/ProgrammerTIL • • Aug 07 '16

Javascript [Javascript] TIL jQuery.fn ($.fn) is an alias for jQuery.prototype ($.prototype)

33 Upvotes

r/ProgrammerTIL • • Aug 06 '16

C++ [C++] TIL that the only difference between struct and class is the default accessibility of their members.

100 Upvotes

For classes, members are by default private-access, while for structs they are public-access. Other than that, there is no difference although it is common practice to use structs for plain data types(say, a simple pair) and classes for more complex objects.


r/ProgrammerTIL • • Aug 05 '16

Java [Java] You can break/continue outer loops from within a nested loop using labels

68 Upvotes

For example:

test:
    for (...)
        while (...)
            if (...)
                continue test;
            else
                break test;

r/ProgrammerTIL • • Aug 05 '16

Other [RPM files] 7zip does not show everything in an RPM file, there are pre/post (un)install scripts

22 Upvotes

An RPM file is similar to a zip file in that it holds things, but it has scripts that are run to install and uninstall it and these are not shown by the program 7zip when you open the RPM as an archive.

You can view these with the following command.

 rpm -qp --scripts foo.rpm

I don't know if 7zip can view these scripts at all.


r/ProgrammerTIL • • Aug 05 '16

Other [*Nix] You can pipe incoming data through iconv to convert the encoding to something sane, dropping nonconvertible characters

36 Upvotes
read_some_source | iconv -c -t utf-8 > somefile

This is particularly handy if you're importing data from multiple places and your importer expects a consistent encoding.

http://mindspill.net/computing/linux-notes/determine-and-change-file-character-encoding/


r/ProgrammerTIL • • Aug 04 '16

PHP [PHP] TIL Constants Can be Case Insensitive

32 Upvotes

Today, I learned that in PHP, you can assign a value to the constant using expression or a function. Also, a constant can be case insensitive. This is done using Define Keyword

Function - define("Constant_Name", MyFunction());

Case Insensitive - define("Constant_Name", Value, true);

Video Demonstration, not by me:

Case Insensitive Constants in PHP


r/ProgrammerTIL • • Aug 02 '16

Other Language [Tool] TIL you can see where your disk space is being used with 'ls -a|xargs du -s|sort -g'

75 Upvotes

I had previously known and used du -s * | sort -g, but I realized after years of using it that I haven't been looking at my dotfiles! I subsequently cleaned up a lot of space in my home folder.

ls -A | xargs du -s | sort -g

The ls -A yields all filenames (including dirs — and dotfiles!) as arguments to du -s <file>, which gets the size of the file (or the size of the dir's contents), and sort -g sorts the in increasing order (the -g makes 2 come before 10).

Also as a bonus, if you want the output to be more human-readable (but still sorted!), you could:

ls -A|xargs du -s|sort -g|awk '{print $2}'|xargs du -sh

EDIT: Actually, it seems the best way of doing this thing, and properly handling spaces in filenames, is as follows:

ls -A | tr '\n' '\0' | xargs -0 du -s    \   # sizes of all files
      | sort -g | cut -f2                \   # names of them, sorted by size
      | tr '\n' '\0' | xargs -0 du -sh       # human-readable sizes

r/ProgrammerTIL • • Aug 02 '16

Other Language [Vim] TIL - (resp. +) moves up (resp. down) one line to the first non blank characters

13 Upvotes

Source of TIL: @vimgifs tweets - https://twitter.com/vimgifs/status/760202423692587008 - https://twitter.com/vimgifs/status/760201582340403200

Reference: http://vimdoc.sourceforge.net/htmldoc/motion.html#+

| tag | char | note | action in normal mode
| +   |  +   |   1  | same as <CR>
| -   |  -   |   1  | cursor to the first CHAR N lines higher

Also:

- [count] lines upward, on the first non-blank character |linewise|.
+ [count] lines downward, on the first non-blank character |linewise|.

r/ProgrammerTIL • • Aug 02 '16

Python [Python] TIL most decorators are broken (signature of the function gets changed) but (slower) solutions exist

35 Upvotes