Ehsan Ghanbari

Experience, DotNet, Solutions

Inheritance in Ruby

As I'm a beginner in Ruby, so don't expect me to write an advanced post about that. I'm writing for beginners like myself! I was just reading about inheritance in ruby. It is so funny! Look at the following class:

 

class Animal

  def speak

  "Hello from an animal!" 

  end

End

 

inheriting from a class is so easy in ruby as well:

 

Class Cat< Animal

End


 

That's it! Let's print the output:

 

myCat = Cat.new

puts myCat.speak

 

The output would be:  Hello from an animal!

There is a built-in function in Ruby called super which just like the base in C#.

 

class Dog < Animal

   def speak

 super + " And this is a Dog"

 end

End

 

 And if we would like to create an instance from Dog and print the output, it would be:

 

 

dog = Dog.new

Dog.speak

 

 

Output:  Hello from an animal! And this is a Dog

I learned the inheritance in my last 15 minutes! I will share more about this lovely programming language.



Ruby is so cool!

I'm interested in to be familiar with other programming languages although my field of activity is Microsoft.Net. I just read about Ruby, it has such an interesting syntax and standard libraries. it's been in most of the open source projects within 10 years and the core document is available. some big companies like NASA or Intel use Ruby. 

Ruby is a programming language from Japan, it is A dynamic, open source programming language with a focus on simplicity and productivity. "It has an elegant syntax that is natural to read and easy to write." based on wiki: "It supports multiple programming paradigms, including functional, object-oriented, and imperative.  It also has a dynamic type system and automatic memory management."

I'm hoping to learn more and write about Ruby, some sample code in Ruby:

  1. puts 'Hi!'                  
    print 'enter your name:'    
    name = gets.chomp      
    puts "Hi! #{name}"        
    

     

Arrays in Ruby:

 

a = [4,6,7,5]    
a.length         
a.rotate         
a.sort           
a.sort.reverse
a[0]             
a[3]            
a[4] = 3        
a << 1           
a[10] = 0   
a.length      

 

Interesting! 



About Me

Ehsan Ghanbari

Hi! my name is Ehsan. I'm a developer, passionate technologist, and fan of clean code. I'm interested in enterprise and large-scale applications architecture and design patterns and I'm spending a lot of my time on architecture subject. Since 2008, I've been as a developer for companies and organizations and I've been focusing on Microsoft ecosystem all the time. During the&nb Read More

Post Tags
Pending Blog Posts
Strategic design
Factory Pattern
time out pattern in ajax
Selectors in Jquery
using Log4net in asp.net MVC4
How to use PagedList In asp.net MVC
Redis as a cache server
Domain driven design VS model driven architecture
What's the DDD-lite?
Multiple submit buttons in asp.net MVC