Ruby adalah bahasa pemograman yang dinamis, open-source dengan fokus di simplicity dan productivity. Ruby memiliki syntax yang elegan yang natural dan mudah untuk dibaca dan ditulis.
dirilis sejak tahun 1995 ruby sekarang banyak digunakan di berbagai aplikasi, baik scripting, web, bahkan mobile development.
Techtalk kali ini akan membahas beberapa basic pemrograman ruby, khususnya hal-hal yang mungkin jarang ditemukan di bahasa pemograman lainnya. Kemudian kita akan membahas aplikasi ruby di pemrograman web dengan memperkenalkan Sinatra, sebuah micro web framework yang elegan dan menginspirasi banyak micro web framework di bahasa pemrograman lainnya.
2. Tentang Ruby
A dynamic, open source programming language with
a focus on simplicity and productivity. It has elegant
syntax that is natural to read and easy to write -
ruby-lang.org
Dibuat oleh @matz (Yukihiro Matsumoto)
Public release 1995
Object Oriented
Sekarang versi 2.2.1
https://github.com/ruby/ruby
3. Why Ruby
"Ruby stays out of your way" (Dave Thomas)
“trying to make Ruby natural, not simple” (Matz)
Imperative with functional flavor (+OO)
Versatile
Great communities ( )RubyGems
4. Ruby is Versatile
Scripting
Server side web (Rails, Sinatra, etc.)
Client side web (Opal, Volt)
Mobile (Ruby Motion)
Robotics (Artoo)
JVM based app (JRuby)
9. Can doesn't mean you should
class Animal; def walk; "Walking..."; end; end;
class Cat < Animal
def speak
"Meong..."
end
end
puts "#{Cat.new.speak} while #{Cat.new.walk}"
10. Arrays
my_arr = [ 10, 'qiscus', 1.618 ]
puts "The second element is #{my_arr[1]}"
# set the third element
my_arr[2] = nil
puts "The array is now #{my_arr}"
11. Hashes
person = {
'name' => 'Delta Purna Widyangga',
'age' => '28',
'job' => 'Programmer'
}
p person['name']
p person['job']
p person['weight']
13. Blocks (2)
def our_programmers
yield "Hiraq", "Backend"
yield "Fikri", "Frontend"
end
our_programmers do |name, role|
puts "#{name} is a #{role} developer"
end
15. Collections
p [ 1, 2, 3 ].map { |n| n * 2 }
p (1..10).select { |n| n % 2 == 0 }
p [ 10, 20, 30 ].reduce { |sum, n| sum + n }
16. Ruby for DSL
Ruby is good for creating internal Domain Specific Language (DSL)
tweet_as('deltawidyangga') do
text 'hello world this is my first tweet'
mention 'putiayusetiani'
link 'http://melangkahkesurga.com'
hashtag 'first'
end
tweet_as('deltawidyangga') do
mention 'putiayusetiani'
17. Tentang Sinatra
Sinatra is a DSL for quickly creating web applications
in Ruby with minimal effort - sinatrarb.com
Dibuat oleh @bmizerany (Blake Mizerany) tahun 2007
Sekarang di maintain oleh @rkh (Konstantin Haase)
18. Why Sinatra
They are both solving a different set of issues, even
though they indeed overlap. While Rails is a
framework focused on writing model driven web
applications, Sinatra is a library for dealing with
HTTP from the server side. If you think in terms of
HTTP requests/responses, Sinatra is the ideal tool. If
you need full integration and as much boilerplate as
possible, Rails is the way to go. - Konstantin
Sinatra is great for the micro-style, Rails is not. As
long as you stay micro, Sinatra will beat Rails. If you
go beyond micro, Rails will beat Sinatra. - David