techtalk #67 : introduction to ruby and sinatra
TRANSCRIPT
![Page 2: TechTalk #67 : Introduction to Ruby and Sinatra](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55aa84221a28ab431c8b46d3/html5/thumbnails/2.jpg)
Tentang RubyA dynamic, open source programming language witha 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 1995Object OrientedSekarang versi 2.2.1https://github.com/ruby/ruby
![Page 3: TechTalk #67 : Introduction to Ruby and Sinatra](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55aa84221a28ab431c8b46d3/html5/thumbnails/3.jpg)
Why Ruby"Ruby stays out of your way" (Dave Thomas)“trying to make Ruby natural, not simple” (Matz)Imperative with functional flavor (+OO)VersatileGreat communities ( )RubyGems
![Page 4: TechTalk #67 : Introduction to Ruby and Sinatra](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55aa84221a28ab431c8b46d3/html5/thumbnails/4.jpg)
Ruby is VersatileScriptingServer side web (Rails, Sinatra, etc.)Client side web (Opal, Volt)Mobile (Ruby Motion)Robotics (Artoo)JVM based app (JRuby)
![Page 5: TechTalk #67 : Introduction to Ruby and Sinatra](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55aa84221a28ab431c8b46d3/html5/thumbnails/5.jpg)
Ruby.newdef greeting(name);
result = "Hello, " + name;
return result;
end;
puts(greeting("Delta"));
puts(greeting("Puti"));
![Page 6: TechTalk #67 : Introduction to Ruby and Sinatra](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55aa84221a28ab431c8b46d3/html5/thumbnails/6.jpg)
Ruby.new (Refined)def greeting(name)
"Hello, #{name}"
end
puts greeting("Delta")
puts greeting("Puti")
No semicolon | return the last expression | optional parentheses |String interpolation
![Page 7: TechTalk #67 : Introduction to Ruby and Sinatra](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55aa84221a28ab431c8b46d3/html5/thumbnails/7.jpg)
Object.. Object.. Everywhereputs "Tech Talk JDV".length
puts "Qiscus".index("c")
puts "Delta Purna".reverse
puts 42.even?
puts nil.to_i
String, FixNum, Everything on ruby land, even nothing is an object
![Page 8: TechTalk #67 : Introduction to Ruby and Sinatra](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55aa84221a28ab431c8b46d3/html5/thumbnails/8.jpg)
Monkey Patch Everythingclass String
def shout!
"#{self.upcase}!!!"
end
end
puts "Tech Talk JDV".shout!
![Page 9: TechTalk #67 : Introduction to Ruby and Sinatra](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55aa84221a28ab431c8b46d3/html5/thumbnails/9.jpg)
Can doesn't mean you shouldclass Animal; def walk; "Walking..."; end; end;
class Cat < Animal
def speak
"Meong..."
end
end
puts "#{Cat.new.speak} while #{Cat.new.walk}"
![Page 10: TechTalk #67 : Introduction to Ruby and Sinatra](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55aa84221a28ab431c8b46d3/html5/thumbnails/10.jpg)
Arraysmy_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}"
![Page 11: TechTalk #67 : Introduction to Ruby and Sinatra](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55aa84221a28ab431c8b46d3/html5/thumbnails/11.jpg)
Hashesperson = {
'name' => 'Delta Purna Widyangga',
'age' => '28',
'job' => 'Programmer'
}
p person['name']
p person['job']
p person['weight']
![Page 12: TechTalk #67 : Introduction to Ruby and Sinatra](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55aa84221a28ab431c8b46d3/html5/thumbnails/12.jpg)
Blocks (1)def my_block
puts "Begin"
yield
yield
puts "End"
end
my_block { puts "Inside my block" }
![Page 13: TechTalk #67 : Introduction to Ruby and Sinatra](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55aa84221a28ab431c8b46d3/html5/thumbnails/13.jpg)
Blocks (2)def our_programmers
yield "Hiraq", "Backend"
yield "Fikri", "Frontend"
end
our_programmers do |name, role|
puts "#{name} is a #{role} developer"
end
![Page 14: TechTalk #67 : Introduction to Ruby and Sinatra](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55aa84221a28ab431c8b46d3/html5/thumbnails/14.jpg)
Iterators[ 'angga', 'oki', 'omayib' ].each {|name| print name, " " }
3.times { print "*" }
2.upto(8) {|i| print i }
('b'..'f').each {|char| print char }
puts
![Page 15: TechTalk #67 : Introduction to Ruby and Sinatra](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55aa84221a28ab431c8b46d3/html5/thumbnails/15.jpg)
Collectionsp [ 1, 2, 3 ].map { |n| n * 2 }
p (1..10).select { |n| n % 2 == 0 }
p [ 10, 20, 30 ].reduce { |sum, n| sum + n }
![Page 16: TechTalk #67 : Introduction to Ruby and Sinatra](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55aa84221a28ab431c8b46d3/html5/thumbnails/16.jpg)
Ruby for DSLRuby 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'
![Page 17: TechTalk #67 : Introduction to Ruby and Sinatra](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55aa84221a28ab431c8b46d3/html5/thumbnails/17.jpg)
Tentang SinatraSinatra is a DSL for quickly creating web applications
in Ruby with minimal effort - sinatrarb.com
Dibuat oleh @bmizerany (Blake Mizerany) tahun 2007Sekarang di maintain oleh @rkh (Konstantin Haase)
![Page 18: TechTalk #67 : Introduction to Ruby and Sinatra](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55aa84221a28ab431c8b46d3/html5/thumbnails/18.jpg)
Why SinatraThey are both solving a different set of issues, even
though they indeed overlap. While Rails is aframework focused on writing model driven webapplications, Sinatra is a library for dealing withHTTP from the server side. If you think in terms of
HTTP requests/responses, Sinatra is the ideal tool. Ifyou 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. Aslong as you stay micro, Sinatra will beat Rails. If you
go beyond micro, Rails will beat Sinatra. - David
![Page 19: TechTalk #67 : Introduction to Ruby and Sinatra](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55aa84221a28ab431c8b46d3/html5/thumbnails/19.jpg)
Installing SinatraSinatra adalah sebuah gem (library di ruby)
gem install sinatrahttps://rubygems.org/gems/sinatra
![Page 20: TechTalk #67 : Introduction to Ruby and Sinatra](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55aa84221a28ab431c8b46d3/html5/thumbnails/20.jpg)
Hello Sinatrarequire 'sinatra'
get '/' do
'Hello Sinatra!'
end
![Page 21: TechTalk #67 : Introduction to Ruby and Sinatra](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55aa84221a28ab431c8b46d3/html5/thumbnails/21.jpg)
Resources
Komunitas
Untuk Belajar
jogja.rbid-techid-rubyBelajar Ruby on Rails @qiscusRoR Semarang
Try RubyBelajar Ruby Bahasa IndonesiaCodecademy RubyLearn Ruby the Hard WayThe Pickaxe BookSinatra READMESinatra Book