Ruby Programming Series Chapter 2 Variables
Variables are the memory locations, which are assigned to any data to be used by the program. The data is usually strings, numbers or arrays. For example if we need to hold the data (information) like the movie name, release date, rating, actors, then we use variables to hold the value.
When we declare variables in ruby it can start with a..z, _ or $. Variables cannot start with a number, capitals or any special character in ruby, but numbers can be used after any valid declaration. We cannot use spaces between variable names like actor name, but we can either use camel casing like actorName or a snake casing (preferred way in ruby) like actor_name.
Valid variable names & declaration
actor_name = “Di Caprio” nick_name = “Leo”
Invalid variable names
There are five types of variables supported by Ruby. The type of variable determines the scope of the variable in a program. Scope of variable means the accessibility or availability of the variable in the program.
- Global Variables
Global Variables are declared using the $ sign. Global variables once declared can be used across the program. These are made available inside methods (functions), block of code or inside classes.
$top_movies = 250
- Local Variables
These are the variables once declared can used inside a program and is also available inside block of code. These variables are not available directly inside methods or classes. We declare a local variable by using proper naming conventions in ruby.
actor_name = “Di Caprio”
- Block Variables
These variables are the ones whose scope is restricted only inside a block of code in ruby. Blocks are generally used while looping (commonly known as iterators in ruby) over an array or hash. They are declared the same way as a local variable but the scope is limited to only the block of code.Lets assume an array of actor names
actor_names = [“Di Caprio”, “Jackie Chan”, “Will Smith”] actor_names.each do |name| puts name.upcase end
Here name is a block variable and it is available only inside do … end.Example above demonstrates how to iterate (loop) over an array, don’t worry if the syntax looks alien to you now, we will cover iterators in the upcoming chapters.
- Class Variables
Class variables are declared using @@ in ruby, when we declare a class variable the value is available across all objects that belong to the same class. Eg – @@count
The usage of class variables will be understood better in the chapter of Classes & Objects, for now knowing that class variables exist is important.
- Instance Variables
Instance Variables are declared using @ sign in ruby, these are used to hold values inside an object, hence instance variables are also known as attributes / properties of an object. The value of instance variable is specific to a given object alone.
Unlike C based languages, when we declare a variable we have to assign a value to it. This is true except for instance variables, by default instance variables hold value nil when not assigned.
Write a ruby program asking a user to enter his name, his favorite movie, number of times he has watched, who he would like to rate a movie (1 – 10)
puts "Enter your name" user_name = gets.chomp # reads the value from the keyboard as a string puts "Enter your favorite movie" movie_name = gets.chomp puts "How many times have you watched " + movie_name times_watched = gets.to_i # type conversion from string to integer puts "How would you rate the movie (1-10) " + movie_name rating = gets.to_f # type conversion from string to float if rating >= 8 puts "Time to watch " + movie_name + " again." # when we use + with strings it performs concatenation else puts "Time for you to make another movie your favorite" end
Here in the above program, the variables like user_name, movie_name, times_watched & rating are used to hold values in the program.
Give it a shot
In the above program, ask the user if they wish to recommend their friends to watch the movie. If the user wishes then display
“ Time to recommend your friends to watch the movie ”
If not display
“ Can recommend another movie to them ”