Ruby on Rails
PhpTypes (Version #14)

Ruby has a lot of similar types to php, with one major difference. In the ruby world, everything is an object, be it a string, a number, the lack of anything at all, or an ActiveRecord instance. You can create ruby types similarly to php, too.

Null

The type you know as Null is dead! nil takes his place! nil is the value of anything that has no value to call its own, like a variable. It’s an object too, just like everything else.

Boolean

A Boolean in ruby works almost exactly the same as in php, it can be either true or false. Unlike php however, very few things evaluate to false, the main ones being nil and false themselves. 0, 896547.6 and "false" are all just as true as true itself.

Integer

Integers, just like in php, are whole numbers. The main differences in Ruby is that if you divide 5 by 2, you should get 2, not 2.5. The reason being that Ruby gets the object before the operator to do the operation, and 5 usually uses Integer Maths, not Floating Point Maths. Also, when you’re hard coding integers, you can add underscores in the middle of the number to make your code more readable, and the ruby parser will skip over them: 5_000_000 == 5000000 evaluates true.

Float

A float works just like it does in php, except that when you’re writing them, you must have a decimal point in it, like 5.0. This is because ruby doesn’t automatically convert types, and just having 5 would make an integer, which will mess you up later if you aren’t careful (see the previous section for an explanation).

String

Strings work the same as in php, variables in double quotes are evaluated, but not for strings in single quotes. Variables should be included like #{var_name} instead of $var_name or {$var_name}. Also, strings, like everything else, are objects. So strlen("a string") becomes "a string".length, and so on.

Array

Arrays in ruby are like arrays in php, except that they are only numeric, if you need something like an ‘associative array’, look in the next section. You can create an array with syntax like this: ['first string', 'second string', 'third string'].

Arrays are zero based, and elements can be accessed with the var[] operator.

Hash

A hash is the ruby equivalant of an “associative array”, its main difference being that it doesn’t store elements in any particular order, it too uses the var[] operator to access elements. For example:


colours = {:school_bus => :yellow, :cat => :black}

if colours[:cat] == :black
  print "The cat is evil I say! EEEEEVIL!"
end

Which will of course output something about evil cats. What’s that :thingy thing, you ask? Why, that’s a…

Symbol

These things are really neat, I mean, REALLY neat! php doesn’t have them, but it should!

A symbol is a bit like a constant, except that you never define it, and like everything, it’s an object too! The way it works is when ruby first sees some variable prefixed with a colon, it adds it to its symbol table, with its own unique id, then every time it encounters that symbol again, it returns the same object, with the same id, so :cat == :cat, but :dog != :cat. They don’t need to be constrained to variable name sorts of characters either, because :"this symbol has spaces in its name"! They’re used a lot in rails, because they’re more efficient to compare to one another (since they’re compared by their id numbers, and numbers take less processing than strings).

If that’s not enough, ruby still supports…

Constants

Ruby, like rails, uses some naming conventions to speed things up, constants are named in upper camel case, like so: ExampleConstant. you can assign a value to them just like a variable (which is named with @each_word_separated_by_an_underscore@), but don’t let yourself think for one second that ruby won’t kick your butt if you try to assign a value to a constant after it already has one. There will be much carnage as errors explode left, right and center!

Objects

Everything’s an object in ruby! Strings, Arrays, Numbers! If you can do stuff to it or get stuff from it, it’s an object. Objects come from classes, just like in php, but unlike php, everything inside them is a method, even the properties! It might seem strange at first, but thanks to the flexibility of ruby, it’s a Really Good Thing™.

Resources?

They’re gone, it’s all oopy in ruby.

Type Juggling

Ruby doesn’t do this, so you need to be careful. In ruby, operators are a method of the operand to their left. Meaning 5 + 2 is exactly the same in ruby’s eyes as 5.+(2). This can sometimes catch you off guard because most of the time these methods return the same type of object as themselves, which means if you do something like 3 / 2, you won’t get 1.5, you’ll get 1, an integer, just like 3 was. if you ran 3.0 / 2 though, you’d get 1.5. This isn’t a bad thing though, Integer maths is very useful when you need to deal with the real world… can you buy one and a half movie tickets? or have one and a half friends?

With Integers, you can use a float as the right operand, and it will return a float too. Can you feel the love?

Ruby has a lot of similar types to php, with one major difference. In the ruby world, everything is an object, be it a string, a number, the lack of anything at all, or an ActiveRecord instance. You can create ruby types similarly to php, too.

Null

The type you know as Null is dead! nil takes his place! nil is the value of anything that has no value to call its own, like a variable. It’s an object too, just like everything else.

Boolean

A Boolean in ruby works almost exactly the same as in php, it can be either true or false. Unlike php however, very few things evaluate to false, the main ones being nil and false themselves. 0, 896547.6 and "false" are all just as true as true itself.

Integer

Integers, just like in php, are whole numbers. The main differences in Ruby is that if you divide 5 by 2, you should get 2, not 2.5. The reason being that Ruby gets the object before the operator to do the operation, and 5 usually uses Integer Maths, not Floating Point Maths. Also, when you’re hard coding integers, you can add underscores in the middle of the number to make your code more readable, and the ruby parser will skip over them: 5_000_000 == 5000000 evaluates true.

Float

A float works just like it does in php, except that when you’re writing them, you must have a decimal point in it, like 5.0. This is because ruby doesn’t automatically convert types, and just having 5 would make an integer, which will mess you up later if you aren’t careful (see the previous section for an explanation).

String

Strings work the same as in php, variables in double quotes are evaluated, but not for strings in single quotes. Variables should be included like #{var_name} instead of $var_name or {$var_name}. Also, strings, like everything else, are objects. So strlen("a string") becomes "a string".length, and so on.

Array

Arrays in ruby are like arrays in php, except that they are only numeric, if you need something like an ‘associative array’, look in the next section. You can create an array with syntax like this: ['first string', 'second string', 'third string'].

Arrays are zero based, and elements can be accessed with the var[] operator.

Hash

A hash is the ruby equivalant of an “associative array”, its main difference being that it doesn’t store elements in any particular order, it too uses the var[] operator to access elements. For example:


colours = {:school_bus => :yellow, :cat => :black}

if colours[:cat] == :black
  print "The cat is evil I say! EEEEEVIL!"
end

Which will of course output something about evil cats. What’s that :thingy thing, you ask? Why, that’s a…

Symbol

These things are really neat, I mean, REALLY neat! php doesn’t have them, but it should!

A symbol is a bit like a constant, except that you never define it, and like everything, it’s an object too! The way it works is when ruby first sees some variable prefixed with a colon, it adds it to its symbol table, with its own unique id, then every time it encounters that symbol again, it returns the same object, with the same id, so :cat == :cat, but :dog != :cat. They don’t need to be constrained to variable name sorts of characters either, because :"this symbol has spaces in its name"! They’re used a lot in rails, because they’re more efficient to compare to one another (since they’re compared by their id numbers, and numbers take less processing than strings).

If that’s not enough, ruby still supports…

Constants

Ruby, like rails, uses some naming conventions to speed things up, constants are named in upper camel case, like so: ExampleConstant. you can assign a value to them just like a variable (which is named with @each_word_separated_by_an_underscore@), but don’t let yourself think for one second that ruby won’t kick your butt if you try to assign a value to a constant after it already has one. There will be much carnage as errors explode left, right and center!

Objects

Everything’s an object in ruby! Strings, Arrays, Numbers! If you can do stuff to it or get stuff from it, it’s an object. Objects come from classes, just like in php, but unlike php, everything inside them is a method, even the properties! It might seem strange at first, but thanks to the flexibility of ruby, it’s a Really Good Thing™.

Resources?

They’re gone, it’s all oopy in ruby.

Type Juggling

Ruby doesn’t do this, so you need to be careful. In ruby, operators are a method of the operand to their left. Meaning 5 + 2 is exactly the same in ruby’s eyes as 5.+(2). This can sometimes catch you off guard because most of the time these methods return the same type of object as themselves, which means if you do something like 3 / 2, you won’t get 1.5, you’ll get 1, an integer, just like 3 was. if you ran 3.0 / 2 though, you’d get 1.5. This isn’t a bad thing though, Integer maths is very useful when you need to deal with the real world… can you buy one and a half movie tickets? or have one and a half friends?

With Integers, you can use a float as the right operand, and it will return a float too. Can you feel the love?