source

A ‘printf’ format reference page (cheat sheet)

Summary: This page is a printf formatting cheat sheet. I originally created this cheat sheet for my own purposes, and then thought I would share it here.

A cool thing about the printf formatting syntax is that the specifiers you can use are very similar, if not identical, between several different languages, including C, C++, Java, Perl, Ruby, and others, so your knowledge is reusable, which is a good thing.

Trying to understand where the universe comes from

My method for trying to understand this fundamental essence – the presence of “something bigger” than me – was to examine intellectually all the reasons I could think of for the universe to exist and to try to envision what had “existed” before the universe came into being.

On the one hand, if there was nothing before creation, how could the “something” of the universe come from “nothing”? On the other hand, if there was something before the creation of the world, it must have always existed, without beginning. But how could “something” have no starting point, no first moment?

I was frustrated by these questions, and by not being able to envision the timelessness that went with “no beginning.” As a boy, I was continually preoccupied by such attempts to explain the world rationally. I was unable to recognize or accept the limitation of my logical mind, its inability to understand the nature of life beyond concepts of solid objects and linear time.

(I had these same thoughts back in high school, but these words are from the book, “Zen at Work.”)

Scala code to read a text file to an Array (or Seq)

As a quick note, I use code like this read a text file into an Array, List, or Seq using Scala:

def readFile(filename: String): Seq[String] = {
    val bufferedSource = io.Source.fromFile(filename)
    val lines = (for (line <- bufferedSource.getLines()) yield line).toList
    bufferedSource.close
    lines
}

How to use a String like it’s a File in Scala (such as in testing)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 12.7, “How to use a String like it's a file in Scala.”

Problem

Typically for the purposes of testing, you want to pretend that a String is a file.

How to open and read text files in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 12.1, “How to open and read a text file in Scala.”

Back to top

Problem

You want to open a plain-text file in Scala and process the lines in that file.

Back to top

Solution

There are two primary ways to open and read a text file:

Table of Contents

  1. Problem
  2. Solution
Back to top

Scala file reading performance - Line counting algorithms

Out of curiosity about Scala’s file-reading performance, I decided to write a “line count” program in Scala. One obvious approach was to count the newline characters in the file:

Ruby command line arguments

Ruby FAQ: How do I read command line arguments in a Ruby script (Ruby command line args)?

To read command line args in a Ruby script, use the special Ruby array ARGV to get the information you need. Here are a few examples.

1) Getting the number of command line args

To get the number of command line arguments passed in to your Ruby script, check ARGV.length, like this: