Lua Day 1: So near yet so far

ruby lua


So far Lua is strikingly similar to ruby. This of course only appears to be the case for very simple scripts.

Some common things so far:

  1. Condition expressions accepts any value, and false / nil are the only things which are failures. 0 and "" are both true.
  2. The concept of a Nil class which only has nil as a value.
  3. The use of end to denote closing blocks for if, for, function definitions, etc.
  4. The general way in which a program feels.

Lua Fibonacci:

Ruby Fibonacci:

The differences?

  1. Lua statements can't be taken as returning a value, hence in the fibonacci example we need to explicitly use return.
  2. Lua has a boolean type, unlike in ruby, which has a FalseClass and a TrueClass.
  3. Lua only has only double precision floating point numbers to represent any number. Ruby has Fixnum, Bignum, Float.
  4. Lua takes the more functional approach for converting between types, e.g. tonumber, tostring instead of Ruby, which has a to_i, to_s method for numeric and string types.
  5. Lua requires parenthesis when calling functions. I guess Ruby is very unique in this regard.
  6. Accessing any variable, even undefined ones, are legal, and always returns nil. This also applies to tables, i.e. accessing an undefined key returns nil.
  7. Lua has a somewhat weird looking concatenation operator ... Ruby just uses + for concatenating strings.
  8. Tables (which are kinda like Arrays and Hashes in one) use a 1-based index.
blog comments powered by Disqus