Question: Can anyone explain how to read this code and what it will do?

```
'D' * (num % 1000 / 500)

```

It is from a method for converting integers to Roman numerals. I don't understand how it functions.

Answer: 
**What it Does**

The expression is a way of building the Roman numeral five-hundreds digit, which is 'D'.

It takes any number, extracts only the three rightmost digits (values 0 through 999), and returns a `'D'` *only* if the value is 500 or greater. Otherwise it returns an empty string `''`

**How to Read it**

In Ruby, the multiply `*`, divide `/`, and modulus `%` symbols have equal precedence and are processed in order from left to right. Parentheses, however, have a higher precedence than these three operators.

To help visualize the processing order, you can add optional parentheses:

```
'D' * ( ( num % 1000 ) / 500 )

```

1. num % 1000:
	* extracts the three rightmost digits of a number, resulting in values 0 - 999
2. {0-999} / 500:
	* determines if value is 500 or greater, or not.
	* Returns 1 if so, 0 if not.
	* In Ruby, integer division does not automatically convert to decimals.
3. 'D' \* {1 or 0}:
	* In Ruby, [multiplying a string](http://ruby-doc.org/core-2.3.0/String.html#method-i-2A) by 1 returns the string, multiplying by 0 returns an empty string

**Examples**

For a number 35,045:

```
35045 % 1000  #=> 45
45 / 500      #=> 0
'D' * 0       #=> ""

```

For a number 468,987:

```
468987 % 1000 #=> 987
987 / 500     #=> 1
'D' * 1       #=> "D"

```

For a number 670:

```
670 % 1000    #=> 670
670 / 500     #=> 1
'D' * 1       #=> "D"

```

For a number 7:

```
7 % 1000      #=> 7
7 / 500       #=> 0
'D' * 0       #=> ""

```
See [this](http://www.tutorialspoint.com/ruby/ruby_operators.htm) page and scroll down to Ruby Operators Precedence.

Multiplication, division, and modular arithmetic are all together, so precedence is left to right IIRC.

First, `num % 1000` is evaluated. Then, that is divided by `500`. That's then multiplied by 'D'.