Welcome to Chennai-rb

This gives you a whirlwind tour what the Ruby programming language is like, and what the Chennai-rb group is all about.

Ready?

Ruby

  • A programming language
  • Natural to read, simple to write
  • Open source
  • Popular
  • Fun

  • Is dynamic

  • Has REPL

  • Has a rich standard library

  • Has lambdas and blocks

  • Has gems (mostly open source libraries) 

  • Has a vibrant community

Ruby examples


  # The famous Hello World Program is trivial in Ruby.
  #
  # None of the superfluous:
  #
  # * A "main" method
  # * Newline
  # * Semicolons
  #

  puts "Hello World!"

Ruby examples



  cities  = ['London', 'Oslo', 'Paris', 'Amsterdam', 'Berlin']
  visited = ['Berlin', 'Oslo']

  puts "Yet to visit :", cities - visited

Ruby is simple

  • Simplicity is the goal
  • Code should be easy to write and read
  • No ceremony, no unnecessary terms or structures or keywords

Ruby is fun

  • Programming becomes interesting
  • Everyone can program - getting started is easy
  • Solve the problem at hand. Tool/language should get out of the way

Ruby has an REPL

  • REPL (Read-eval-print loop) is like a command line interface to the language.
  • Ruby's REPL is called IRB
  • We can type our program line by line in IRB, and see how it executes
  • Makes it easy to try out new libraries, code snippets. And is useful in debugging too

Ruby REPL

Screenshots

Ruby is dynamic

  • Dynamic - no need to declare data-types
  • Helps keep things simple while approaching new problems, if we are not sure about the data-types
  • Helps us focus on the algorithm rather than datatypes

Ruby is dynamic


  # Assume 'meaning_of_life' is a library function 
  # It can return a number or a string
  # So x can take a number or a string

  x = meaning_of_life()

  puts 'Take lite da'  if x == 'Be cool'
  puts 'Chance-illa'   if x == 42
  • Did you notice the if clause
  • Ruby allows us to write code that reads better

Ruby has a rich standard library

  • Many facilities for string manipulation and array processing are available right out of the box
  • Some other useful libraries are:
    • Date and Time
    • Regexp
    • Net/Http for Http
    • Socket for socket IO
    • Thread for multi-threading
    • Rake for build tooling
    • Json for Json
    • Cmath for maths

Ruby standard library


  # Sort an array

  arr = [12, 5, 6, 8, 1, 33]
  arr.sort # => [1, 5, 6, 8, 12, 33]


  # Reverse it
  arr.reverse  # => [33, 1, 8, 6, 5, 12]


  # Access it from the tail
  arr[-1]   # => 33

  # Add and subtract
  arr + [1, 2, 3] # => [12, 5, 6, 8, 1, 33, 1, 2, 3]
  arr - [1, 2, 3] # => [12, 5, 6, 8, 33]

Examples with Array

Ruby standard library


  # Joining the array

  ['abc', 'xyz'].join('-')  # => 'abc-xyz'

  # Get all permutations
  
  [1, 2, 3].permutation.to_a  #=> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

  # Rotate, shift and shuffle

  [1, 2, 3].rotate  #=> [2, 3, 1]
  [1, 2, 3].shift   #=> 1
  [1, 2, 3].shuffle #=> [2, 1, 3]

  # Slice

  arr = [ "a", "b", "c", "d", "e" ]
  arr[1..3]  # => ["b", "c", "d"]

  # Remove duplicates

  [1, 2, 3, 1, 2, nil].uniq  # => [1, 2, 3, nil]

Examples with Array

Ruby standard library

  # String concatenation

  "abc" + "123"  #=> "abc123"

  # String comparison

  "abc" == "abc"  # => true
  "abc" == "abc1" # => false

  # Convert to array
  
  "abc".chars  #=> ["a", "b", "c"]

  # Split and slice

  "hello world".split(" ") #=> ["hello", "world"]
  "hello world"[1..4]      #=> "ello"

  # Change case, Strip, reverse

  "hello".upcase  #=> "HELLO"
  " hello ".strip #=> "hello"
  "hello".reverse #=> "olleh"

Examples with String

Ruby standard library

Try the examples in IRB.

Its interesting and fun

Ruby lambdas and blocks

  • Ruby has the ability to treat code as data.
  • This means we can write chunks of code that can be treated like any ordinary Ruby object - can be assigned to a variable, passed around as method arguments etc, before getting executed eventually
  • The chunks of code created this way are called lambda.
  • An interesting property of lambdas is that they carry their environment with them (they are closures)
  • This enables the functional programming paradigm to be used in Ruby

Ruby lambdas and blocks

An example:


 #
 my_env = "Hello"
 my_code = lambda {
   puts "A lambda"
   puts "Code between the braces can be treated like data"
   puts "The variable my_code has the reference to it"
   puts "It can be passed around and invoked when needed"
   puts "It also carries its environment within itself: the my_env variable"
   puts my_env
 }

 foo my_code

 ## somewhere in some other file
 
 def foo(given_code)
  puts "Given code starts"
  given_code.call
  puts "Given code ends"
 end

Ruby lambdas and blocks

  • Ruby has a feature similar to lambdas called blocks
  • A block is a chunk of code, like a lambda. It also carries its environment with itself (closure), like lambdas. The only restriction is that it can be invoked just once.
  • Blocks are invoked using the yield keyword.

 # foo method takes the block
 def foo # A block can be passed without any arguments
   puts "Given code starts"
   yield # This invokes the given block
   puts "Given code ends"
 end

 # And somewhere else
 my_env = "HI"
 foo {
   puts "This is the block: the code within curly braces"
   puts "This will get executed within the foo method"
   puts "And will know the variable my_env too"
   puts my_env
 }

Ruby Gems

  • Libraries that are not included in standard ruby installation are called gems
  • We need to install them separately
    • gem install 'my-gem' (needs internet connection)
  • Ruby community has created thousands of gems. For nearly every task we need to write code for, there is a gem.
  • Most gems are open source and are available for inspection/ improvement

Ruby Gems: Rails

  • Ruby on Rails is also a Ruby gem
  • Ruby on Rails is a popular library and framework for web development.
  • It is current one among the best options to develop database backed web applications
  • It embraces the Ruby philosophy of simplicity and elegance, and adds some of its own
  • Rails aims at enhanced developer productivity using ideas like "convention over configuration" and "don't repeat yourself" (DRY)

Ruby Gems: Rails

  • Developer productivity mean we can execute in weeks the same projects that take months with enterprise technologies (J2EE, Dot.Net) 
  • Rails is open source, just like Ruby. Ruby communities support and encourage the open source movement

Ruby Community

  • Ruby supports and promotes the idea of community
  • This means that people involved in the Ruby programming business - programmers, entrepreneurs, students should meet and interact. 
  • Such interaction happens  both offline and online
  • People share knowledge and opinions, discuss issues with current technology and collaborate towards improvement
  • There are several Ruby conferences for the purpose - dozens of them happen over the year. There are three in India right now.
  • There are also city level user groups. They generally meet monthly, and conduct other events like workshops and hackathons. Chennai has one too.

Ruby Community in Chennai

  • There is an active Ruby community in Chennai
  • We meet once every month, and sometimes conduct workshops too
  • The aim of the community is to help each other by sharing knowledge and opportunities. Its the right place to be for improving your programming skills and finding jobs/internships
  • We discuss online using Slack. We have a mailing list, a meetup.com page and a Twitter too - for announcements. All the details are available on the website

Ruby Community in Chennai

A picture from the recent meet-up

Ruby Community in Chennai

  • You are most welcome to join the group. Just visit one of the meetups or join the Slack chat
  • Like the worldwide Ruby community, we believe in being a friendly and safe place. No one is allowed to be disrespectful to others - especially new-comers and students
  • To ensure that it is comfortable for women, we have zero tolerance against sexism. 
  • We also love polyglotism - this means that our discussions are not restricted to Ruby alone. We talk a lot about Javascript and Clojure and Docker and many other technologies

Ruby Resources

 

Chennai-rb links

Created by Abhishek Yadav (h6165) . Freely distributable without change in original content. Copies must hold this notice.  

Brand and project names are properties of their respective owners.

start-here

By Abhishek Yadav

start-here

Introduction to Ruby and Chennai.rb. Created for Software Freedom Day 2015

  • 1,241