By:
Sameer Rahmani (@lxsameer)
From:
C/C++ , Python, PHP, Perl, Javascript, Clojure, Lisp, Lua
www.iranonrails.ir
Iranian Ruby users group
Ruby?
and more ...
First of all it's FREE
Ruby is designed to make programmers happy.
- Matz
Ruby brings large increase in productivity
With Non traditional programming style
Learn
&
Develop
Too many resources to learn Ruby
Huge community. I mean Huuuge
Lots of useful Gems
Documentation
Ruby knowledge is in demand
Beautiful code
RubyOnRails
And other tools like
Meta Programming
string = String.new
2.times do
puts "Hello world"
end
# Hello world
# Hello world
1.upto(100) { |i| puts i }
# 1
# 2
...
# 100
some_list = [String, "some string", Integer]
class Class
def some_method
puts "I'm in 'Class' class"
end
end
class Test
end
Test.some_method
# I'm in 'Class' class
Blocks
people.each do |person|
if person.sick?
doctor.visit person
else
person.jumps_around
end
end
hello = Proc.new do |string|
puts "Hello #{string.upcase}"
end
hello.call 'sameer'
# Hello sameer
Open Classes
class String
def say_hello
puts "Hello #{self.to_s}"
end
end
"sameer".say_hello
# Hello sameer
Use ruby installer on MS Windows
You need `git` and `build-essentials`
$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
$ echo 'eval "$(rbenv init -)"' >> ~/.bashrc
$ git clone https://github.com/sstephenson/ruby-build.git \
~/.rbenv/plugins/ruby-build
Now let's install ruby 2.1.4 and make it default
$ rbenv install 2.1.4
$ rbenv versions
system
2.1.4
$ rbenv global 2.1.4
$ rbenv versions
system
*2.1.4
puts 'Hello World'
String, Integer, Class, ActiveRecord::Base
Methods are verbs - Actions and operations on objects (things)
[7] pry(main)> "IranOnRails".reverse
=> "sliaRnOnarI"
[8] pry(main)> 5.even?
=> false
[9] pry(main)>
[11] pry(main)> "IranOnRails rocks".sub('IranOnRails', 'Karajlug')
=> "Karajlug rocks"
[12] pry(main)> "IranOnRails rocks".sub 'IranOnRails', 'Karajlug'
=> "Karajlug rocks"
[13] pry(main)>
Some of the basic methods are available everywhere
gets # Read a string from terminal (stdin)
puts "Hey guys"
exit
name = "Sameer"
nickname = 'lxsameer'
twitter = "@" + nickname
PI = 3.14
[14] pry(main)> "Ruby".length
=> 4
[15] pry(main)> "Ruby".upcase
=> "RUBY"
[16] pry(main)> "Ruby".upcase.capitalize
=> "Ruby"
[17] pry(main)> "Ruby" + "onRails"
=> "RubyonRails"
[18] pry(main)> rails = "onRails"
=> "onRails"
[19] pry(main)> "Ruby#{rails}"
=> "RubyonRails"
[20] pry(main)> "Ruby on Rails".split(' ')
=> ["Ruby", "on", "Rails"]
[21] pry(main)>
List of objects (things)
[22] pry(main)> langs = ["Ruby", "Python", "Clojure", "Scala", "C"]
=> ["Ruby", "Python", "Clojure", "Scala", "C"]
[23] pry(main)> langs.count
=> 5
[24] pry(main)> langs.sort.join(', ')
=> "C, Clojure, Python, Ruby, Scala"
[25] pry(main)> langs[3]
=> "Scala"
[26] pry(main)> langs[-1]
=> "C"
[27] pry(main)> langs[1..3]
=> ["Python", "Clojure", "Scala"]
[28] pry(main)>
Key/Value associations
[28] pry(main)> frameworks = { "Ruby" => "RubyOnRails",
"Python" => "Django" }
=> {"Ruby"=>"RubyOnRails", "Python"=>"Django"}
[29] pry(main)> frameworks["Ruby"]
=> "RubyOnRails"
[30] pry(main)> frameworks["PHP"]
=> nil
[31] pry(main)> frameworks
=> {"Ruby"=>"RubyOnRails", "Python"=>"Django"}
[32] pry(main)>
def analyze(page)
fail '"page" is nil' if page.nil?
if page.even?
puts 'Page is even'
else
puts 'Page is odd'
end
end
analyze(10)
5.times { puts "Hey" }
1.upto(10).each { |i| puts i }
i = 0
while i <= 10
puts i
i += 1
end
for i in (1..10)
puts i
end
(1..10).each do |i|
puts i
end
["Ruby", "Python", "Lisp"].each do |i|
puts i
end
[57] pry(main)> "Ruby".object_id
=> 70043392889260
[58] pry(main)> "Ruby".object_id
=> 70043393002600
[59] pry(main)> "Ruby".object_id
=> 70043393124120
[60] pry(main)> "Ruby".object_id
=> 70043393245640
[61] pry(main)> "Ruby".object_id
=> 70043393399880
[62] pry(main)> :Ruby.object_id
=> 3904908
[63] pry(main)> :Ruby.object_id
=> 3904908
[64] pry(main)> :Ruby.object_id
=> 3904908
[65] pry(main)> :Ruby.object_id
=> 3904908
[66] pry(main)>
Whaaat ? I want to learn more
IranOnRails community ML
IRC channel on freenode:
Website:
Email:
lxsameer => gnu.org
Twitter:
@lxsameer
I'm always available on IRC:
#5hit #iranonrails