ruby on rails
DESCRIPTION
Ruby On RailsTRANSCRIPT
Mostafa Menessy & Hesham shabana
Ruby
Outline
- Ruby Building Blocks Bot Network- Code Samples Ruby On Rails PE Packer- Advantages/Disadvantages Of Ruby- Ruby Internals ( if there is enough time )
Ruby Building Blocks - Data types
- Built-in data types aren’t declaredx=5 ; y = “10” ; z = [] , f = { "a" => 100, :b => 5 }x = Array.new ; x = [] ; x << "Word" ; puts x.pop ; puts x.length
- Data types can dynamically change during runtime execution.- Constants are defined by Capitalizing the first letter
Ruby Building Blocks - Control FlowCONDITIONS : :if tall < 150 puts "You can’t be a pilot"elsif tall > 220puts "You’re unique"end
puts "You're a teenager" if age > 12 && age < 20
puts"You're NOT a teenager" unless age > 12 && age < 20
x=20,y=12
puts "You're NOT a teenager" if x <=> y == 1
EXCEPTION:
if y == 0
raise ZeroDivisionError
else
x=x/y
end
begin... code here ...rescue ZeroDivisionError... code to rescue the zero division exception here ...rescue YourOwnException... code to rescue a different type of exception here ...rescue... code that rescues all other types of exception here … retryend
Ruby Building Blocks-LOOPS/BLOCKS5.times do ...code to loop here... end
5.times { ...code to loop here... }1.upto(5) { ...code to loop here... }10.downto(5) { ...code to loop here... }0.step(50, 5) { ...code to loop here... }
"xyz".scan(/./) { |letter| puts letter if letter == ‘x’ else redo }
"xyz".scan(/./) { |letter| puts letter if letter == ‘x’ else retry }
[1, "test", 2, 3, 4].each { |element| puts element.to_s }i = 1i = i * 2 until i > 1000while (i < a.length)puts a[i].to_s + "X"i += 1end
for i in 5..9 for i in 5...9 puts i puts iend end
class Array def reverse_iterate if block_given? current_index = self.size-1 while current_index >= 0 yield self[current_index] current_index -= 1 end else print self.reverse end endend
[2,4,6,8].reverse_iterate 8642[2,4,6,8].reverse_iterate { |x| puts x }8642
Ruby Building Blocks - OOP
- All of the constructs of the language are treated as objects. Checking the data types is done through *.class class the_class
attr_accessor :length
attr_reader : getter_only
def name@name
enddef name=(name)
@name = nameend
end
example = the_class.new
class Documentattr_accessor: magic_word
def initialize w, h@width, @height = w, hend
def [](index)words[index]end def ==(other) return magic_word == other.magic_wordendend
$global_var = “”class Example < SuperExample@@counter = 0@instance_counter = 0def initialize
@@counter += 1@instance_counter = @@counter superend
end
Packets through a Bot
Primitive C&C Network
Cont’d -- Payload
Who hates Vodafone ?
What is Rails
- Open source web application framework written in Ruby language
- Built using the MVC pattern
Rails Application
- Convention over configuration- DRY
- save time- reuse code- maintain
rails [app_name] -d mysql
Folder structureapp/ Contains the controllers, models, views, helpers, mailers and assets for your application.
app/controller The controllers subdirectory.
app/view The models subdirectory.
app/model The views subdirectory.
config/ Configure your application's runtime rules, routes, database, and more.
db/ Contains your current database schema, as well as the database migrations.
public/ The only folder seen to the world as-is. Contains the static files and compiled assets.
assest/ Contains the images, style sheets, javascript.
test/ Unit tests, fixtures, and other test apparatus.
mailers/ Contains the email, templates.
The MVC pattern
The Model View Controller principle divides the work of an application into three separate but closely cooperative subsystems.
The MVC pattern
MVC - Model
Rails Active Record - Rails Active Record is the Object/Relational Mapping (ORM) layer: tables map to classes,rows map to objects and columns map to object attributes
- Each Active Record object has CRUD (Create, Read, Update, and Delete) methods for database access.
MVC - Model: Implementation
rails generate model book
Associations: (one-to-one, one-to-many)class Book < ActiveRecord::Base belongs_to :subjectendclass Subject < ActiveRecord::Base has_many :booksend
MVC - Controller
- Separates businesslogic from the presentation.
- rails generate controller book
class BookController < ApplicationController def list end def show end def new end def create end def edit end def update end def delete endend
MVC - Controller: ImplementationImplementing the list Method:def list @books = Book.find(:all)end
Implementing the show Method:def show @book = Book.find(params[:id])end
Implementing the create Method:def create @book = Book.new(params[:book]) if @book.save redirect_to :action => 'list' else render :action => 'new' endend
MVC - View
- A Rails View shares data with controllers through accessible variables.
Create Action:<h1>Add new book</h1><%= start_form_tag :action => 'create' %><p><label for="book_title">Title</label>:<%= text_field 'book', 'title' %></p><p><label for="book_price">Price</label>:<%= text_field 'book', 'price' %></p><%= submit_tag "Create" %><%= end_form_tag %><%= link_to 'Back', {:action => 'list'} %>
Submit Action:<form action="/book/create" method="post">
Back Action:<form action="/book/list" method="post">
<% … %> Execute a ruby code
<%=…%> Execute and output the result
RoutesRoute:
match ':controller(/:action(/:id))(.:format)'http://mysite/user/get/5http://mysite/article/ruby-on-rails.html
Custome:get 'signup', to: 'users#new', as: 'signup'http://mysite/signup
All together
Portable executable EncryptionPrimitive PE Protector
- Encrypt the .text section data
- Append a new stub section for decrypting the data during runtime
- Adjust the PE Header ( AddressOfEntryPoint )
Ruby PE PACKER
[(Disa)|A]dvantages Of Ruby
- Senior Ruby on Rails engineer: One of the top 5 jobs in Silicon Valley
- Perfectly suits the startup environment
- Good Contribution by the community: over 4k rubygems, and a wide range of built -in modules
- For learning how to implement a new language
Cont’d
- Original MRI is relatively slow
- Errors don’t help so much in fixing problems
Different Implementations: Does it sound good or bad ?
Ruby Internals ( Extra )
Is it really an interpreter ? 1.8 <= Ruby version
- Its own custom tokenization code
- LALR Bison parser generator Involved , Parse.y and Parse.c in the ruby source folders
YARV is Another Ruby VM
Cont’d
Extract from Ruby Under A microscope
Questions
?