2. Outline
- Ruby Building Blocks
Bot Network
- Code Samples
Ruby On Rails
PE Packer
- Advantages/Disadvantages Of Ruby
- Ruby Internals ( if there is enough time )
3. Ruby Building Blocks - Data types
- Built-in data types aren’t declared
x=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
4. Ruby Building Blocks - Control Flow
CONDITIONS :
:
if tall < 150
puts "You can’t be a pilot"
elsif tall > 220
puts "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 …
retry
end
5. Ruby Building Blocks-LOOPS/BLOCKS
5.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=1
i = i * 2 until i > 1000
while (i < a.length)
puts a[i].to_s + "X"
i += 1
end
for i in 5..9
puts i
end
for i in 5...9
puts i
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
end
end
[2,4,6,8].reverse_iterate
8642
[2,4,6,8].reverse_iterate { |x| puts x }
8
6
4
2
6. Ruby Building Blocks -
OOP
- All of the constructs of the language are
treated as objects. Checking the data types is
done through *.class
$global_var = “”
class the_class
class Document
attr_accessor: magic_word
attr_accessor :length
attr_reader : getter_only
def name
@name
end
def name=(name)
@name = name
end
end
example = the_class.new
def initialize w, h
@width, @height = w, h
end
def [](index)
words[index]
end
def ==(other)
return magic_word == other.magic_word
end
end
class Example < SuperExample
@@counter = 0
@instance_counter = 0
def initialize
@@counter += 1
@instance_counter = @@counter
super
end
end
11. Folder structure
app/
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.
12. The MVC pattern
The Model View Controller principle divides the
work of an application into three separate but
closely cooperative subsystems.
14. 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.
15. MVC - Model: Implementation
rails generate model book
Associations: (one-to-one, one-to-many)
class Book < ActiveRecord::Base
belongs_to :subject
end
class Subject < ActiveRecord::Base
has_many :books
end
16. MVC - Controller
- Separates business
logic 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
end
end
17. MVC - Controller: Implementation
Implementing 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'
end
end
18. MVC - View
- A Rails View shares data
with controllers through
accessible variables.
<% … %>
Execute a ruby code
<%=…%>
Execute and output the result
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">
21. Portable executable Encryption
Primitive 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
22. [(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
23. Cont’d
- Original MRI is relatively slow
- Errors don’t help so much in fixing problems
Different Implementations: Does it sound good
or bad ?
24. 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