why ruby?
DESCRIPTION
my story how I started learning and using Ruby by Ihor Kosyanchuk, Technical Leader, SoftServeTRANSCRIPT
{Why Ruby?
my story how I started learning and using Ruby
Plan1. About me2. Why Ruby?3. What is Ruby?4. Ruby on Rails5. Ruby-world Ecosystem6. Future7. Questions
About me
Igor Kasyanchuk
Education: National University of Oil and Gas, PZ-01-1Worked at: Eleks, SoftServe, freelancing, own projectsCompleted projects: 20+Used: Java, Visual C++, C++ BuilderUsing: Ruby, Ruby on Rails, HTML/CSS/JS/etcWorking now: in SoftServe as Ruby, Ruby on Rails Team/Tech Lead
Why Ruby?
I was tired of …
... and many other things
Why Ruby?
And I met Ruby …
say = "I love Ruby"puts say
say['love'] = "*love*"puts say.upcase
5.times { puts say }
What is Ruby?
Ruby is a dynamic, reflective, object-oriented, general-purpose programming language. It was designed and developed in the mid-1990s by Yukihiro "Matz" Matsumoto in Japan.
… from Wikipedia
Q: What other dynamic languages do you know?
What is Ruby?
Ruby is a dynamic, reflective, object-oriented, general-purpose programming language. It was designed and developed in the mid-1990s by Yukihiro "Matz" Matsumoto in Japan.
… from Wikipedia
What other dynamic languages do you know? – PHP, Python, Node.JS
Ruby is• Dynamic programming language• Less is more, efficient code, easy to read, elegant• One of the best language for creating startups (Twitter,
Groupon, Airbnb, Slideshare)• Companies using it: HP, Twitter, Intel, NASA, HULU, Groupon,
Airbnb, Github, Slideshare• Has ~90K libraries, which were downloaded ~3.8M times• Killer app: Ruby on Rails
Also, Ruby ishttp://hyperpolyglot.org/scripting
PHP
$a = [1, 2, 3]min($a)max($a)
Python
min([1, 2, 3])max([1, 2, 3])
Ruby
[1, 2, 3].min[1, 2, 3].max
Java
import java.util.Arrays;import java.util.Collections;
import org.apache.commons.lang.ArrayUtils;
public class MinMaxValue {
public static void main(String[] args) { char[] a = {'3', '5', '1', '4', '2'};
List b = Arrays.asList(ArrayUtils.toObject(a));
System.out.println(Collections.min(b)); System.out.println(Collections.max(b)); }}
Syntax1. Overview2. Loops3. Blocks4. Classes, Mixins5. OOP in Ruby6. “Dynamicity”
Overviewputs "Hello World"
"LOREM".downcase
"lorem".length
"abcd".split("")if /1999/.match(s) puts "party!"end
t = Time.now
Time.now.strftime("%Y-%m-%d %H:%M:%S")
a = [1, 2, 3, 4]
a[-1](1..1_000_000).each do |i| …end
role = :admin
ROLES = [‘admin’, ‘user’]
x = 1000
user = user.new unless user
Loopsi=0while i < 3 print i, " while\n” i += 1end
for ss in 0..2 print ss, " for doubledot\n”end
array=['zero', 'one', 'two', 'three', 'four', 'five']array.each{|ss| print ss, " array.each\n"}
array[0,3].each{|ss| print ss, " array_first_three.each\n”}
Blocksdef simple puts 'Here comes the code block!’ yield puts 'There was the code block!’end
Now:simple { puts 'Hooray! The code block is here!' }
Result:Here comes the code block!Hooray! The code block is here!There was the code block!
Classes, Mixinsclass Greeter def initialize(name) @name = name.capitalize end
def salute puts "Hello #{@name}!" endend
# Create a new objectg = Greeter.new("world")
# Output "Hello World!"g.salute
# Creating mixinmodule Log def log(message) puts message endend
# Creating classclass User include Logend
user = User.newuser.log “hello user”
OOP in Ruby
1. Everything is an object (numbers, strings, classes, everything…)2. Class can inherit only from one parent (single inheritance)3. You can use mixins (in other languages eq. to interfaces + implementation)
5.times do |e| puts “hello world”end
[1,0,6,9,3,5].select{|e| e > 3}.collect{|e| e*2}.min.times do |e| puts eend
“Dynamicity”class Greeter def initialize(name) @name = name end
def method_missing(e) greeting = e.to_s.capitalize "#{greeting} #{@name}" endend
g = Greeter.new("Igor")puts g.hello
=> Hello Igor
1. Strong reflection2. eval3. method_missing4. send5. overload, override anything6. modify, remove, add methods on the fly
Ruby on RailsRuby on Rails, or simply Rails, is an open source web application framework written in Ruby. Rails is a full-stack framework that emphasizes the use of well-known software engineering patterns and paradigms, including convention over configuration (CoC), don't repeat yourself (DRY), the active record pattern, and model–view–controller (MVC).
… Wikipedia
Author:David Heinemeier Hansson
Since 2004
From Denmark
Ruby on Rails1. Built on Ruby2. Web Application Framework3. Blog in 15 minutes4. MVC, CoC, DRY
Built on Ruby
To work with Ruby on Rails you just need to install Ruby our your computer and then install ”rails” as gem.
gem install rails
Web Application Framework
• HTML/HAML/SLIM…• CSS/SCSS/SASS…• JS/CoffeeScript….• MySQL/Postgres/Mongo….• REST/API• Testing• Java, C, Resque, Sphinx, ElasticSearch, etc
Blog in 15 minutes
Check it!
Blog in 15 minutes
Check it!
MVC• Object Relation Mapping• Database Agnostic• CRUD simple• Class to Table, Row to Object
class User < ActiveRecord::Base has_many :photosend
User.first => SELECT `users`.* FROM `users` ORDER BY `users`.`id` ASC LIMIT 1
User.find(2) => SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1
User.find_by(first_name: "igor") =>SELECT `users`.* FROM `users` WHERE `users`.`first_name` = 'igor' LIMIT 1
user = User.find(1)user.photos => SELECT photos.* from photos where user_id = 1
MVCConnecting view and model
сlass UsersController < ApplicationController def index @users= User.all end
end
MVCGenerating HTML (using SLIM)
table - @users.each do |user| tr td= user.first_name td= user.last_name
CoCConvention over Configuration
DRY # def easy_ui # self.easy.presence || 'EASY' # end
# def medium_ui # self.medium.presence || 'MEDIUM' # end
# def hard_ui # self.hard.presence || 'HARD' # end
[:easy, :medium, :hard].each do |field| define_method "#{field}_ui" do [field].presence || field.to_s.upcase end end
Ruby-world EcosystemInterpreters: MRI, jRuby, Rubinius, …Frameworks: Ruby on Rails, Sinatra, E, … Editors: Sublime, RubyMine, VIM, …Tests: Rspec, Selenium, Capybara, minitestServers: Passenger, Unicorn, Puma, Thin, …Libraries (gems): for any tasks … xml, video, image, data processing, API, ….Mobile App: RubyMotion – create native iOS apps in RubyCommunity: Google groups, stackoverflow, rubyclub.com.ua, …
And more …. Github, BitBucket, CoffeeScript, SASS/SCSS, HAML/SLIM,OAuth, Emails, Ajax, Books(30+), integration with Backbone (and other JS frameworks), payments (PrivatBank has gem), …
Future• Ruby 2.2 is coming• Rails 4.2 is coming + Rails 5 branch is created• Many job offers• New Projects, challenges, startups
Questions?
skype: igorkasyanchuk
feel free to contact me
Thank you
and welcome to Ruby world