ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
Ruby Language Overview
Features
¡ñObject-Oriented
¡ñMixins
¡ñVery Dynamic
¡ñMetaprogramming
Alright, simple enough
a = "Hello, Ruby"
puts a
=> "Hello, Ruby"
Functions
def plus(a, b)
a+b
end
plus(1,2)
=> 3
Method calls without parentheses
def plus(a, b)
a+b
end
plus 1, 2 # WAT
=> 3
Arrays, hashes, ranges
# List of string, float, string, int and hash
array = ["Hello", 3.14, "ruby", 1, {hello: "hi"}]
for i in array
puts i
end
>> Hello
>> 3.14
>> ruby
>> 1
>> {:hello=>"hi"}
Arrays, hashes, ranges
for i in 1..10
puts i
end
for i in 'a'..'z'
puts i
end
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x,
y, z
Arrays, hashes, ranges
# split(¡° ¡°)
puts %w(I still love you Kotlin)
"I",
"still",
"love",
"you",
"Kotlin"
String interpolation
a = 1
b = 4
puts "The number #{a} is less than #{b}"
puts "%0.5d" % 5
>> "The number 1 is less than 4"
>> "00005"
Blocks (lambdas)
5.times do |x|
puts "x inside the block: #{x}"
end
('a'..'z').each {|i| puts i}
>> x inside the block: 0
>> x inside the block: 1
>> x inside the block: 2
>> x inside the block: 3
>> x inside the block: 4
a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x,
y, z
Functional programming
array.length.downto(1).map {|n| "I said #{n}"}
Classes and methods
class Sample < Base
def hello
puts "Hello Ruby!"
@field = "Field"
local = "Local variable"
end
end
Bonus
reponse = {
title: "Harry Potter and the Gradle Build",
description: "Book by J.K. Rowling",
attrs: {
pages: 192,
worth_reading: true
},
reviews: [ "Awesome!", "Great" ]
}
puts reponse.to_json
NoMethodError: undefined method `to_json' for #<Hash:0x007fcdb18f84b0>
from (irb):19
from /usr/bin/irb:12:in `<main>'
Bonus (Extension functions)
require "json" #
...
puts reponse.to_json
{
"title": "Harry Potter and the Gradle Build",
"description": "Book by J.K. Rowling",
"attrs": {
"pages": 192,
"worth_reading": true
},
"reviews": [
"Awesome!",
"Great"
]
}

More Related Content

Ruby language overview