This gives you a whirlwind tour what the Ruby programming language is like, and what the Chennai-rb group is all about.
Ready?
Fun
Is dynamic
Has REPL
Has a rich standard library
Has lambdas and blocks
Has gems (mostly open source libraries)
Has a vibrant community
# The famous Hello World Program is trivial in Ruby.
#
# None of the superfluous:
#
# * A "main" method
# * Newline
# * Semicolons
#
puts "Hello World!"
cities = ['London', 'Oslo', 'Paris', 'Amsterdam', 'Berlin']
visited = ['Berlin', 'Oslo']
puts "Yet to visit :", cities - visited
Screenshots
# 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
# 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
# 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
# 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
Try the examples in IRB.
Its interesting and fun
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
# 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
}
A picture from the recent meet-up
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.