Changes

Jump to: navigation, search

Understanding Ruby Variables

11 bytes added, 13:57, 31 March 2009
no edit summary
== Ruby and Variable Dynamic Typing ==
<google>ADSDAQBOX_FLOW</google>
Many languages such as Java and C use what is known as ''strong'' or ''static'' variable typing. This means that when you declare a variable in your application code you must define the variable type. For example if the variable is required to store an integer value, you must declare the variable as an integer type. With such languages, when a varaible variable has been declared as a particular type, the type cannot be changed.
Ruby, on the other hand, is a dynamically typed language. This has a couple of key advantages. Firstly it means that you do not need to declare a type when creating a variable. Instead, the Ruby interpreter looks at the type of value you are assigning to the variable and dynamically works out the variable type. Another advantage of this is that once a variable has been declared, you can dynamically change the variable type later in your code.
== Identifying a Ruby Variable Type ==
Once a Ruby variable has been declared it can often be helpful to find out the variable type. This can be achieved using the ''kind_of?'' method of the ''Object'' class. For example, to find out if our variable is an Integer we can use the ''kind_of?'' methodas follows:
<pre>

Navigation menu