Ruby as a Lens
This course isn't about helping you add a few new languages to your résumé. It's about programming languages in general. However, your instructor believes that humans learn better when they start with concrete examples and then generalize what they see to form abstract concepts. Accordingly, we will discuss programming language concepts through the lenses of several real programming languages: Ruby, Haskell, and Rust.
Each of these three languages has a unique take on how programs are written. You won't become an expert in any of them by the end of the semester. However, each is a window into the motivations and constraints that shape programming languages. In studying them, you will encounter the many dichotomies that split the programming language universe. Some languages are verbose while others are succinct. Some require developers to be explicit while others infer a programmer's intent. Some ensure safe execution while others place that burden on the programmer. Some focus on the speed of execution while others focus on the speed of development. Some restrict programmers to the expression of mathematical truths while others allow direct editing of memory cells. Some are the passion projects of a small group of developers while others are managed by the giants of industry.
We will start our investigation with Ruby, which by many measures is the least complex of the three and has perhaps the most in common with languages you already know.
Ruby has several identities. It is an imperative language, which means the programmer orchestrates the computation from start to finish by giving commands to the CPU. This is in contrast to a declarative language like HTML in which the programmer only describes a structure and lets the browser figure out how to present it. It is a scripting language, which according to computer scientist and language designer John Osterhout means it is often used to quickly write code that glues together other programs and manipulates the file system. It is also an object-oriented language, which means programmers can organize data and its related code into coherent abstractions.
Yukihiro Matsumoto explains that he invented Ruby to live at the intersection of these identities:
Well, Ruby was born in Feb. 23 1993. At that day, I was talking with my colleague about the possibility of object-oriented scripting language. I knew Perl (Perl4, not Perl5), but I didn't like it really, because it had smell of toy language (it still has). The object-oriented scripting language seemed very promising.
I knew Python then. But I didn't like it, because I didn't think it was a true object-oriented language. OO features are appeared to be add-on to the language. I, as a language mania and OO fan for 15 years, really really wanted a genuine object-oriented, easy-to-use object-oriented scripting language. I looked for, but couldn't find one.
So, I decided to make it. It took several months to make the interpreter run. I put it the features I love to have in my language, such as iterators, exception handling, garbage collection.
Ruby's adoption surged in the 2000s thanks in large part to Ruby on Rails, a framework for building web applications that interact with databases. But Ruby has utility apart from web applications. It is a general-purpose programming language, supports the development of all kinds of software, and is used by all kinds of programmers.
One thing that all programmers do is name things, and that is the first big idea of programming languages that we examine through the lens of Ruby. By the end of this chapter, you'll be able to answer the following questions:
- What practices and language policies influence how programmers name data and other components of a program?
- What impact does the von Neumann model of a computer have on the notion of a variable?
- How is a variable and its value influenced by the variable's name and modifiers?
- What support is available for sharing or isolating names across different units of code?
You will also learn a little about how to write programs in Ruby, but not as much as you would in a textbook devoted to the language. You are encouraged to find additional tutorials, books, or videos to introduce you to the larger language beyond variables.