Upload
delphicon
View
263
Download
0
Embed Size (px)
Citation preview
SO FOLKSSO FOLKS
WANT TOWANT TO
YOUYOU
SO, RUBY IS...SO, RUBY IS...
A dynamic programming language
Focused on simplicity andproductivity
Has an elegant syntax
Natural to read and easy to write
THE IDEALS OF IT’S CREATORTHE IDEALS OF IT’S CREATOR
“ Ruby is simple in appearance, but is verycomplex inside, just like our human body
Yukihiro “Matz” Matsumoto
Ruby is a language of careful balance
Blended of Perl, Smalltalk, Eiffel, Ada, and Lisp
Language that balanced functionalprogramming with imperative programming
RUBY’S GROWTHRUBY’S GROWTH
Released in 1995
Achieved mass acceptance in 2006
Ranked among the top 10 on most of theindices that measure the growth andpopularity of programming languagesworldwide
DEMAND OF RUBYDEMAND OF RUBY
graph by TIOB
E
EVERYTHING IS AN OBJECTEVERYTHING IS AN OBJECT
“ I wanted a scripting language that was more powerfulthan Perl, and more object-oriented than Python
Yukihiro “Matz” Matsumoto
In Ruby, everything is an object
Object-oriented programming calls properties by thename instance variables and actions are known as methods
Ruby follows the influence of the language by giving methods and instancevariables to all of its types
Smalltalk
5.times { print "We *love* Ruby -- it's outrageous!\n" }
RUBY’S FLEXIBILITYRUBY’S FLEXIBILITY
Ruby is seen as a flexible language
It allows its users to freely alter its parts
Essential parts can be removed/redefined, at will
Existing parts can be added upon
Ruby tries not to restrict the coder
class Numeric def plus(x) self.+(x) endend
y = 5.plus 6
Use the readable word plus toRuby’s builtin Numeric class
BUILT WITH RUBYBUILT WITH RUBY AND RAILSAND RAILS
AND NOWAND NOW
WHAT IS RAILS ?WHAT IS RAILS ?
A web application framework written in Ruby
Uses model–view–controller (MVC)
Provides structures for database/web service/pages
JSON or XML for data transfer
Rails emphasizes the use of:
convention over configuration (CoC)
active record pattern
don't repeat yourself (DRY)
A WORD ABOUT ARA WORD ABOUT AR (Active Record)
active record pattern is architectural pattern
This is approach to accessing data in a database
Interface of this pattern would include:
Insert Update Delete
+ properties that correspond to columns inunderlying database table
/ /
SO...SO...In AR database table or view is wrapped into a class
Object instance is tied to a single row in the table
Object created = new row is added to the tableObject updated = row is also updated
Object implements accessor methods for eachcolumn in the table
Object removed = row removed too
C# FELLAS MAY CHECKOUT ACTIVEC# FELLAS MAY CHECKOUT ACTIVERECORD IMPLEMENTATION BY RECORD IMPLEMENTATION BY CASTLECASTLEPROJECTPROJECT::
OKAYOKAYLETS GET IT INSTALLEDLETS GET IT INSTALLED
FOR LINUXFOR LINUX
Or via Ruby Version Manager (RVM)
$ sudo apt-get install ruby-full
$ curl -sSL https://get.rvm.io | bash -s stable$ rvm install ruby$ rvm use ruby --default$ rvm rubygems current
Simply
EASY AS HELL
Finally$ gem install rails
FOR WINDOWSFOR WINDOWS
For God's (and your nerves) sake:
Go to:
Click most inconspicuous button
1.
2.Like this one
AND JUST RUN GODDAMN INSTALLATIONAND JUST RUN GODDAMN INSTALLATION
FINALLYFINALLY
Check all things are installed successfully
$ ruby -vruby 2.0.0p (2015-04-13)
IF SO - WE'RE GOOD TO GO
$ gem -v2.4.8
$ rails -vRails 4.2.3
$ sqlite3 --version3.8.11.1 2015-07-15....
LETS SCAFFOLD !LETS SCAFFOLD !Create new Rails project in folder `blog`
$ rails new blog
Install all dependency gems$ bundle install
$ cd blog
Enter project folder
Run Rails server$ rails server $ rails sor
PROFITPROFIT not yet...
http://localhost:3000
WELCOME ABOARDWELCOME ABOARDFolder Description
/app controllers, models, views and assets for application
/config Application's routes, database, etc.
/db Contains database schema and migrations
/public Static files and compiled assets
/test Unit tests and fixtures
/tmp Cache, pid, and session files
/vendor Place for all third-party code
LETS CLARIFY
DOES RAILS USEWHAT STACK
eRuby
Instead of plain JS
To make CSS debugging even more fun
Templating system similar to ASP, JSP and PHP
Quite unexpected, isn't it ?
&To compile into plain JS
COFFEESCRIPTCOFFEESCRIPTIf you ever wondered how it work
square = (x) -> x * xcube = (x) -> square(x) * x
alert square 2
var cube, square;
square = function(x) { return x * x;};
cube = function(x) { return square(x) * x;};
alert(square(2));
CoffeeScript JavaScript
RAILS CLIRAILS CLIThere are a few commands of everyday usage.
In the order of how much you'll use them are:
rails consolerails serverrakerails generaterails dbconsolerails new app_name
All commands can run with -h or --help to list more information.
GENERATEGENERATE
$ bin/rails generate controller Greetings hello create app/controllers/greetings_controller.rb route get "greetings/hello" invoke erb create app/views/greetings create app/views/greetings/hello.html.erb invoke test_unit create test/controllers/greetings_controller_test.rb invoke helper create app/helpers/greetings_helper.rb invoke assets invoke coffee create app/assets/javascripts/greetings.js.coffee invoke scss create app/assets/stylesheets/greetings.css.scss
WE GET THISWE GET THIS
class GreetingsController < ApplicationController def hello @message = "Hello, how are you today?" endend
app/controllers/greetings_controller.rb
<h1>A Greeting for You!</h1><p><%= @message %></p>
app/views/greetings/hello.html.erb
RAILS `SCAFFOLD`RAILS `SCAFFOLD`
A scaffold in Rails is a full set of:
AS A MORE SUITABLE TOOL FOR GENERATING STUFF
modeldatabase migration for the modelcontroller
FOR EXAMPLEFOR EXAMPLE$ bin/rails generate scaffold HighScore game:string score:integer invoke active_record create db/migrate/20130717151933_create_high_scores.rb create app/models/high_score.rb invoke test_unit create test/models/high_score_test.rb create test/fixtures/high_scores.yml invoke resource_route route resources :high_scores invoke scaffold_controller create app/controllers/high_scores_controller.rb invoke erb create app/views/high_scores create app/views/high_scores/index.html.erb create app/views/high_scores/edit.html.erb create app/views/high_scores/show.html.erb create app/views/high_scores/new.html.erb create app/views/high_scores/_form.html.erb ...
... invoke test_unit create test/controllers/high_scores_controller_test.rb invoke helper create app/helpers/high_scores_helper.rb invoke jbuilder create app/views/high_scores/index.json.jbuilder create app/views/high_scores/show.json.jbuilder invoke assets invoke coffee create app/assets/javascripts/high_scores.js.coffee invoke scss create app/assets/stylesheets/high_scores.css.scss invoke scss identical app/assets/stylesheets/scaffolds.css.scss
AND RAKEAND RAKE
Rake is a Make-like program implemented inRuby.
Except tasks and dependencies arespecified in standard Ruby syntax.
WE USE RAILS FOR:WE USE RAILS FOR:
Running db migrationsSeeding database with preset dataGetting list of all routesRunning test
LETS FIND OUT WHAT WE CAN RAKELETS FIND OUT WHAT WE CAN RAKE
Simply typing:$ bin/rake --tasks
We get:rake about # List versions of all Rails frameworks and the environmentrake assets:clean # Remove old compiled assetsrake assets:clobber # Remove compiled assetsrake assets:precompile # Compile all the assets named in config.assets.precompilerake db:create # Create the database from config/database.yml for the current Rails.env...rake log:clear # Truncates all *.log files in log/ to zero bytesrake middleware # Prints out your Rack middleware stack...rake tmp:clear # Clear session, cache, and socket files from tmp/rake tmp:create # Creates tmp directories for sessions, cache, sockets, and pids
AND THATS ALLAND THATS ALL
CHECKOUT `HELLO WORLD` REPOCHECKOUT `HELLO WORLD` REPO
FURTHER LEARNINGFURTHER LEARNING
RubyRails
GREAT SCREENCASTS:GREAT SCREENCASTS:
https://mackenziechild.me/12-in-12/
THANK YOU !THANK YOU !
QUESTIONS ?QUESTIONS ?