Creating Nested Hash in Ruby

Single line code!

block = lambda {|hash, key| hash[key] =}

nested_hash =

nested_hash[:one] = “1”
nested_hash[:two][:sub1] = “2.1”
nested_hash[:two][:sub2] = “2.2”
nested_hash[:three][:sub1][:subsub1] = “3.1.1”
nested_hash[:three][:sub1][:subsub2] = “3.1.2”
nested_hash[:three][:sub2] = “3.2”

Result Hash:

{:one=>”1″, :two=>{:sub1=>”2.1″, :sub2=>”2.2″}, :three=>{:sub1=>{:subsub1=>”3.1.1″, :subsub2=>”3.1.2″}, :sub2=>”3.2″}}

Ruby Vs Java

I happened to ask a candidate who had worked on Java & Ruby – “Which language do you like more – Java or Ruby?” Prompt came the reply – “Ruby, because it is purely object oriented!”

Our debate that followed inspired me to write the blog (and not to judge the candidate with his opinion!).

Object Oriented Programming definitions:

1) In his book Concepts in programming languages, John C. Mitchell identifies four main features of OOPS: dynamic dispatch, abstraction, subtype polymorphism, and inheritance.

2) Michael Lee Scott in Programming Language Pragmatics considers encapsulation, inheritance and dynamic dispatch as OOPS concepts.

3) W3Resource mentions the 3 main principles of OOPS are: Encapsulation, Inheritance and Polymorphism.

In Ruby, everything is treated as an object. So it is considered a pure OO language. But for argument sake – Ruby does not have direct support for method overloading (polymorphism), interfaces and abstract classes/methods. Ruby is an open source language with focus on simplicity and productivity. Again, we can argue if the productivity goal is really achieved in Ruby – since huge Ruby programs become complex to understand. Duck typing is a cool feature in Ruby, but large code can get complex due to it. Though coding becomes easier with duck typing, I really do not like to spend time understanding such a code.

One issue with duck typing is that it forces programmers to have a much wider understanding of the code they are working with at any given time. For instance, in Python, one could easily create a class called Wine, which expects a class implementing the “press” attribute as an ingredient. However, a class called Trousers might also implement the press() method. With duck typing, in order to prevent strange, hard-to-detect errors, the developer needs to be aware of each potential use of the method “press”, even when it’s conceptually unrelated to what they are working on. By way of contrast, in a strongly and statically typed language that uses type hierarchies and parameter type checking, it’s much harder to supply an unexpected object type to a class. For example, in a language like Java, the ambiguity in the above reuse of the method name press() would not be a problem unless one of the two classes was deliberately defined as a child of the other.

Ruby is a scripting language used popularly for developing web sites. Some popular websites are written using ruby. Ruby is also used as a Glue Language.

First release of Java and Ruby happened during the same time, in 1995. In the recent couple of years Ruby is gaining popularity, but according to the TIOBE Programming Community Index, Java still leads Ruby.

Today Java is seen as clunky and inefficient. But the applications built using Java have been around for more that 2 decades. US Govt, Citibank, United Airlines are some examples of applications that use Java. Such applications will not be replaced with any language so easily due to the risks involved with migration of legacy applications. And definitely it will not be replaced by a language that seems good just because it is purely Object Oriented.

Java is a more mature language. It has its own pros and cons. Ruby and Java both have different applications. Depending on the requirement of your project, you need to choose a language that must be used. I believe no language can be more good than another.

As a software programmer you must be conversant with multiple languages. You must understand the essence of the language – syntax can be learnt and referred to. If you have been told to code in Ruby – do it the Ruby-way. Don’t apply Java logic to Ruby and vice versa.