Ruby - Day 1 Learning
Ruby is an object oriented programming language which has grown in popularity mainly because of its association with the rails framework for developing web applications. In this entry, I intend to summarize some of the basic syntactical rules of Ruby.
1. Firstly everything is an object in Ruby
2. Local variables, method names start with small letters and the ones which have more than one word have underscore separating both the variables
3. Each method name starts with def and ends with end. If the method does not have any reference to the attributes of the class, then typically the method syntax has a self word to describe the nature of the function
puts is used to invoke a ruby function where the purpose is to print something on to the screen
4. Classes : They start with a keyword class followed by classname, module name and its relation to the base class
5. Getters and Setters in a class can be written by one single line instead of the traditional method definition . Use attr_accessor / attr_reader / attr_writer to understand them
6. Private and Protected can also defined in the same way
7. Modules are defined with the keyword module and are generally used as helper classes for various parts of application
8. _Arrays and Hashes :_One can define an array as a collection of different objects. Hashes are the same as Java hashmaps, except that the way to pass a hashmap to a function is slightly different
9. Control Structures :These are used to put in place a do end logic or while end logic
10 . Blocks and iterators are used immediately after a function call as means to invoke the function multiple times
11.Exceptions: use raise function, or begin-rescue-end structure
12.Marshaling objects is possible as it is needed to store session variables
13.irb is a way to play with ruby in an interactive way
Containers , Iterators and Blocks:
If there was a need to contain a set of objects, one can use array or a hashmap. Advantages of the array comes from the fact that it can be sorted, accessed based on the index position, easy to handle,functions to pop and push elements.Hash maps are used in situations where you can use any object as index unlike arrays where one has to use an integer for indexing.
First, a block may appear only in the source adjacent to a method call; the block is written starting on the same line as the method’s last parameter. Second, the code in the block is not executed at the time it is encountered. Instead, Ruby remembers the context in which the block appears (the local variables, the current object, and so on), and then enters the method.One can pass parameters to the block from a method call and also get some values from the block. find, each and collect.each are some of the common iterators used in ruby.
Finally the proc objects.The interesting thing is that it’s more than just a chunk of code. Associated with a block (and hence a Proc object) is all the context in which the block was defined: the value of self, and the methods, variables, and constants in scope. Part of the magic of Ruby is that the block can still use all this original scope information even if the environment in which it was defined would otherwise have disappeared. In other languages, this facility is called a closure.
Enough of theory …
I checked out few IDE’s for ruby..Firstly RadRails IDE is primarily meant for RoR framework and does not seem to help a beginner in Ruby. Jedit has a plugin for ruby, but it somehow did not appeal to me. The one I settled for was FreeRIDE , which was up and running in a jiffy..Need to do some coding with FreeIDE…
Anyway..That’s the end of my day 1 on Ruby