Welcome to the Video Series - Ruby for Java Programmers.
The objective of this video is to help experienced Java programmers to learn Ruby.This video covers Strings, Numbers, Classes and Objects at a very high level. In future we will come up with detailed videos for each of these topics.
"Hello World" program in ruby can be written by a one line code - "put s Hello World".
In Java, double quotes are used to represent a string. In Ruby, both single as well as double quotes can be used to represent a string. In the case of single quotes, Ruby will print the String as it is, without any evaluation.
Ruby has an efficient string interpolation operator and it is recommended to use it. We can also concatenate strings using plus operator, but it is not efficient and error prone. Let us see more on this on next slide. The string concatenation using plus symbol will result in Type Error, if the variable is not a valid string object.
In Ruby it is NOT mandatory to use parenthesis around function parameters.
Ruby uses gets method to read a string from console. Ruby also supports single and multiline comments.
The to_f method, which is used to convert a string to a float, will return 0.0 in case it fails to convert. So use it carefully. In Java, such methods will result in Exceptions, which is a more sensible behavior.
In Ruby, while writing if statements, "then" is not mandatory if "end" is in a new line. That means then is mandatory if end is also in the same line. Please not that "end" is always mandatory in the "if" condition.
Local variables have local scope and they are represented in lower case. Local variables can share the same name in different scopes. Global variables are pre-fixed with $ Sign. When a variable is unassigned it will print "nil".
Ruby does not impose an explicit return statement as in Java. If no return statement is specified, last evaluated value will be returned.
Ruby is a pure Object Oriented language. Almost everything in ruby is an object. Even plus and minus sign are objects in Ruby. Equal symbol is not an Object and it is the only exception. To create a new instance of a class, use the new operator. The instance variable of a class has a prefix @. The data hiding is NOT rigorously enforced in Ruby. The "initialize" method will act as constructor, if it is defined in a class. To print a string version of a class, use the function "to_s" function. Like in Java, Ruby also has built in automatic garbage collection feature.
Every Ruby object has an inspect method, which returns a human readable string representation of the object. Ruby also has a p method which is shortcut for inspect method. If a class file contains code, which is not part of the class, then it will be added to be a part of the main object.
Please note that Ruby is case sensitive like Java.
En esta página del sitio puede ver el video en línea Ruby for Java Programmers - String, Numbers, Classes and Objects de Duración hora minuto segunda en buena calidad , que subió el usuario TechThali 25 junio 2014, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 971 veces y le gustó 6 a los espectadores. Disfruta viendo!