Having Fun with Ruby

You should try Ruby language. Ruby is a OO development language created by a Japanese called Yukihiro Matsumoto in 1993.  It's a old language BUT far to be on the mainstream. Ruby is a niche language, I don't dare tell what will happen in the future BUT I see Ruby more and more stronger in world and Brazil as well. Part because Java community is detaching(caused by ORACLE actions).

There is a first strike that get you if you start coding with ruby. I called "It works :-)" If you work with Java you know what I'm talking about. Learn Java eco-system is hard and take long time, things are complex and often stuff that you get on the web just don't work. How many times did you get a tutorial or a blog post code that don't work because version of some jar that change, missing dependencies, environment setup and so on and on.

You get really happy coding and learning Ruby because everything that you get on the web works, it's amazing you just install a couple of gems and them copy the code and paste it, run it and this is it, working. This is not just good in the sens to help beginner to become advanced beginners but in the sense of doing experiments. You have this(drug - in the good sense :-) ) culture in Ruby, it's easy to try it, every gem is in the most of the cases simply and code that you see on the web just works, it's a code apology to you start coding and having fun with it.

Other thing that blow my mind was the softness in Ruby I mean you don't need a heavyweight complex development environment, in several cases you don't need a IDE, a simple text editor is enough, after years working with java I feel like someone remove the chains on my arms. IF there is one thing that really sucks working with Java is working with the full JEE Stack, waiting kill you brain, kill your life, I hate waiting for a deploy, I just waiting take so long time for setup and start see the code results(Java was worst on that, now is get better but still sucks on  that flavor) but in Ruby I don't have this issues, because is everything fast.

So,for Java folks(like me) start looking for new open source solutions. Ruby is getting a really nice eco-system, we can run Ruby on the JVM through JRuby. Ruby is not a performance language, it's not perfect do deal with threads and concurrency like Erlang, Scala and Clojure. Ruby performance is reasonable. Ruby was not designed like Java(To be a language to do everything - huge general purpose). I see ruby perfect suited to a hight level application language. Building web applications in Ruby is such a pleasure, that pleasure for build web applications  long lost in Java(my personal opinion).

The chart above shows basically that Ruby is there, as I said it's not mainstream BUT is growing and being used by the so called enterprise. Using Ruby on the JVM it's really a nice idea because we can do hard core stuff in Java and have all the expressiveness and fun of Ruby for other things.

Ruby Zen Philosophy

Ruby was primary designed to provide developer productivity and Fun. Because of the POLA when you code with ruby or most of ruby gems is very easy to "guess" what you should do, this save you lot of time going offline and reading documentation.

I don't see ruby as a language for lazy developers, poor developers will screw up code in any language the main difference is that you have the freedom do choose, the language don't block you.

Ruby introduce a great parading shift, like slave becoming the master :D It's not the language was build to archive maximum performance and provide joy to the machine but instead the language was designed to provide happiness to the developer, fun to the developer, hell yeah!

You are able to do very powerful useful stuff in Ruby, some are danger BUT, in Ruby philosophy it's about you figure out a decide what you should do. I see folks working with ruby without agile techniques like XP could be really damage as well in Java the difference is that issues that different forms but the evil still there.

More stuff done with less in Ruby, is all very minimalist, small, simple and yet consistent. Language allow you have fun in some many flavors, is like letting your creativity going out. I learn how to enjoy ruby, why ? It's open source, small, cool, simple, powerful and let me get more things done with less time. There a lot of cool and new ideas emerging from the ruby community. Innovation is a important aspect of any community, years ago Java community was driven the innovation and coll stuff was around Java, now there're lots of that on Ruby Community.

Let's see some code

It's time to see code :-) I already write and you read it a lot why I think Ruby is cool, now let's see it in code the coolness in practices. Sharing the ruby Zen Philosophy I will show you small pieces of cool code like small rocks on the Zen Garden, small but intensive enjoyable.

cities = {   
   "RS" => "Porto Alegre",
   "SC" => "Florianopolis",
   "SP" => "São Paulo"

cities.each { |e,c| 
   puts "Estado [#{e}] Cidade [#{c}]"

Ruby is also about testing I can't finish this code without showing you how we do test in ruby, ruby has a awesome test eco-system I will show just the basic one.

require 'rubygems'
require 'test/unit'

class Caclculator
    def sum(a,b)
      return a + b
    def multiply(a,b)
      return a * b

class CaclulatorTest < Test::Unit::TestCase
    def test_right_sum
      c = Caclculator.new
      result = c.sum(5,5)
    def test_right_multiply
      c = Caclculator.new
      result = c.multiply(5,5)

I have the my simple Calculator that just perform sum and multiply to test is just create a class and inherit form   Test::Unit::TestCase and create your method to test using assertions like assert_equal.

In the code above you can see how easy is to create a map and iterate it. The variables e and c are the key and the value of the map, you can use the names that you want don't need to be e and c.

class String
  def to_hell
    return "GTFO :> #{self}"

var = "Stupid String with no changes, Really ?"
puts var.to_hell

That's the classic Monkey Patch, Ruby gives you the ability to change Ruby core classes and add more code and new features. As you can see I'm changing the String class and adding a to_hell method to s String. You can't do that to Java. For lots of people Monkey Patch is danger and terrible, I just found awesome :D

class NameCaller
   def method_missing( name, *args )
     puts "You're calling '#{name}' and you say:" 
     args.each { |say| puts "  #{say}"}
     puts "But no one is there yet." 
NameCaller.new.simon( 'Hello?', 'Hello? Simon?' )

This is my second favorite! I'm adding a method in other weird way. You don't have the method *simon* on the class NameClaller and this code works.  Every time that you call a method that does not exist the method method_missing is called and you have the power to do something, in this case all methods that does not exist that you call will have the same behavior as *simon* BUT you can change this and do different.

Monkey Patch and method_missing are just awesome to write DSLs. Ruby is awesome to DSL indeed! You don't need the parenthesis and the comma all the time this just increase the coolness of the language. :D IF you want see more fun or want get the code showed in this post you can go to my github page and get it.

Diego Pacheco

Popular posts from this blog

Telemetry and Microservices part2

Installing and Running ntop 2 on Amazon Linux OS

Fun with Apache Kafka