Because of being invited to take part in a Rails Girls event, I created two presentations. Both of them are intended to be used as short introductions (to Ruby and Git, respectively).
Take a look if you’d like to use them to train other beginners, possibly at an another Rails Girls event.
Introduction to Ruby It tries to cover:
Variables; simple calculations; conditions; functions; collections; symbols; classes. Introduction to Git It goes through the basic workflow:
A fellow Rubyist, Tim Millwood, is about to write an eBook on Sinatra. In order to begin writing, he needs to get appropriate funding on Kickstarter first. If you want to show your support, visit his eye-appealing website and back the project!
What’s interesting about the eBook? The author states:
“There is some great documentation and online tutorials for Sinatra and even some top tutorials. What’s really lacking is a guide on how Sinatra can relate to real world scenarios.
Introduction Web programming is, in general, a business of dealing with texts. Client sends a text request and receives a text response. Processing web documents consists mostly of processing texts, often in a sophisticated way. While new methods like image search are under active development these days, it’s the text search that is the most well-known and adopted method.
Text algorithms offer different ways of efficient text representation, processing and lookup.
For many beginning Rubyists, especially those having experience in other programming languages such as Java or C, checking whether variable is nil may seem a little bit confusing. And even those speaking Ruby quite fluently don’t usually know the tiny little details that stand behind the nil object.
NilClass and Nil Object In Ruby, there are no primitives. I don’t mean people hunting for mammoths and living in caves, I mean primitive types.
There is a long-awaited moment after launching a web application, when technical means used so far are no longer sufficient. Long story short, number of pageviews increases each day and at the same time response time increases rapidly. One of the first things which should be considered in such a case is introducing data caching.
Among filesystem and local memory, memcached is one of the most widely-used storage systems being adapted to cache data in web applications.
When thinking of parsing math expressions (which can be thought of as kind of language interpretation), one should not give the Interpreter Pattern a miss.
Interpreter is one of the design patterns originally described by the “Gang of Four”. It refers to interpreting some abstract language and it shows one of the ways of building an interpreter.
The keynote is simple: write a class for each kind of symbol that may occur in expression, then instantiate those classes and connect the objects together so they will form a tree.
Rake is a build tool written in Ruby, similar to make, Ant and Phing. There is a major difference between Rake and the others, though. Unlike the rest of the tools, Rake does not provide an external DSL (like XML build file in Ant). Instead, all the task are written in pure Ruby. Therefore you gain full flexibility and can take advantage of some nice Ruby features.
What Are The Build Tools?
In the introduction to building a math parser I already mentioned the Reverse Polish notation, also called “postfix notation”.
Its main advantage is unambiguity: you can simply read expression from left to right and calculate its value at the same time. You have to neither set up operators priorities nor use parenthesis. Refer to the Reverse Polish notation description for further details.
There are many implementations of Reverse Polish notation parser available, but most of them seem to be too complicated.
Generally, it is a good idea to automate every boring, complicated programming task that often needs to be repeated. Firstly, developer’s time and nerves can be spared. Secondly, the risk of doing something wrong (e.g. making a typo or leaving out one of the steps required) is minimized. Prepare the task once and don’t repeat yourself anymore.
There are many tools available and the choice is determined principally by the language and environment we use.
A Hash in Ruby is a data structure that holds values in the key => value fashion. Symbols are often being used as keys to speed up the process of key lookup.
The Hash class provides many useful methods, some of them come from the Enumerable module. We won’t go through all of them now, we’ll rather focus on the often forgotten, but worth its value method: default().
Let’s start with the basics.
Sometimes, when we are working with objects in Ruby, we want to make a copy of them. But what for? Well, in most cases we want to have a working copy and still maintain the original, intact object. Changing a reference back to the primary object is much simpler than repairing object’s state.
To our relief, Ruby makes object copying easy. It provides the clone() method that can be applied to any object.
Writing test code is a worthwhile practice and building a parser is a good example to prove this claim. We have seen previously that the Parser class consists of many methods and each method is a part of a chain in top-down analysis. It won’t be a good idea to start writing the parser from scratch, add all methods we think necessary and then run the code for the first time.
The example implementation of a math parser is going to be written in Ruby because of its simplicity. Remember, however, that parsers are usually written in low-level languages, like C or C++. They are often located in the backbone of much bigger programs and are called frequently, so their performance has a great influence on overall system performance. Note that C code is fast, but its main disadvantage is the lack of portability (C code has to be compiled for every software platform separately).
In general, parser is a program that determines whether its input is valid, referring to the given grammar. So, if we would like to parse math expression, we have to set a formal grammar first. The most convenient way to do this is to write the context-free grammar’s production rules using EBNF (Extended Backus-Naur Form) notation. Take a look at this simple example:
digit = "0"|"1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9" The line we wrote is called a production rule and it reads: a digit can be equal to 0 or 1 or 2… or to 9.
Everyone knows how to calculate value of a simple math expression, like "2 + 3 * 7". But not every programmer knows how to write a program that would accomplish the same task.
The first idea usually looks like this:
result = get_number() while operator = get_operator() operand = get_number() result = result operator operand end return result It means: “read the expression from left to right and perform appropriate calculations when operator occurred”.