Elixir Isn't Ruby

Content posted here with the permission of the author PJ Hagerty, who is Developer, writer, speaker, musician, and Developer Advocate. He is known to travel the world speaking about programming and the way people think and interact.

Programming languages tend to have trends. Once upon a time, it was COBOL that ruled the world. Then languages like Perl and PHP dominated the landscape as the world moved to the web to meet its needs. These days, everyone loves and hates JavaScript in its many forms. 

In the early part of the 2000s a few things happened that created a different kind of space in the world of technology. 

First, a web framework came along that boosted the notability of a happy little programming language that had been puttering along since the mid-90’s called Ruby. Rails changed the web game when it arrived, making it easier for people, developers and companies, to build applications ready for the modern world. 

A little later, in the twenty-teens, a similar idea began to grow. One of the people who made Rails such a success decided to move in a different direction. Combining the style and philosophy of Ruby and the functional programming style of Erlang, Elixir was built. Not long after came a very Rails-like framework, Phoenix. 

While these two languages have a good many similarities, their success in the world of technology users, builders of the tools the general public gets to see. 

These days, about 15 years after Rails first hit the scene, Ruby and Rails is still a prominent programming language. Used often in the startup offices in Silicon Valley and around the world, Ruby on Rails is a great language for getting 80% of the work done in 20% of the time. That last 20% might be your core business logic, which would be what your team was looking to build. 

When it comes to web applications, getting your application to market is key. Additionally, Ruby is capable of being used for backend internal tools, mobile applications, and other things. As the market moves further away from the web and more toward mobile, a programming language needs to be aware of this movement to capture the minds of developers. 

Elixir, coming along much later, may have missed out on some of the benefits Ruby on Rails experienced. As developers began to pick up on the benefits of functional programming, Elixir was poised to be the next big thing. Some companies even began conversions from Ruby or PHP to Elixir with the hopes of it becoming a powerful player in the market. 

Unfortunately for Elixir, even with the help of Phoenix, the concept of pure, single language development, was a thing of the past. Applications need to be built to cover the web, mobile, and any other places where people needed to access tools and toys and whatever developers built. As such developers needed to use a melange of different techniques and languages to continue to deliver what our end users needed and wanted. 

Elixir is still a player in the world of open source software. Projects like Nerves allow for using Elixir in relation to IoT and hardware and give it a life beyond the web. Phoenix continues to be 

used by certain companies as part of their web solutions. All that is true, yet Elixir still failed to capture the excitement Ruby on Rails was able to capture so long ago. 

With the polyglot revolution, developer teams need to have diversified skills. Add to that things like the DevOps movement and you can see why the deck was stacked against Elixir from the start. 

Every language has hurdles when it comes to wide-range acceptance and longevity and only time will tell if Elixir will continue to be a player in the open source market as Ruby has continued to be. In the end, the decision is up to your team on what tool works best.

Making Your Rails Console Interesting..

Content posted here with the permission of the author Akshay Birajdar, who is currently employed at Josh Software. Original post available here

As a Rails developer, every time we work on a Rails project we heavily use rails console for development. I love the rails console as much as any Rails developer. It is the best place to experiment or validate your logic but…!

There are some of my personal nit-picks:

  • When we are playing around with multiple rails projects and end up opening rails console for multiple projects, then it becomes difficult to identify which rails console belong to which project.
  • Another problem with these plain rails console is identifying the rails environment in which we are. Until you run Rails.env you can’t be sure.
  • But the biggest issue with the plain old rails console is; it is BORING!It’s the same old screen every time I open.

Your rails console should be more declartive

Enter the Dotfiles 

Time To spice things up

A little while ago I came to know about dotfiles by Rails conf talk by @bradurani. So basically the dotfiles are the configuration for Unix-y systems. In our daily dev life we use .bashrc, .bash_profile etc.
To my surprise ruby’s IRB also has dotfile based configuration .irbrc and rails console is based on IRB so this config does apply to rails console too.

In this blog post, we will explore .irbrc prompt configuration and tackle the issues mentioned above.

IRB provides many prompt modes out of the box, these are NULLDEFAULTCLASSICSIMPLE, etc.
To switch between these prompt-modes, edit your .irbrc (located in HOME directory, if it’s not present, create it.)

# .irbrc

IRB.conf[:PROMPT_MODE] = :SIMPLE # default is :DEFAULT

# Or, invoke irb with the prompt mode by

# terminal

❱ irb --prompt simple
Simple IRB Prompt

There’s a lot we need to understand before the above configuration starts making sense.

1 . Specific format for the irb prompt is used:

{
  PROMPT_I => ""     # Simple prompt
  PROMPT_S => ""     # Prompt for continued strings
  PROMPT_C => ""     # Prompt for continued statement
  RETURN   => ""     # Format to return value
}

2 . Special strings are provided in irb as prompt-helpers:

%N    # command name which is running
%m    # to_s of main object (self)
%l    # type of string(", ', /, ]), `]' is inner %w[...]
%NNi  # indent level. NN is digits and means as same as printf("%NNd").
%NNn  # line number.

Now, let’s take a look at how IRB defines Classic mode & Simple mode.

# IRB Classic prompt
IRB.conf[:PROMPT_MODE][:CLASSIC] = {
  :PROMPT_I => "%N(%m):%03n:%i> ",   # irb(main):001:0>
  :PROMPT_S => "%N(%m):%03n:%i%l ",  # irb(main):003:0"
  :PROMPT_C => "%N(%m):%03n:%i* ",   # irb(main):005:0*
  :RETURN => "%s\n" # used to printf # 
}

vs 
# IRB Simple prompt
IRB.conf[:PROMPT_MODE][:CLASSIC] = {
  :PROMPT_I => ">> ",  # >>
  :PROMPT_C => "?> ",  # ?>
  :RETURN: => "%s"
}

Hurray!!, we completed the basic guide, now lets start tweaking with our .irbrc

Let’s target our first issue, “How to inform the developer about the rails application name in the rails console?”

The first step, get the application name in rails.

We can find our rails app name in config/application.rb

Note: We will be using meetup rails app for examples

# config/application.rb

module Meetup
  class Application < Rails::Application
  ....
  end
end

Notice that our application name is the module name for rails application class, now let’s get it programmatically.

# We can get application class by

>> Rails.application.class.name
# => "Meetup::Application"

## Since we are only interested in application name, we can get it by

>> Rails.application.class.module_parent.name
# => "Meetup"

# Caveat: In previous versions of ruby, rails it can be acheived by
>> Rails.application.class.parent.name
# => "Meetup"

Now let’s tackle the second issue, Rails environment information in rails console this is straightforward…

# We can get rails environment by simply calling

>> Rails.env
# => "development"

Now let’s glue these together with .irbrc

def application_name
  Rails.application.class.module_parent.name
end

def rails_environment
  Rails.env
end

# Checking if we are in rails console
if defined?(Rails)
  prompt = "#{application_name}[#{rails_environment}]"
  
  # defining custom prompt
  IRB.conf[:PROMPT][:RAILS] = {
    :PROMPT_I => "#{prompt}>> ",
    :PROMPT_N => "#{prompt}> ",
    :PROMPT_S => "#{prompt}* ",
    :PROMPT_C => "#{prompt}? ",
    :RETURN   => " => %s\n"
  }

  # Setting our custom prompt as prompt mode
  IRB.conf[:PROMPT_MODE] = :RAILS
end
Stitched irbrc

Oh! Wait we still haven’t solved the important issue. Its still BOORINGplain rails console.
Since irbrc is just ruby, your imagination is only the limitation for making it interesting. Following are my version of rails console’s

  1. Colorful rails console using Rainbow gem or ANSI escape codes
# This gem needs to installed
require 'rainbow'

def app_prompt
  rails_klass = Rails.application.class

  app_name =
    if rails_klass.respond_to? :module_parent
      rails_klass.module_parent
    else
      rails_klass.parent
    end

  Rainbow("#{app_name.name}").blue
end

# target log path for irb history
def log_path
  rails_root = Rails.root
  "#{rails_root}/log/.irb-save-history"
end

def env_prompt
  case Rails.env
  when "development"
    Rainbow("development").green
  when "production"
    Rainbow("production").red
  else
    Rainbow("#{Rails.env}").yellow
  end
end

if defined?(Rails)
  IRB.conf[:HISTORY_FILE] = FileUtils.touch(log_path).join
  IRB.conf[:PROMPT] ||= {}

  prompt = "#{app_prompt}[#{env_prompt}]:%03n "

  IRB.conf[:PROMPT][:RAILS] = {
    :PROMPT_I => "#{prompt}>> ",
    :PROMPT_N => "#{prompt}> ",
    :PROMPT_S => "#{prompt}* ",
    :PROMPT_C => "#{prompt}? ",
    :RETURN   => "  => %s\n"
  }
  IRB.conf[:PROMPT_MODE] = :RAILS
end

Color rails console

2. Rails console with emojis

# This gem needs to installed
require 'rainbow'

def app_prompt
  rails_klass = Rails.application.class

  app_name =
    if rails_klass.respond_to? :module_parent
      rails_klass.module_parent
    else
      rails_klass.parent
    end

  Rainbow("#{app_name.name}").blue
end

# target log path for irb history
def log_path
  rails_root = Rails.root
  "#{rails_root}/log/.irb-save-history"
end

def env_prompt
  case Rails.env
  when "development"
    Rainbow("development").green
  when "production"
    Rainbow("production").red
  else
    Rainbow("#{Rails.env}").yellow
  end
end

if defined?(Rails)
  IRB.conf[:HISTORY_FILE] = FileUtils.touch(log_path).join
  IRB.conf[:PROMPT] ||= {}

  prompt = "#{app_prompt}[#{env_prompt}]:%03n "

  IRB.conf[:PROMPT][:RAILS_EMOJI] = {
    :PROMPT_I => "#{prompt}\u{1F601}  >",
    :PROMPT_N => "#{prompt}\u{1F609}  >",
    :PROMPT_S => "#{prompt}\u{1F606}  >",
    :PROMPT_C => "#{prompt}\u{1F605}  >",
    :RETURN   => "  => %s\n"
  }

  IRB.conf[:PROMPT_MODE] = :RAILS_EMOJI
end

Emoji

Thank you for reading!!

Let’s get Go-ing!

Content posted here with the permission of the author Tanya Saroha, who is currently employed at Josh Software. Original post available here

A second part can change the prequel. It can either ruin a good first impression by being too extravagant or polish it to a perfect shine. And Golang Girls 2019, the 2nd edition organized in Pune on 10th November is absolutely one more feather in the cap.

A large group of 80 individuals including attendees and coaches turned up. All of them had different experiences with programming but the same energy & enthusiasm to learn and share.

The-Day!

Everyone started assembling at Red Hat office in Pune early morning. The day started with a session by Priyanka, who had the most difficult job of getting attendees familiar with Golang syntax and terms very quickly. She took assistance of Go tour and made sure that everyone was Go-ing together. As soon as she was done with the basics, we threw a question at the audience — Why are you listening to us and learning Go?

And once everyone was in doubt, a very enthusiastic Gopher Gautam Regegave a very enlightening talk on evolution of programming, why Golang was written, why and where we should use it. He filled the audience with questions and everyone was just amazed. Once it became clear that people were excited about Go, the session was taken over by our main Coach Varsha along with Priyanka. They made the attendees dig deeper and deeper into the language and soon everyone was submerged inside Go packages, structs, maps, pointers and interfaces.

Here’s to the first decade that was full of achievements!

10 years ago on the same day, Golang was released as an open-source programming language and we totally wanted to use this opportunity to celebrate. There was a special anniversary cake which was cut by Satish Talim. The celebration was a pleasant surprise for everyone.

After the intermission

Once the food was taken, as we had anticipated — our gophers became a little inactive. It was important for them to regain the zeal before we proceeded further. We engaged them in super fun energizing game that got everyone to compete for amazing goodies. The game was so exciting and such a hit that everyone wanted to play it once more.

Once everyone was full of energy again, it was time to introduce them to the highlight of the day — They were going to build a serverless chat application in Go. One coach was assigned to a group of 5–6. Varsha briefed them about the concepts that they were going to use like GoRoutines and gRPC. And then there was no stopping. There were questions, doubts, struggling and learning. And everyone was able to finish building the chat application before time! All gophers were so amazed at themselves and the Go language.

Climax!

But the best part was yet to come; We had planned a super fun quiz for the attendees. We would announce the questions and gophers would write the answer to us through the chat app they built! Whoever answered the question first got cool goodies. Sounds cool? Well, It was much more engaging and fun than it sounds. It made the day end on a really fun note!

We always look forward to contribute back to the Open source community in all ways possible and Golang Girls is now one of the many ways we do it. See you at the next Golang Girls!

If you missed the event and dont want to wait for the next Golang Girls to start learning Go then you can refer the following:

  1. Tour of GO-https://tour.golang.org/
  2. Build your own chat app with the help of TODOs-https://github.com/gautamrege/gochat
%d bloggers like this: