P. 1
مقدمة في روبي

مقدمة في روبي

|Views: 331|Likes:
منشور بواسطةapi-3784406
كتاب عن برمجة روبي
كتاب عن برمجة روبي

More info:

Published by: api-3784406 on Oct 16, 2008
حقوق الطبع:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

03/18/2014

pdf

text

original

تا ي وت حمل ا سرهف

ةمدقم ........................................................................................ 7
؟ باتكلا اذه اذامل ....................................................................................... 7
تابلطتملا .............................................................................................. 7
تاثيدحتلاو ةمداقلا تارادصإلا ............................................................................. 7
نمها!ملا .............................................................................................. 7
"#!$لا %&ق' ........................................................................................... 7
؟اذاملو (تم و ) اذام" *و+لا ,-.لا ................................................................ 8
؟ ة/م0بلا (هام ........................................................................... 8
؟ 1ق$تلا ($2مام ......................................................................... 8
(هام Ruby ؟ ............................................................................ 8
اذامل Ruby ؟ ............................................................................ 8
34ور 5بث6 ............................................................................................ 9
"78ها/لا 98حلا 5بث6 :لوا .................................................................. 9
0ي&طتلا تا;4 ........................................................................................... 10
<لا2لا4 =>ه? "3@اثلا ,-.لا ....................................................................... 11
ةبAاحB 34ور .......................................................................................... 12
C,DE? ,كF4 34ور اب'0م ................................................................................ 12
تاقل2تلا .............................................................................................. 13
C,DE? ,كF4 34ور اب'0م ................................................................................ 13
تا0Gتملا ............................................................................................. 14
؟ 0Gتملا H&@ (لI *&-حلا ة.B .......................................................................... 15
نم ,ي&حتلا ة.B int (لJ float ........................................................... 15
ةقي0طلا to_s ........................................................................... 16
54ا&ثلا ............................................................................................... 16
دادI+لا K&.ص ......................................................................................... 16
؟ L0!قلا ,ي&حتلا <تي MB .............................................................................. 17
نم ,ي&حتلا Binary ..................................................................... 17
succ, next .......................................................................................... 17
تامكحتملا "Nلاثلا ,-.لا ..................................................................... 18
If/Elsif/Else ........................................................................... 18
O 2 P

Unless ................................................................................ 19
3Q=>ثلا ,ما2لا ........................................................................... 19
7رابI Case ............................................................................ 20
*اودلا" R4ا0لا ,-.لا Functions ................................................................ 21
ةلادلا (هام Function ةقي0طلا ن4و اS$4 %0.لامو ؟ Method ؟ ................................... 21
ة$بملا *اودلا .......................................................................................... 24
تام&ل2ملا ($4" TماUلا ,-.لا .................................................................. 25
تاE&.-ملا ............................................................................................ 25
Vدملا Ranges ....................................................................................... 30
Hashes ............................................................................................. 31
تاWاي0لا Rم ,م2لا " 7XEاكم ............................................................................. 34
Y (ح$ملا ة$ZاB ة/م0بلا "[دا!لا ,-.لا OOP ) ................................................... 36
تاAاA? .............................................................................................. 36
ةQار&لا ............................................................................................... 42
Public/Private/Protected ........................................................................... 49
%0طلا ,مح6 .......................................................................................... 51
*اك\+لا دد26 .......................................................................................... 52
را0كتلا "R4ا!لا ,-.لا .......................................................................... 55
times ............................................................................................... 55
upto/downto ....................................................................................... 55
while ............................................................................................... 57
for .................................................................................................. 58
until ................................................................................................ 59
ة-$لا ,A=>!لا "نماثلا ,-.لا ................................................................... 61
ة-$لا ,A=>!لا ة!ل]^ .................................................................................. 61
length ................................................................................ 61
upcase/upcase! ........................................................................ 61
downcase/downcase! .................................................................. 61
capitalize/capitalize! ................................................................... 62
reverse/reverse! .................................................................................... 62
indexing ............................................................................................ 62
String[From..To] ................................................................................... 63
O 3 P

index ............................................................................................... 63
count ............................................................................................... 63
empty? ............................................................................................. 63
include?(char)/include?(substring) ................................................................ 64
chomp/chomp! ..................................................................................... 64
chomp/chomp!(separator) .............................................................. 64
concat ............................................................................................. 65
Comparing ......................................................................................... 65
scan(pattern) ....................................................................................... 65
split(separator) ..................................................................................... 66
join(separator) ..................................................................................... 66
gsub(pat, replacement), gsub!(pat, replacement) .................................................. 66
freeze, frozen? ..................................................................................... 66
to_s ................................................................................................ 67
to_i ................................................................................................. 67
to_f ................................................................................................. 67
to_a ............................................................................................... 67
to_hash ............................................................................................. 67
Heredoc string ..................................................................................... 68
"RAاتلا ,-.لا IO _`0Uلا a,bدلاY .............................................................. 69
تادل/ملا ............................................................................................. 69
3لاحلا ,م2لا ............................................................................ 69
دل/ملا 0G6 ............................................................................ 69
تادل/ملا cاF@J ......................................................................... 70
تادل/ملا Kذ' ........................................................................ 70
دل/م 1.-6 ............................................................................ 70
تادل/ملا تا$ZاB ....................................................................... 71
path .................................................................................. 71
tell ................................................................................... 71
read .................................................................................. 71
rewind ................................................................................ 72
each .................................................................................. 72
O 4 P

تا.لملا .............................................................................................. 73
delete(file_path) ....................................................................... 73
rename(from, to) ...................................................................... 73
ctime(file_path) (creation time) ........................................................ 73
mtime(file_path) (last modification time) ............................................... 73
atime (file_path) (last access time) ...................................................... 73
zero? ................................................................................. 74
exist?(file_name) ...................................................................... 74
exists?(file_name) ..................................................................... 74
readable?(file_name) .................................................................. 74
writeable?(file_name) ................................................................. 74
Executable?(file_name) ................................................................ 74
size(file_name), size?(file_name) ....................................................... 74
ftype ................................................................................. 74
directory?(file_name) .................................................................. 74
ة4اتكلا ................................................................................................ 75
syswrite, puts, putc .................................................................... 75
each_byte ............................................................................. 75
تاcا$ثتA+لا ة/لا2م "0\ا2لا ,-.لا ................................................................ 77
7و=>I ................................................................................................ 79
Reflection ............................................................................ 79
"0FI Lداحلا ,-.لا XML & Ruby ............................................................. 83
XML vs HTML ................................................................................... 83
"0FI 3@اثلا ,-.لا Ruby Gems ................................................................ 89
تا8مملا .............................................................................................. 89
5بثتلا ............................................................................................... 89
تاراUلا <ها .......................................................................................... 90
1 d install ............................................................................. 90
2 d list ................................................................................ 90
3 d uninstall ........................................................................... 90
O 5 P

4 d search ............................................................................. 90
5 d help ............................................................................... 91
URLShorter ........................................................................................ 91
FasterCSV ........................................................................................ 92
Y تا@ابلا دIا&e "0FI Nلاثلا ,-.لا SQLite/ActiveRecords) ....................................... 94
8مي Lذلا ام SQLite ؟ ................................................................................ 94
ActiveRecords ................................................................................... 100
ةEاWاf user .......................................................................... 102
تا@ا4 g0If user .................................................................... 102
Nحبلاf .............................................................................. 102
9دUت!م Nيدح6f ..................................................................... 103
,كلا g0If .......................................................................... 103
Kذ'f .............................................................................. 103
,كلا Kذ'f .......................................................................... 104
"0FI R4ا0لا ,-.لا Ruby on Rails ............................................................. 106
*ا نI <لكت@ (لا26 MVC =>لe ......................................................................... 106
hبط6 ةلكه Rails .................................................................................. 107
Rake: ............................................................................................. 108
WebRick: ........................................................................................ 109
HolaRails ......................................................................................... 109
*ا (لI ة2ي0A 70i@ database.yml .................................................................... 115
Rails 2 Minutes PhoneBook ...................................................................... 116
"تابلطتملا ............................................................................ 116
؟jkلا اذام ................................................................................... 123
اS4 1-@ا lتB .............................................................................. 123
CC اSتل2E دقل ................................................................................ 123
0ك\ ....................................................................................... 123
O 6 P
ةمدقم
مدق
م
؟ باتكلا اذه اذامل
Rم ,ما2تلا (E ة4&2ص m]ا&تA m6cا0e د24 n@? دقتI?:ل ),ماكتمو (@ا/م باتB m@? &ه (!Z0لا lب!لا Ruby
تابلطتملا
<i@ Vد'J mلI ,D.يو 06&بمB GNU/LINUX و? Windows XP
9ادUتAJ ة.B ةE02م Text Editor
تا'=>-لا4 7د] ةE02م Permissions *او Users/Groups اSل g026? نل (@+ل
(402لا Tك$ل Rمت/م *=>b نم Tك$للا (E ,ماكتم <Iد (لI *&-حلا Rطت!6dd
تاثيدحتلاو ةمداقلا تارادصلا
(مA0لا Re&ملا (لI امZاد د]&A رادصJ 0bo
Programming-Fr34ks.NET
ةم&A0لا ةبتكملا Rم ,ما2تلا ,مFA 9داقلا رادصإلا QT4 ةبتكم و Wx
نيمهاسملا
jاتA0B d دم'?
!س"لا #$ق%
د&$4 5ح6 ,يد2تلا ةلامت'او Rيp&6و #!$4 q&م!م GNU FDL نمها!ملا ) #!$لا %&ق' ةت4اثلا 9ا!e+لا Rم
تاثيدحتلاو ةمداقلا تارادصإلاو
(لI اSلاAرJ (]0ي تاداقت@:لاو تا'ا0تe:لا guru.python[at]gmail.com
.اS4احص? (لJ اSتكلم د&26 باتكلا اذه 3E ةمدUت!ملا p&م0لا و ةيرا/تلا تام=>2لا و تارا2Fلا ,B
O 7 P
لولا لصفلا : ؟اذاملو ىتم و ، اذام
؟
ا
ذ ا م
ل
و ى
تم
و ،
ا
ذ ا م : لولا ل
ص
فل
ا
ىهام ةجمربلا ؟
7ردقملا nلم6 n@J r!4? ($2م4 .. اهذ.$ل ب&Aاحلل تامل26 3ط26 n@? 3$2ي nلذE ام sما@04 lتك6 5$B اذJ
.ب&Aاحلا ةبtاUم (لI
ىنعمام حيقنتلا ؟
Y 1ق$تلا Debugging ب&Aاحلا ltاU6 ام4ر n@+ل .. n/ما@04 رد-م4 7د&]&ملا cاطbu>ل 1ح-6 ةلمI (ه _
. ة;tاb ةقي0ط4 ذ.$ي j? نكمي و? .. rبDلا4 vدي06 ام <S.ي نلE ة;tاb ةقي0ط4
ىهام Ruby ؟
اSبتB XF$ملا ة@ا4اي ة/م04 ةGل (ه Yukihiro Matsumoto ب ر&SFمو Matz د'? <Aا نم cا] ةم!تلا lبA )
d ةمي0كلا را/'+لا : انتعاتب ىبور شم D d
9اI اSE ?د4 1993 (ت' m!.$ل اS4 w.تحم ,xو 1995 <لا2لل اSقلtا (ت' ..
اذامل Ruby ؟
" y4 8مت6
1 رد-ملا ة'&ت.م d
2 ةماI gا0z? تاذ d
3 (ح$ملا ة$ZاB d
4 70!.م و ) ةكما$يد d
5 ةل&محم d
6 ة.i@ ةGص d
رد-ملا ة'&ت.م ةI0Aو )ةGللا (لI نمZاe نير&طملا نم (Sت$م 0z ددI د]&ي m@? ($2ي رد-ملا ة'&ت.م ةGللا j&B "
. cاطb+لا ة/لا2م (E
ةماI gا0z? تاذ تاS]او و? تا@ابلا دIا&e 3E cا&A sما0بلا نم ة.لتUم Hا&@? (E اSمادUتAJ ة@اكمJ ($2ي "
.#لJ... lي&لا و? ةملI sما04 و? ةم&Aر
(ح$ملا ة$ZاB ةGل تا/م0بلا 0ي&طتل نم+لاو ,DE+لا ب&لA+لا اS@? (ه 78ملا "
O 8 P
لولا لصفلا : ؟اذاملو ىتم و ، اذام
ةكما$يد *ا ,ثم ة@اQ تاGل Tك24 )0Gتملا H&@ نI نل26 j? ا0طDم j&ك6 نل " Java " =>ثم
int i=5; //ة$BاA
i=5 //ةكما$يد
70!.م 9دUت!6 اS@? ($2م4 " 0!.م Tلو <]0تم .<]0تملا و 0!.ملا &ه ام n$هذ (لJ ردابتA )
Lذ.$6 Mلم (لJ =>ثم (!لا ,ثم ةGل نم د&كلا ,ي&حت4 9&قي sما@04 &ه <]0تملا exe
"تابلA و تا8مم mل اذهو 0طA د24 0طA د&كلا ذ.$ت4 9&قي sما@04 &ه 0!.ملا
.دات2لا نم ة$4 نم 0ثB?و ,GF6 9اi@ نم 0ثB? (لI ةل&محملا (ه "تا8مملا نمE
.ة-$م ,كل 70م ,B n/ما@04 ةم]06 7داIJ (لJ `اتحتA 3تلا ةم]0تملا تاGللا Tك24
d70حلا ردا-ملا <لاI 3E ةلكFم 0بت26 :ل vذهوd د&كلا (لI H=>tإلا ة@اكمJ و crبلا " تابل!لاا نم
ةل&محملا .h4ا!لا c8/لا 3E ا@0Bذ امB <i$لا نم ديد2لا (لI ةم&Iدم اS@? ($2م4 "
&'ور (يبث)
ل jا w':ل 34ور ةG ,محت4 nلI وا 78ها] ةم8' 9دUت!6 3SE 3!لا ةGللا4 ة4&تكملا تاGللا نم ديد2لا ,ثم اSلثم
.{pاS] (لI mتم]06 و 3!لا ةGل4 ب&تكملا ةGلل Lد&كلا رد-م
"نتقي0طلا q0F$A ,B (لI
:ةزهاجلا مزحلا تيبثت لوا
.n2م 3تلا Tك$للا ة2يp&6 Rم ةتبثم 36X6 ابلاz 34ور j? w':ل
"تاهد0ل ةS4اFملا تا2يp&تلا 3E
"3لاتلا 0م+لا 9ادUتAا4 ) :ل 9? nماi@ (لI ةتبثم 34ور ,ه ) 9=>2تAا ,مIا
rpm -q ruby
9دUتAا ةتبثم نك6 <ل اذJ yum "3لاتلا 0م+لا 9دUتAا و ) رذ] 9دUت!مB
yum install ruby
"jا4دل ةS4اFملا تا2يp&تلا 3E
O 9 P
لولا لصفلا : ؟اذاملو ىتم و ، اذام
9دUتAا ةتبثم نك6 <ل اذJ apt-get
sudo apt-get install ruby
"pود$ي&لا 9اi@ (لI
"pود$ي&لا 9اi$ل 3مA0لا Re&ملا نم 34ور ةم8' *8@
http://www.ruby-lang.org/en/downloads a
*ا 1تEا cmd lتBاو ruby -v رادصإلا ةE02مل
C:\Documents and Settings\StrikerXruby -!
ruby "#$#% &'(()-(*-"* patc+le!el (, -i*$%-ms.in*'/
lتBا fxri mEو hQ&تلل ةم&Aر ةS]او &هو interactive ruby shell
*ي$طتلا تا+ي'
yل 70ثB ةي0ي&ط6 تا;4 د]&6 Ruby/Rails
"ةي0ي&طتلا ة;4 9ادUتAا ,DE? ا@? NetBeans 6
اSلي Eclipse *ا Rم RDT plugin
9دUت!6 ةtا!4 ,ك4 ردق6 وا Easy Eclipse for Ruby
"اه0S\? نمو )70ثB تار0حم د]&6 vim و? Vد$I ,D.ملا TextMate | ,B (لI ) |mمدUت!4 8لير hي0E Rم]
9دUتAXA ا@ا *ا' vim .0b+لا ,-.لا ادI ام ) باتكلا ,B 3E
*ا راتbJ " ةx&حلم Editor/IDE 3E {را&Fم 3E {دIا!ي }\ 9دUت!6 9p:ل n@+ل .. sم0بملا 7ا' (E <Sم }\
*ا "0ي&طتلا ة;4 و? ر0حملا 3E 0E&ت6 j? l/ي 3تلا cا\+لا <ه? نمو )ة/م0بلا <لاI Syntax highlighting *او line
numbering *او code folding #لا .. ~
"3لاتلا r4ا0لا R]ار 34ور ةGلل ةي0ي&طتلا تا;4 ,B (لI H=>tu>ل
http://www.rubymatters.com/ruby-ide.shtml
O 10 P
يناثلا لصفلا : ملاعلاب لهأ
م
ل
ا
ع
ل
ا ب ل
ه
أ :ى
ن
ا

!
ا ل
ص
فل
ا
Rم *و+لا n/ما@04 Ruby j&كA Hello, World m4 ?دبتي j? 7رود L? 3E 7داI (هو
<Aا4 Mلملا w.'او {ر0حم 1تEا hi.rb jJ w':ل ) rb تات40كA دادتمJ &ه ruby "lتBاو )
puts 01ello2 3orld45
,يد2تلا w.'ا
"3لاتلاB Mلملا ,G\و
ruby +i#rb
C:\Documents and Settings\StrikerXruby +i#rb
1i 6uby4
C:\Documents and Settings\StrikerX+i#rb
1i 6uby4
3$26 اذام puts ؟
puts *ا ةIابt (E 9دUت!6 ةلاد (ه strings dة-@ ,A=>Ad
(لI nماi@ K02تي <ل اذJ "ةi'=>م ruby "*اتلاB {د$I 1ح-لا را!ملا4 vرابbJ &ه mل2E nلI ام ,كE
set pat+=7pat+7;689:;<=>1\bin
N' RUBY_PATH mE 5بQ Lذلا را!ملا &ه Ruby
"3لاتلاB و?
1 (لI نملا4 0ق@ا_ My Computer <Q Properties
2 0تbا _ Advanced Tab
3 <Q_ Environment Variables
4 3E_ Variables Y yل UserName (لI " _ PATH 0تbا <Q Edit
5 MW?_ RUBY_PATH •
_•Y ةt&ق$ملا ةلصا.لا ةEاWJ T$6 :ل
Rم 5e&لا <i2م (E ,ما2ت$A Ruby "nلذ *اثم )mتeو 3E ذ.$A mبتكتA Lذلا 0ط!لا j? ($2م4 )ةلIا.تلا ةقي0ط4
O 11 P
يناثلا لصفلا : ملاعلاب لهأ
C:\Documents and Settings\StrikerXruby -e ?puts @1ello4@?
1ello4
*ا jJ w':ل e (ه switch اها$2م execute 9دUت!$A اذل )=>لe %ا\ ب&لA? m$كلو اهد24 3تلا 7راب2لا ذ.$ت4 (هو
irb script *ا (E 70E&تملا 9دUت!6 ردق6 و? fxri
*ا (E cmd lتBا
irb -v
C:\Documents and Settings\StrikerXirb -!
irb (#A#5&(5/(B/"*,
9ادUتAا4 mتبثE {د$I اد&]&م نكي <ل اذJ yum وا apt-get nت2يp&6 l!'
yum install irb
sudo apt-get install irb
irb&main,:((":( puts ?1ello2 689:4?
1ello2 689:4
= nilC
ةملB w':ل nil اق':ل 0ثBا mلI K02تتA .. ةمe L? R]0ي <ل {0ب26 j? 326 (هو
nil = null = None
ةب,اح- &'ور
ة4ا!' تالمI ذ.$6و )ةبAاحB 34ور 9ادUتAJ Rطت!6
irb&main,:((':( "D'
= *
irb&main,:((*:( $EA
= )'
irb&main,:((B:( "(/'
= 5
irb&main,:((5:( %-A
= -*
./012 /ك3' &'ور اب%*م
0!.ملا را!م &ه *و+لا 0ط!لا j&كي j? g0ت.ي ) *04 و j&ثيا4 ,ثم ة-@ ةGل L? و? 34ور (E mبتك6 امل 540كA L?
.ةGللا4 €اUلا
O 12 P
يناثلا لصفلا : ملاعلاب لهأ
؟ را!ملا K02تA MB
9ادUتA•4 which
7.+ic+ ruby
/usr/bin/ruby
lتBاو ,D.ملا {ر0حم 1تEا
C4usr/bin/ruby
puts 01i2 6uby45
{ Mلملا w.'ا hi2.rb
(لاتلاB mلG\و
7#/+i'#rb
"3لاتلا ةلاA0لا nل 0SiA Permission denied
9ادUتAا ذ.$تلا تا'=>-ل ة]اح4 m@? 1Wا&E chmod "ةمp=>لا تا'=>-لا vاطIإل
7 c+mod )55 +i'#rb
7 #/+i'#rb
1i26uby4
تاقيل4تلا
=>ثم ا/ما@04 5بتB n@? ,U6 2000 اذاملو )mتمدUتAا اذامل 5!@ n@J 5.FتBاو ة$A د24 m2]ا0تل 5;]و 0طA
؟ n/ما@04 رد-م Rلاطي ‚UF4 nلا4امE )اذكه 3لص+لا د&كلا l'اص 5@? 5$B اذJ ؟ د&كلا (E اذB 5لمI
Re&ت6 (تلا تاZ8/لا (لI تاحW&6و تاحمل6 MD6 j•4 )n/ما@04 رد-مل 1W&تلا نE 3ه تاقل2تلا ةtا!4 ,ك4
vذه j? <S.ي m@+ل اهذ.$ي نلو )vذه تاقل2تلا ,ها/تA <]0تملا و? 0!.ملا j? w':لو )n/ما@04 3E ةDماz اS@?
.&ه mل Tل نكلو رد-ملا ?0قA Lذلا ‚UFللو sم0بملل تاقل2تلا
./012 /ك3' &'ور اب%*م
"lتBاو ,D.ملا {ر0حم 1تEا
C4usr/bin/ruby
puts 01i2 6uby45 Cprinting @1i2 6uby@ on t+e screen4
ا-U\ mلDE? ب&لA+لا اذهو )تاقل2تلا ة4اتB (E ^ "ةم=>2لا 9دUت!6 70!.ملا و? ة-$لا تاGللا <i2م j? w':ل
.امZاد ^ ةم=>2لا 9دUتA? 3$كلو ^ yل ةEاWJ ة.لتUم lلاA? 9دق6 تاGل د]&6 m@? Rم
(مدUت!م نم 5$B اذا .. 0طA اذB (لI دتمي hل2تلا ,2/6 j? Rطت!6 C af fa yل اS4اFم vد/ت!E =>ثم
O 13 P
يناثلا لصفلا : ملاعلاب لهأ
(Eو ruby "اذكه j&كيو
ƒ begin
ƒ end
"*اثم
C4usr/bin/ruby
=begin
=ut+or : a+med yousseF
Ganguage: 6uby
<urpose : printing 1ello2 3orld4
=end
puts 01ello2 3orld45
تا*ي5تملا
n/ما@04 *=>b اSتمe 0Gتت4 اS@? L? تا0Gتم اها$2م <A:لا نم
<A•4 0Gتم *اثملا ,بA (لI String | yل 0FA Hello, World „
j? w':ل string <Aا ,مح6 3تلا Hello, World ,bاد اSIدت!@ 3كلو .. 70Bاذلا (E ن2م jاكم (E ة@8Uم
9ادUتAا ا@در? امل نح@ اذل .. اSE ة@8Uملا تا@ابلل ,-@ ردق@ (ت' اS@اكمل را2ت!م <Aا اSل ,م2@ j? l/ي ا$/ما@04
Hello, World <Aا اها$طI? String ةملB 9دUت!@ املBو ) String (Iدت!A ا$/ما@04 *=>b Hello, World
CC 70\ابم
"3لاتلا *اثملا w':ل
irb&main,:((B:( name=?=+med?
= ?=+med?C
irb&main,:(():( puts name
=+med
= nil
ن4 %0.لا w':ل name | و name |
"ة-@ ةل!لA (ه ة@اثلاو 0Gتم (ه (لو+لا
irb&main,:(($:( puts name
=+med
O 14 P
يناثلا لصفلا : ملاعلاب لهأ
= nil
irb&main,:((A:( puts ?name?
name
= nilCC
ةلاتلا ةلثم+لا w':ل
name=5=+med :ousseF5
city=5Cairo5
lang=56uby5
site=5<#H5
age=%%
<A•4 تا0Gتم ا@XF@? name, city, lang, site, age اSل ةبAا$م <e اS$طI?و
"3لاتلاB د'او 0طA (لI <SE026 j? Rطت!6
name2 age=5=+med52 '(
ةGللا4 7p&/حم تاملك4 n6ا0Gتم (م!6:ل "ةx&حلم
؟ *ي5تملا 6$7 8ل9 :$;حلا ةي<ي-
ةtا!4 ,ك4 Ruby نI 7رابI اSE c3\ ,B اق4اA ا$لe املثم Object اS$م نZاB ,B (E ةAاA? cا\? د]&6و نZاB و?
ةقي0طلا class "3لاتلا w':ل .. m!.@ نZاكلا H&@ ا$ل R]06 vذهو
irb&main,:('(:( name=?=+med?
= ?=+med?
irb&main,:('":( name#class
= String
irb&main,:('':( I="(
= "(
irb&main,:('*:( I#class
= HiInumCC
.. اذكهو
نم !"حتلا ةي#ي$ int ىل% float
ةقي0طلا 9ادUتAا4 <ت4 to_f * را-تbJ (هو to float
irb&main,:('B:( I="(
= "(
O 15 P
يناثلا لصفلا : ملاعلاب لهأ
irb&main,:('5:( F=I#to;F
= "(#(CCC
ةق!ر&لا to_s
(لJ نZاكلا *&ح6 ةقي0t 3هو string "3لاتلاB نZاB د]&ي &ل =>ثم )_ة-@ ةل!لAY
x=5555
1حص ددI نI 7رابI اذه نZاكلا int ةقي0طلا 9دUت!$!E ؟ ة-@ ةل!لA (لJ mلي&ح6 دي0@ نح@ نكلو ) VداI to_s
"3لاتلاB
irb&main,:('%:( I=5555
= 5555
irb&main,:('):( s=I#to;s
= ?5555?
irb&main,:('$:( I#class
= HiInum
irb&main,:('A:( s#class
= String
,ما2ملا تر0م اذJ n@ا ,ي&حتلا ةبAا$م4 w':ل 2 yل int تر0م اذJو ةZا$ثلا ةGص (لJ *&حتتA اSلي&ح6 دي06 3تلا
,ما2ملا …† "3لاتلاB ةي0FI 5!لا ةG-لا (لJ *&حتتA
irb&main,:(*(:( "B#to;s&', Cbinary
= ?"""(?
irb&main,:(*":( "B#to;s&"%, C+eI
= ?e?
('ا$ثلا
&ه اSلI *اثم 0S\? .. n/ما@04 *ا&t اSتمe 0Gت6 :ل 54ا&Q نI 7رابI (ه PI = 3.1459
.70بB Kو0' 54ا&ثلا Kو0' ,2/6 j•4 ة4اتكلا (E (Aاقلا ب&لAإلا Rبت6 j? ,D.ي
داد9=ا >$<ص
Ruby ",ثم 9اeر+لا نI 0ب2تلا (E ةمدUت!ملا K&.-لا نم ديد2لا اSE Integer و Fixnum و Float و Bignum و
Rational *ا 0z ا$مSي :ل نح@ نكل Integer *او Fixnum .hQ&تلا 3E (eابلا (لI Rلط6 j? Rطت!6 و
O 16 P
يناثلا لصفلا : ملاعلاب لهأ
؟ ?*سقلا /ي$حتلا @تي Aي-
ا@د$Iا g0E (لI float اذه ,كFلا (لI 10.8
(لJ mل&ح@ j? دي0@و integer M-لل <e0لا ر0م6 &ه nلI Lذلا ,كE ) Integer "3لاتلاB
irb&main,:(*5:( FloatJar="(#$
= "(#$
irb&main,:(*%:( intJar=Knteger&FloatJar,
= "(
*ا jJ w':ل int *ا نم ةحح-لا ةمقلا (قب6و اSلها/تE ةمZا2لا ةلصا.لا Rم ,ما2تي :ل float (ه 10
اذJ string j&كي j? نكمي Integer *اثملا ,بA (لI )
‡ 56 (لاتلاB <e0ل mل&ح@ j? ردق@ „ 56
irb&main,:(*):( Knteger&?5%?,
= 5%
| =>ثم 3لاتلا ,كFلا (لI jاB اذJ نكل abc .Kو0' ,4 ) 9اeر? Tل m6اي&تحم j+ل )R.$ي نلE „
نم !"حتلا Binary
irb&main,:(B':( Knteger &(((("""(,
= 5$B
.. اذكهو
succ, next
"3لاتلا ,كFلا (لI )<e0لل ةلاتلا ةمقلا د26 %0t (ه
puts "#succ Creturns '
puts 5#neIt Creturns %
O 17 P
ثلاثلا لصفلا : ام!"تملا
"#
$%

ا :'
!
ا

!
ا ل
ص
فل
ا
If/Elsif/Else
ةملBو <A+لا ,bد? j? د24 )<2@ ة4ا]إلا ؟ mمادUتAا ة.B 3E ت0كE ,ه .. ا@و0تكلJ ادي04 nلم6 n@? دBˆملا نم
.رو0ملا
"تا&طUلا vذه R4ا6 ) Lد$I *&bدلا ,/!6 5@?و ) 3@و0تكلإلا دي0بلا ةمدb 9دقم 3@? g0تEا
jذJ ) ةي0!لا mتملB ƒ رو0ملا ةملB و ام ‚U\ ƒ 9دUت!ملا <Aا jاB اذJ
mلbد?
nلذ 0z
*&bدلا4 mل 1م!6 :ل
"*اثم 3E 70ك.لا hبط@ ا$Iد
name = ?a+med?
0Gتملا اذJ name ‡ ƒ mتمe ahmed د24 36X6 3تلا ةZ8/لا ذ.$$A „ then .
"اذكه ةما2لا 7ر&-لا j&كتA و
KF condition as >68L t+en
iF;suite
Lnd
"اSقبط@ ا$Iد
iF name==5a+med5 t+en
puts 0+i a+med5
end
iF "M' t+en
puts 0mat+ genius5
end
"اDي? 3لاتلاB اSمدUت!6 j? ردق6و
puts ?<yt+on time? iF name==?StrikerX?
.. mل 1ت.ي نلE mE02ي <ل jJو ) mل 1ت.A mE02ي بابلا %راطلا jاB اذJ m@? ,.t <ل2@ j? ا@در? اذJ )r!4 *اثم ذbX$ل
O 18 P
ثلاثلا لصفلا : ام!"تملا
"&ح$لا اذه (لI *اثم hبط@ ا$2م (لا26
Family=5a+med5
knocker=5a+med5
iF knocker==Family t+en
Copen t+e door
else
Ckeep it closed4
Unless
Lوا!6 7رابI (ه if not _نكي <ل اذJY
puts ?Not "(? unless I== "(
puts ?Not "(? iF not I== "(
"اذكه `0Uلا j&كA
Not "(
Not "(
'()ثلا ماعلا
(هو mمIدت4 تاGللا <i2م short if/else
condition ? then: else
irb&main,:()):( name==?a+med?O ?<yt+on? : ?ruby?
= ?<yt+on?
تامكحتملا (لI *اثم If/Elsif/Else
programmer=5StrikerX5
iF programmer==5StrikerX5 t+en
!al= 0<yt+on time5
elsiF programmer==5St(rP5 t+en
!al= 0C time5
elsiF programmer==5PutatioN5 t+en
!al= 06uby time5
elseiF programmer==5Sp=.N5 t+en
!al= 0<erl time5
else
O 19 P
ثلاثلا لصفلا : ام!"تملا
!al= 03+o t+e +ell are youO5
end
ة*اب+ Case
,ثم تاGل h4ا!لا *اثملا 5i':ل دB? Ruby و Java yل 70-تUم ةقي0t 9دق6 #لJ.. ) falling
programmer=5StrikerX5
!al= case programmer
.+en 0StrikerX5: 0<yt+on time5
.+en 0St(rP5 : 0C time5
.+en 0PutatioN5 : 06uby time5
.+en 0Sp=.N5 :5<erl time5
else 0.+o t+e +ell are you O5
end
"3لاتلا *اثملا w':ل
age="((
state= case age
.+en "##5 : 0C+ild5
.+en %##"': 0Qid5
.+en "*##"): 0>een5
.+en "$ ## B(: 0=dult5
.+en B"##%(: 0.aiting deat+5
.+en %"## "'(: 0dead5
else 0not +umanO5
end
($2م w':ل 1 .. 5 ن4 ‰Vدملا d 70ت.لا اS@X4 1 (لJ 5
O 20 P
#با$لا لصفلا : لاودلا Functions
ل
ا
ود(ا : )
*
ا
+
,
ا ل
ص
فل
ا Functions
ةلا,لا ىهام Function ةق!ر&لا ني-و ا.ني- /ر#لامو ؟ Method ؟
.. 70م نم 0ثBا اSمادUتAJ نكميو تا0ب2تلا نم ةI&م/م 3ه ةلادلا
c8] j&ك6 اS@+ل nلذ اSلI hلطي نكل ةلادلا *ا (ه (ه ةقي0طلا j+ل rقE ةم!6 د0/م &ه ةقي0طلا ن4و اS$4 %0.لا
_" نZاB نم
"lتك@ j? دي0@ ام lب!ل نح@ .. *اثم
>+is program is .ritten by: a+med yousseF
,ل راتbJ (E اذJو =>ثم sما@0بلا ةياد4 (E help ؟ ,م] 70م نم 0ثB? اSمدUت!$A ($2ي .. اذكهو mE اS$مD@
*دبE ),SA ,يد2تلا ا$لI (قبAو ) 70م نم 0ثB? اSمدUت!@ j? ردق@و اذه 0ط!لا ,ثمت4 ةلاد ,م2@ ا$@J &ه ,' ,DEXE
C rقE ةلادلا (E *د2@ .. sما@0بلا (E c8] ,B (E *د2@ j?
C>+e start
puts ?>+is program is .ritten by: a+med yousseF?

C>+e +elp block
puts ?>+is program is .ritten by: a+med yousseF?
C=not+er block
puts ?>+is program is .ritten by: a+med yousseF?
C>+e end
puts ?>+is program is .ritten by: a+med yousseF?
0طDتA ة$2م ةZ8] *د26 jX4 5بzر اذJ nلذ (لI دp و ...,كFلا اذS4 ر0ك6 jX4 اZاS@ 3قط$م Tل m@J )اذه ,U6
؟nلذB Tل? ... jاكم نم 0ثB? 3E *د26 jX4
"=>ثم )اهذ.$6 j? دي06 3تلا 0ماو+لا نم ةI&م/م نI 0ب26 ةلاد ,م26 j? &ه (لاثملا ,حلاE
deF about&,
puts ?>+is program is .ritten by: a+med yousseF?
end
0ماو+لا ةI&م/م (لI *د2تA j•E ) 0ماو+لا ةI&م/م (E *د26 j? 5/ت'ا اذ•E .. ةلاتلا 7ر&-لل *&حتA د&كلا ,UتE
"ةلادلا (E د&]&ملا
O 21 P
#با$لا لصفلا : لاودلا Functions
C>+e start
about&,

C>+e +elp block
about&,
C=not+er block
about&,
C>+e end
about&,
.. اSIدت!@ 70م ,B 3E 0ماو+لا نم ةI&م/م ذ.$6 اSل2/@ ةلادلا jJ ا$ق.6ا نح@
ةملB ا$ل Rبط6 ةلاد ,م2@ j? دي0@ =>ثم Hi 3 تا0م
deF say1i*>imes&,
*#times do
puts ?1i?
end
end
w':ل 3 . times *ا q0F@ امل اSل g02ت$A times ذ.$6 j? اهدي0@ اهد24 36X6 3تلا 0ماو+لا ةI&م/م اSمSEJ نكلو ) 3
"3لاتلل د&كلا *&' الا' )اSتب2-تAا اذJ .. تا0م
deF say1i*>imes&,
puts ?1i?
puts ?1i?
puts ?1i?
end
* ةلادلا ا$IدتAJ 3 =>ثم تا0م
say1i*>imes
say1i*>imes
say1i*>imes
ةلادلا اSي&ح6 3تلا 0ماو+لا ذ.$A 3 "تا0م
Rutput:
1i
1i
1i
1i
1i
1i
1i
O 22 P
#با$لا لصفلا : لاودلا Functions
1i
1i
Xطb *اودلا نI mE026 Lذلا ,B jkلا دحل .. ,م] 100 " Š D
cا0]J (م!ي m2م 5لما26 Lذلا Procedure ن/م0بم ‰ Pascal d3$@&مS.ي دBX6 ,ك4
دZاI اSل Tل نكلو ذ.$6 0ماو+لا نم ةI&م/م &ه cا0]إلا return C
3$26 اذام نكلو return " دZا2لا و? S ؟
deF areaRFSquare
side=B
area=sideEside
return area C>+e !alue returned From t+e Function#
end
t+e=rea=areaRFSquare Carea
puts t+e=rea
ةلادلا4 ةصاUلا 0ماو+لا ةI&م/م jJ w':ل areaOfSquare *ا ةمe (هو ) ةمe R]0ي area
yل ةلادلا د$!@ j? ا$2طتAا ا$@J w':ل theArea *ا d ةلادلا نم 7دZا2لا ةمقلا ا@د$A? ا$@? &ه ,-' Lذلا نكل ..
returned value (لJ ‰ theArea
_" =>2E اS]ات'ا اذJ :لJ cا0]J ةملB <حق6 :لو )ةلادلا ةملك4 <SملB [ا$لا <لك6 امل ),B (لI
*ا ,ثم ةلادلاو cا0]إلا ن4 %0.6 :ل 3تلا تاGللا ‹24 (E C/Ruby نكلو ةلاد :لJ &هام cا0]إلا j? او0بت2E #لJ ..
.دZاI اSل Tل
*&t j? نWراE ا$@? &هو )د] Tل c3\ ,e و? د&كلا4 ةلكFم w':ل side ب R40ملا (E 4 ددح@ j? دي0@ نح$E )
*ا ةمe Side ةمe اSE ةلاد j&لم lتك@ j? *&ق2ملا نم Tل m@+ل side ؟ nلذB Tل?.ة.لتUم
deF areaRFSquare&side,
area=sideEside
return area C>+e !alue returned From t+e Function#
end
t+e=rea=areaRFSquare&5, Carea
puts t+e=rea
اSل2]ا ةلادلل د$!ت4 (هو اSتمe 3E <كحت6 j? دي06 ة]ا' Vا Parameter *ا ا$هو parameter *ا &ه side
"3E w':ل
t+e=rea=areaRFSquare&5,
O 23 P
#با$لا لصفلا : لاودلا Functions
*ا 5 *ا ةمe (ه side اSمAJ ا$ه نكلو argument C
اذام Parameter و Argument ؟
*ا ةtا!4 ,ك4 Parameter *ا نكل )ةلادلا (لJ ,A06و 5@? ددح6 3تلا ةمقلا (ه Argument (للا ةمقلا (ه
*ا j? ($2م4 ) ةلادلا (لJ 5لAر? parameter &ه side
*او Argument (ه 5
ةي"بملا :اودلا
(ه kلا ا$تلا' 3Eو ) اS!.@ ةGللا nل اSمدق6 *اود (هو Ruby
"70\ابم اSمدUت!6 j? Rطت!6و ) 78ها] دا&B? نI 7رابI (هو
print&?Lnter your name: ?, C9uiltin-Hunction to display an obSect &to;s,
name=gets C9uiltin-Function to read a string From t+e standard input#
puts ?1ello2 ?D name
print ,ثم ةلاد (ه put .. ديد] 0طA اSلJ MD6 :ل نكلو ة\اFلا (لI ة-@ ةل!لA ةIابt (E 9دUت!6
Rم h4ا!لا د&كلا ب0] " 1مل6 puts _• Vد-e <S.تAو
gets 7cا0e (E 9دUت!6 اDيا ةلاد (ه string *ا نم stdin
01ello2 0Dname
ا$ط26 Hello *ا Rم ة/مدم name ,ل g02ت$A .. Concatenation اق':ل Œ cا\ jJ ,-.تلا4
O 24 P
%ما&لا لصفلا : ام'(عملا ى)ب
"ا م -


ا ى
01
: 2
3
ا
4
5
ا ل
ص
فل
ا
تا1$<;ملا
(لIو .. ,-.لا 3E نيذلا ب=>طلا ,B cامA? mEو )ةAردملا (E Lذلا nل-E نI ا/ما@04 lتك6 =>ثم n@? ,U6
<S@? gا0تEا 30 Mي026 ,Uت6 ,ه )‚U\ 30 ؟؟0Gتم
" n!.@ l2تتA n@•E )<2@ 5لe اذJ D
jود4 ‹2ب4 ةطب60م تا0Gتم ةI&م/م ني8U6 (E 9دUت!6 تام&ل2ملل ة$4 نI 7رابI (هو )ةE&.-ملا (E نمكي ,حلا
R4ا6 .. Cد'او ,B <e ,مFتل تا0Gتم cاF@J
.. ةقي0t نم 0ثB? د]&ي )ةE&.-ملل نZاB cاF@إل ":لو?
1 ةقي0طلا 9ادUتAJ d new *ا نم Array Class *ا نم نZاB ا$ط26 (هو Array Class
ary==rray#ne. CCreate an empty array#
p ary
Coutput: -/
2 0\ابم دا$AJ d
ary=- /
p ary
Coutput: -/
ةقي0طلل )ة@اQ R]0@ new *ا =>ثم ةصاUلا MZاx&لا ‹24 اSل (هو populating cاF@J د0/م4 <e RWو (هو
"نZاكلا
ary==rray#ne.&B, CB nils4
p ary
Coutput: -nil2 nil2 nil2 nil/
ةقي0طلل ا$لe ا$ه new mE j&كيو )ةE&.-ملا Mص نم نZاB دي0@ ا$@J 4 ) 0صا$2لا <e ددح@ <ل ا$$كلو .. 0صا$I
تدbXE nil ..اWا0تEا
ary==rray#ne.&B,T?test?U Carray contains B elements all are test
p ary
Coutput: -?test?2 ?test?2 ?test?2 ?test?/
ةقي0طلل ا$لe ا$ه new mE j&كيو )ةE&.-ملا Mص نم نZاB دي0@ ا$@J 4 | (ه د'او ,B ةمeو 0صا$I test „
std"=5a+med5
##
O 25 P
%ما&لا لصفلا : ام'(عملا ى)ب
##
std*
(E ب=>طلا cامA? RD6 n@J &ه ,DE+لا ,حلاE Array (لاتلاB
students=-0a+med52 0ayman52 0c+ristina52 0rogina52 0.ael52 0mostaFa52 #### /
mب606 اSE 0-$I *و? ةE&.-ملا jJ w':ل 0
students=-?a+med?2 ?c+ristina?2 ?rogina?2 ?3ael?2 ?*amer?/
p students Cprint t+e array
Coutput: -?a+med?2 ?c+ristina?2 ?rogina?2 ?3ael?2 ?*amer?/
puts students-(/ *و+لا 0-$2لا ةIابt
Coutput: a+med
puts students-(##*/ *ا 0صا$2لا B (لو+لا
Coutput:
a+med
c+ristina
rogina
3ael
puts students--"/ C0b+لا 0-$2لا
Coutput: *amer
puts ?a+med included4? iF students#includeO&?a+med?,
Coutput: a+med included4
Include?(obj)
*ا نمW |ةلاد| ةقي0t (ه array class R]06و return ب true وا false *ا ,ه اها$2م 0bkلا نمو ) array ,مFت4
*ا object .:ل 9?
puts students#lengt+
Coutput: 5
length ةE&.-ملا 3E 7د&]&ملا 0صا$2لا ددI ا$ل د26 ةقي0t
size yل را2ت!م <Aا (ه length
puts students#siVe Calias For lengt+
Coutput: 5
delete_at(index)
دbX6 ةقي0t (ه 1 parameter *ا &هو index mEذح6 j? دي06 (للا 0-$2لل
students#delete;at&*, C.ael is remo!ed#
p students
O 26 P
%ما&لا لصفلا : ام'(عملا ى)ب
mب606 jاB Lذلا 0-$2لا ا$Eذ' ا$ه 3 نم l60تلا ?دب@ ا$@J w':لd 0 d
Coutput:-?a+med?2 ?c+ristina?2 ?rogina?2 ?*amer?/
clear ةE&.-ملا 3E 7د&]&ملا 0صا$2لا ,B Kذحل 9دUت!6 ةقي0t (ه
students#clear Cdelete all
p students
Coutput: (
empty?
ب ة@&ل&4 ةمe د26 ةقي0t (ه true وا false 0صا$I (لI L&ح6 :ل ةE&.-ملا 5@اB اذJ *ا' (E
students#clear
puts ?=rray is empty? iF students#emptyO
Coutput: =rray is empty
insert(index, object)
,ل 0-$I ةEاWJ (E 9دUت!6 ةقي0t array [0S.لا hي0t نI index =>ثم (لاتلاB vددح6 Lذلا
students=-?a+med?2 ?c+ristina?2 ?rogina?2 ?3ael?2 ?*amer?/
students#insert&B2 ?ayman?,
p students
Coutput: -?a+med?2 ?c+ristina?2 ?rogina?2 ?3ael?2 ?ayman?2 ?*amer?/
concat(array)
ةEاWJ (E 9دUت!6 ةقي0t (ه array =>ثم (لاتلاB ةلاحلل V0bا
students=-?a+med?2 ?c+ristina?2 ?rogina?2 ?.ael?2 ?*amer?/
students#concat&-?ramy?2 ?mona?2 ?*obaida?/,
p students
C-?a+med?2 ?c+ristina?2 ?rogina?2 ?.ael?2 ?*amer?2 ?ramy?2 ?mona?2 ?*obaida?/
last
ةE&.-ملا (E 0-$I 0bا د26 ةقي0t (ه
students=-?a+med?2 ?c+ristina?2 ?rogina?2 ?3ael?2 ?*amer?/
puts students#last Cstudents--"/
Coutput: *amer
ةقي0t (لJ <eر تر0م اذJ n@J w':ل last اذكهو ر0م

لا دد2لا T.$4 0صا$2لا 0bo nلJ د26 اS@•E
puts students#last&',
Coutput
3ael
O 27 P
%ما&لا لصفلا : ام'(عملا ى)ب
*amer
first
TكI (ه last ةE&.-ملا نم _<e0لاY *وا نم nل د2A <eر اSل تر0م اذJو 0-$I *وا د26 (هو امام6
puts students#First
Coutput: a+med
puts students#First&*,
ةQ=>Q *وا
Coutput
a+med
c+ristina
rogina
replace
ة@اQ ةE&.-م4 ةE&.-ملا4 0صا$2لا ,B *ادبتAJ (E 9دUت!6 ةقي0t (ه
langs=-?<ascal?2 ?C?2 ?<erl?/
langs#replace&-?6uby?2 ?<yt+on?2 ?CC?2 ?Wa!a?/,
p langs
Coutput: -?6uby?2 ?<yt+on?2 ?CC?2 ?Wa!a?/
reverse
"*اثملا 0i@ا )3!كI l60ت4 نكلو ةE&.-ملا نم ةU!@ R]06 ةقي0t (ه
re!ersedGangs=langs#re!erse
p langs
Coutput:-?6uby?2 ?<yt+on?2 ?CC?2 ?Wa!a?/ ## Not C+anged4
p re!ersedGangs
Coutput: -?Wa!a?2 ?CC?2 ?<yt+on?2 ?6uby?/
j? 5i':ل n@? دBا reverse (لI 0Gتلا Žدحي j? دي0@ ا$@? g0E (لI نكلو .. ةلص+لا ةE&.-ملا (لI 0QX6 <ل
9دUت!@ ا$@J &ه ,حلاE ؟ ةلص+لا ةE&.-ملا reverse C
(هام reverse ؟ C
اS@J K0Iا „C| l/2تلا ةم=>I اSE د/6 ةقي0t Vا destructive function m!.@ نZاكلا (لI j&كA اه0QX6 ($2ي
"m$م ةU!@ (لI Tلو m!.@ نZاكلا (E 0صا$2لا 5!كI )ا$لاثم (E ا$هو mتU!@ (لI Tلو
langs=-?<ascal?2 ?C?2 ?<erl?/
langs#re!erse4
p langs
Coutput: -?<erl?2 ?C?2 ?<ascal?/
O 28 P
%ما&لا لصفلا : ام'(عملا ى)ب
"sمدلل 3لاتلا *اثملا w':ل
old=-?C?2 ?<ascal?2 ?Hortran?/
ne.=-?<yt+on?2 ?6uby?2 ?Wa!a?/
all= oldDne. Cor all=old#concat&ne.,
p all
Coutput: -?C?2 ?<ascal?2 ?Hortran?2 ?<yt+on?2 ?6uby?2 ?Wa!a?/
all MM ?CC? C add cC
p all
Coutput:-?C?2 ?<ascal?2 ?Hortran?2 ?<yt+on?2 ?6uby?2 ?Wa!a?2 ?CC?/
all#pus+&?<erl?, C MM perl
p all
Coutput: -?C?2 ?<ascal?2 ?Hortran?2 ?<yt+on?2 ?6uby?2 ?Wa!a?2 ?CC?2 ?<erl?/
all#pop Cremo!e t+e last element#
p all
Coutput: -?C?2 ?<ascal?2 ?Hortran?2 ?<yt+on?2 ?6uby?2 ?Wa!a?2 ?CC?/
%0.لاو داح6إلاو Rtاقتلا ,ثم تا]ا' اSلI hبط6و ةI&م/مB اSمدUت!6 ردق6
langs=-?C?2 ?<yt+on?2 ?6uby?2 ?<ascal?/
intrep;langs=-?<yt+on?2 ?6uby?2 ?Xroo!y?/
,ماI 9دUت!$A داح6إلا 3E or • *ا &هو
Cunion
un=langsYintrep;langs
p un
Coutput: -?C?2 ?<yt+on?2 ?6uby?2 ?<ascal?2 ?Xroo!y?/
*ا 9دUت!$A %0.لا 3Eو minus d
CdiFFerence
diFF=langs-intrep;langs
p diFF
Coutput: -?C?2 ?<ascal?/
9دUت!$A Rtاقتلا 3Eو and operator • &هو
Cintersection
intSec=langs Z intrep;langs
p intSec
Coutput: -?<yt+on?2 ?6uby?/
uniq
را0ك6 jود4 نكلو 0صا$2لا نم ة@&كم ةE&.-ملا نم ةU!@ ا$ل د26 ةقي0t (ه
O 29 P
%ما&لا لصفلا : ام'(عملا ى)ب
uniq C
rقE 7د'او 70م vر&Sx ,2/6و 0صا$2لل تارا0كتلا ,B Kذح6 ةقي0t (ه
70ك.لا 1W&تل (لاتلا *اثملا R4ا6
ary=-"2 '2 * 2*2 52 %2 )2 )2 )2 "(/
p ary
uniq=ry=ary#uniq
p ary
p uniq=ry
ary#uniq4
p ary
Coutput:
-"2 '2 *2 *2 52 %2 )2 )2 )2 "(/
-"2 '2 *2 *2 52 %2 )2 )2 )2 "(/
-"2 '2 *2 52 %2 )2 "(/
-"2 '2 *2 52 %2 )2 "(/
sorting
ةقي0t 9دUتAا l60تلل sort ةب60م نكلو ةE&.-ملا نم ةU!@ د26 ةقي0t (هو
9دUتAJ ةE&.-ملا (لI امZاد ,يد2تلا j&كي j? تدر? اذا sort C
ary=-"2 '2 52%'*2 "B2 5"'/
p ary#sort Cnot c+anged# Sust a copy4
Coutput: -"2 '2 52 "B2 5"'2 %'*/
ary#sort4 Cdestructi!e
p ary
Coutput: -"2 '2 52 "B2 5"'2 %'*/
Bدملا Ranges
نم j&كتت4 70تE نI 7رابI &ه Vدملا start, end, step
rng"to"(="##"( C"( is included
p rng"to"(#to;a Cto;a means to array
Coutput: -"2 '2 *2 B2 52 %2 )2 $2 A2 "(/
rng"toA= "###"( C"( isn@t included
p rng"toA#to;a
Coutput: -"2 '2 *2 B2 52 %2 )2 $2 A/
O 30 P
%ما&لا لصفلا : ام'(عملا ى)ب
min
Vدملا 0صا$I 0Gص? ا$ل د26 ةقي0t (ه
max
Vدملا 0صا$I 0بB? ا$ل د26 ةقي0t (ه
rng="##"(
p rng#to;a
puts rng#min Cmin
Coutput "
puts rng#maI CmaI
Coutput "(
=>ثم )70ثB 0صا$I (لI L&ح6 3تلا تاE&.-ملا ني&ك6 (E 9دUت!ي ابلاz ة'ا0-4 Vدملا 9ادUتAJ
1 * 100
*ا وا a to z
alp+abet=&@a@##@V@,#to;a
p alp+abet
Coutput -?a?2 ?b?2 ?c?2 ?d?2 ?e?2 ?F?2 ?g?2 ?+?2 ?i?2 ?S?2 ?k?2 ?l?2 ?m?2 ?n?2
?o?2 ?p?2 ?q?2 ?r?2 ?s?2 ?t?2 ?u?2 ?!?2 ?.?2 ?I?2 ?y?2 ?V?/
.ords=&@abc@##@abk@,#to;a
p .ords
-?abc?2 ?abd?2 ?abe?2 ?abF?2 ?abg?2 ?ab+?2 ?abi?2 ?abS?2 ?abk?/
Rطقملا ا$ل تدا2E K0' 0bo (لI اcا$4 ($ب@ا Vدملا j? w':ل ab نم Kو0حلا ‘ a (لJ k
include?(obj)
ب د26و :ل وا Vدملا (E نZاB د&]و 0بتU6 ةقي0t (ه true وا false
Hashes
*ا Hash تام&ل2م ($4 نI 7رابI Data Structure ,بe اSتS]او j&ك6 j? نكميو تاGللا نم ديد2لا (E اد] 7ر&SFم
[&ماقلا ,ثم 0bo (م!م 5ح6 Dictionary (E Python وا Associative array (E Perl اذكهو
*ا &هام Hash ؟
ةمقلا و qات.ملا mل ددحت4 5@? (E0حلا ($2ملا4 [&ماe &ه key, value
cاF@إل :لوا HashTable ةقي0t 9دUت!$A new *ا نم Hash Class
+as+>able=1as+#ne. CCreate a ne. 1as+ obSect#
O 31 P
%ما&لا لصفلا : ام'(عملا ى)ب
وا
+as+>able=T U
ةEاWJ keys, values
+as+RbSect-key/=!alue
"*اثم
+as+>able-?name?/=?a+med?
+as+>able-?age?/ ="$
+as+>able-?seI?/ = @m@
p +as+>able
Coutput: T?name?=?a+med?2 ?seI?=?m?2 ?age?="$U
store(key, value)
ةقي0t 9ادUتAJ hي0t نI وا store نيا0تمارا4 دbX6 (هو
+as+#store&?lang?2 ?ruby?, C=dding key2 !alue by using store met+od#
Coutput:T?name?=?a+med?2 ?lang?=?ruby?2 ?seI?=?m?2 ?age?="$U
*ا4 <قلا 9ادUتAا hash
ةمe (لI *&-حلا Rطت!@ key *ا4 j8Uم hash *ا 9ادUتA•4 index ةقي0طلا وا fetch
+as+>able-key/ Creturns t+e !alue
+as+>able#Fetc+&key, Creturns t+e !alue
+as+=T?name?=?a+med?2 ?seI?=?m?2 ?age?="$U
puts ?Name: ? MM +as+-?name?/
Coutput: Name: a+med
puts ?seI : ? MM +as+#Fetc+&?seI?,
Coutput: seI : m
clear
*ا ,B Kذ' (E 9دUت!6 ةقي0t 3ه keys/values *ا نم HashObj
O 32 P
%ما&لا لصفلا : ام'(عملا ى)ب
shift
*وا د26 Key/Value ني0-$I نم ة@&كم ةE&.-م 7ر&ص (لI
+as+=T?name?=?a+med?2 ?seI?=?m?2 ?age?="$U
ary=+as+#s+iFt
p ary
Coutput: -?name?2 ?a+med?/
invert
*ا TكI (E 9دUت!6 ةقي0t (ه hash *ا *&حت4 9&قتE keys (لJ values *او values (لJ keys
+as+=T?name?=?a+med?2 ?seI?=?m?2 ?age?="$U
in!1as+=+as+#in!ert
p in!1as+
Coutput: T?m?=?seI?2 "$=?age?2 ?a+med?=?name?U
puts in!1as+-?a+med?/
Coutput: name
has_key?(key) #true/false
has_value?(value) #true/false
*ا has_key د&]و رابتbJ (E 9دUت!6 ةقي0t 3ه key *ا4 hash
*ا has_value د&]و رابتbJ (E 9دUت!6 ةقي0t 3ه value *ا4 hash
+as+#+as;keyO&?name?, Ctrue/False
+as+#+as;!alueO&"$, Ctrue/False
length
*ا length *ا (E `اوp+لا ددI (لI *&-حلا (E 9دUت!6 ةقي0t 3ه hash
+as+=T?name?=?a+med?2 ?seI?=?m?2 ?age?="$U
puts +as+#lengt+ Cnumber oF pairs
Coutput: *
to_a
*ا ,ي&ح6 (E 9دUت!6 hash (لJ array
+as+=T?name?=?a+med?2 ?seI?=?m?2 ?age?="$U
O 33 P
%ما&لا لصفلا : ام'(عملا ى)ب
ary=+as+#to;a Ccon!ert to array
p ary
Coutput: --?name?2 ?a+med?/2 -?seI?2 ?m?/2 -?age?2 "$//
keys
3E 7د&]&ملا 16ا.ملا ,B ,مF6 ةE&.-م د26 ةقي0t (ه hash
values
3E 7د&]&ملا <قلا ,B ,مF6 ةE&.-م د26 ةقي0t (ه hash
+as+=T?name?=?a+med?2 ?seI?=?m?2 ?age?="$U
keys=+as+#keys
p keys
Coutput: -?name?2 ?seI?2 ?age?/
!als=+as+#!alues
p !als
Coutput: -?a+med?2 ?m?2 "$/
,ي&ح6 Array * Hash
ary=-?First?2 "2 ?second?2 '2 ?t+ird?2 */
+as+=1as+-Eary/
p +as+
Coutput: T?second?='2 ?First?="2 ?t+ird?=*U
تايCاي*لا Dم /م4لا EF1اكم
ة@ا2تA:لا (لJ `اتح$A نح$E تاWاي0لا نI <لكت$A ا$@J ام4 Delta Force (E تاWاي0لل RUBY 7د'و (هو ..
.تاWاي0لا
7د'&لا <SEا module *ا نم ةI&م/م اS@? (لI Functions/Constants/Classes dاSبتك6 j? Rطت!6 وا 78ها] d
... ة$2م ةZ8/4 ةطب60ملا
و ruby 9ادUتAا *=>b نم ةل&S!4 تاWاي0لا تالمI sم0بملا (لI ,S!6 تاGللا نم ديد2لا ,ثم اSلثم Module
(هو 78ها] Math .sم0بملا m]اتحي Lذلا d<i2مd ,B اSE
اSمAJ تاWاي0لا 7د'و 3E ةلاد د]&6 constants "7د'&لا4 7د&]&ملا با&ثلا cامA? ,B د26 (هو
p Pat+#constants Cretrie!e all constants#
Coutput: -?L?2 ?<K?/
*ا ةمe PI
puts Pat+::<K
O 34 P
%ما&لا لصفلا : ام'(عملا ى)ب
Coutput: *#"B"5A'%5*5$A)A*
*ا ةمe E
puts Pat+::L
Coutput: '#)"$'$"$'$B5A(B5
.1Wاو m@? نx? نكلو ) *اودلا4 €اUلا *ود/لا <]06? j? RطتA? :ل اذل ةي8ل/@إلا ةGللا4 تاWاي0لا [رد@ نح@
.hQ&تلا *=>b نم اSلI Rلط6 j? Rطت!6و ةWاي0لا 9اSملا4 ةصاUلا %0طلا نم ديد2لا د]&ي
O 35 P
*+ا,لا لصفلا : ى")ملا ة-).ا/ ة0م$1لا 2 OOP 3
6 ى
78

ا 9
:
;<
ا = 9
>?
@
AB
ا :CD
ا
E
F
ا ل
ص
فل
ا OOP G
تاي,ا,2
"(لJ <!ق$6 تاGللا <i2م
1 ةZا0]J ة/م04 تاGل d
ة;ه (لI اSE sما@0بلا <!ق$ي Modules و Functions و Structures *ا Vp C
2 (ح$ملا ة$ZاB ة/م04 تاGل d
ة;ه (لI دا&B+لا اSE <!ق$6 (هو Classes/Modules
*ا <SE اذJ ‚UFلا j? <Sملا OOP ",ثم 70ثB تاGل <ل2تي 3كل q&ت.م hي0طلا j? د/A اد] Java/C++/C ^
اه0zو
jا!@J <م-6 n@? ,U6 Human %ر&لا (لI
تا.ص اSم!@ تا.-لا vذه .. jp&لاو *&طلا ,ثم تا.ص mل jا!@إلا Attributes *&ق' و? Fields تاGللا ‹24 (E
(E .. Ruby ة.ص (م!ت4 Attribute Cةملكلا (لI د&2تE
Cب0Fيو ,BXيو 9ا$يو (Fمي m@? ,ثم ) اSلم2ي اDي? *ا2E? mل اذه jا!@إلا
"اذكه vا$لe Lذلا ,B hبط$A
"3!Z0لا <م-تلا اذه
class 1uman
deF initialiVe

end
end
*ا w':ل initialize 3@ابلل d ةصاb ةقي0t (ه Constructor Rطت!6 )jا!@إلا ةلا' (E .. نZاكلا cاF@J نI 0ب2ت4 d
" =>ثم m6د:لو *&ق6 j? D
ةقي0t 9ادUتAا4 K&.-لا نم تا$Zاكلا c3F$@ ا$@? w'=>تA "ةi'=>م new ةقي0t 3Iدت!6 ةقي0طلا vذه ..
initialize .M-لا نم نZاكلا ا$ل 8S/6 اهرود4 3تلاو
O 36 P
*+ا,لا لصفلا : ى")ملا ة-).ا/ ة0م$1لا 2 OOP 3

" V0-$I jا!@J د0/م Tلوd j&للاو H&$لاو <Aإلا ,ثم jا!@إلا (E اهددح@ j? l/ي V0b? (E D d
class 1uman
deF initialiVe&name2 seI2 color,
[name =name
[seI =seI
[color=color
end
end
R]0م نI 7رابI (ه ’ *ا reference mل j&كي j? l/ي M-لا نم c(F$A Lذلا نZاكلا j? ($2م4 )3لاحلا نZاكلل
ةم=>2لا vذه mل j&كتE )<Aا
[name=t+e name oF t+e obSect
[seI = t+e seI oF t+e obSect
[color = t+e color oF t+e obSect
*&ق' (م!6 ) j&لو H&@و <AJ نم اها@دد' 3تلا تا.-لا .. jkلا دحل ,م] Fields تا.ص وا Attributes ..
*ا2E+لا ‚eا@ Actions اذSE ,2Eوا ذ.$ي }\ m@J ا$لe ا$@? املاt .. jا!@إلا اSلم2ي 3تلا تاE0-تلا 3هو ) {&ل!لا وا
ةلاد L? j? <Sملاو.. *اود اS!.@ (ه *ا2E+لا j? ةtا!4 ,ك4 ($2يو ؟ nلذB Tل? 0ماو+لا نم ةI&م/م m@J vا$2م
ةقي0t (م!6 M-لا ,bاد ة4&تكم Method .
.m!.@ jا!@إلا *ا2E? hبط@ j? ا$-ق$يو )m6ا.ص V&A lتك@ <ل نح@.. د24 ,متكي <ل m@+ل ا$.-ل R]0@
#لJ ... ,GتFي .. 9ا$ي .. ب0Fي .. ,Bاي ؟ اSلم2ي (تلا *ا2E+لا و? تاE0-تلا ام ؟ اذام ,م2ي jا!@إلا
.ا$.ص 3E *ا2E+لا vذSل hبط6 ,م2$A ) د]
"ةلاتلا 7ر&-لل ا$.ص *&حتA
class 1uman
deF initialiVe&name2 seI2 color,
[name =name
[seI =seI
[color=color
[legs='
[eyes='
end

deF mo!e&,
Cmo!ing code
end

deF sleep&,
Csleeping code
end

deF drink&,
Cdrinking code
O 37 P
*+ا,لا لصفلا : ى")ملا ة-).ا/ ة0م$1لا 2 OOP 3
end
end
3@ابلا &ه }\ *و? Constructor <ل تا.ص د]&6 m@? w':ل .. M-لل اS--U@ j? دي0@ 3تلا تا.-لا mE ا@دد'و
,ثم 3@ابلا (E اهددح@ j? ($2م اSل j&كي نلE )cا$ثتAJ =>4 [ا$لا ,B د$I ,2.لا4 7د&]&م 3ه نكلو 3@ابلا 3E اهددح@
.,]ر+لاو j&2لا ددI
ةU!$لا تا0Gتم اSم!@ نZاكلا4 ةقل2تملا تا.-لا و? *&قحلا ,كE Instance Variables *&ق' اSل ةU!@ ,B j+ل
’ اSE (تلا *&قحلا ,B (ه ةل&S!4 8متللو .. ة@اثلا نI ة.لتUم
(هو ا$لeام ,ثم *اود نI 7رابI &هو ا$.-4 €اUلا {&ل!لا }\ (@اQ sleep, work, drink, move
؟ M-لا 3E 3تلا *&قحلا Rم ,ما2ت@ j? ردق@ MB نكلو ,م]
Cةقط$م 0z <e RDي j? نكمم m@+ل CXطb اذهو *&قحلا (E <كحتي j? د'? V+ل 1م!6 jX4 (لو+لا .. نتقي0t د]&ي
*ا n$ك6 9دUت!6 j? Rطت!6 ةtا!4 ,ك4 )ة@اثلا ةقي0طلا get/set اS4 *د2ي j? 9دUت!ملا *واحA 3تلا <قلا 0بتU6و
,ل =>ثم (لاتلا w':ل )*&قحلا name field
CXetter
deF getName
return [name
end

CSetter
deF setName&ne.;name,
iF ne.;name#class 4= string:
puts ?Lrror4?
eIit&",
else
[name=ne.;name
end
end
ةقط$م 0z ةمق4 <Aإلا 0Gتل ةلواحم
a+med=1uman#ne.&?a+med?2 ?m?2 ?.+ite?,
a+med#setName&"(,
CL66R66666
3@ابلا4 }Fلا T.@ ,م26 j? Rطت!6 j? w':لو ..Œ cا\ jJ اSل g02ت$A ,DE? lلاAا?د]&6 Constructor (ت'
.ةححص ةقي0ط4 c3F@? نZاكلا j? نمD6
*ا getName ’ ,ل د26 ةقي0t (ه name
*ا setName ’ *ا *د26 ةقي0t (ه name (لJ new_name
O 38 P
*+ا,لا لصفلا : ى")ملا ة-).ا/ ة0م$1لا 2 OOP 3
*ا =>ثم sex *ا Hات4 Object
CXetter
deF getSeI
return [seI
end
9دUت!$A Getter " H&$لا *د2@ jا دي0@ :ل ا$@+ل nلذو rقE S
9دUت!6 j? Rط!6 H&$لا *د26 j? 5بب'? اذJ ,B (لI Setter " (لاتلاB D
CSetter
deF setSeI&ne.;seI,
[seI=ne.;seI
end
M-لا4 ةصاb تا0Gتم نI 7رابI (هو M-لا تا0Gتملا &هو ةU!$لا تا0Gتملا 0z تا0Gتملا نم 0bo H&@ (E
’’ ب ?دب6 (هو .. نZاكلل 5!لو
C<هاE 5!ل 5@? ةtا!4 ,ك4
8B0ت4 3لاتلا *اثملا R4ا6
class 1uman

[[N8P9L6;RH;18P=NS=(
C>+e Constructor
deF initialiVe&name2 seI2 color,
[name =name
[seI =seI
[color=color
[legs='
[eyes='
[[N8P9L6;RH;18P=NS D= "
end

deF mo!e&,
Cmo!ing code
end

deF sleep&,
Csleeping code
end

deF drink&,
Cdrinking code
end

deF 1uman#numberRF1umans
return [[N8P9L6;RH;18P=NS
end
end
O 39 P
*+ا,لا لصفلا : ى")ملا ة-).ا/ ة0م$1لا 2 OOP 3
’’ *ا w':ل NUMBER_OF_HUMANS تا$Zاكلا ,B j? ($2م4 .. تا$Zاكلا ,B 3E {0تFم اذه 0Gتملا ..
CmE 7د&]&ملا ةمقلا T.@ 9دUت!6
deF initialiVe&name2 seI2 color,
[name =name
[seI =seI
[color=color
[legs='
[eyes='
[[N8P9L6;RH;18P=NS D= "
end
*ا &هو M-لا (E 0Gتم د]&ي ديد] نZاB cاF@J Rم jا w':ل NUM_OF_HUMANS رادقم4 دي8تA mتمe 1 اذهو
hي0t نI نكلو نZاكلا hي0t نI <تي نل با!حلا دBX6 ,ك4و) v“اF@J <6 نZاB <B l!ح@ ردق@ ا$@J ) m$م KدSلا
C M-لا
*ا ةمe ا$ل د2تA 3تلا ةقي0طلا K02$!E NUM_OF_HUMANS اذل) rقE M-لا4 ةصاb اSل2/@ j? دي0@ نكلو
"ةلاتلا 7ر&-لا4 اSE02$A
deF 1uman#numberRF1umans
return [[N8P9L6;RH;18P=NS
end
<S.6 (ت' M-لا <AJا4 اها$قبA ا$@J w':ل Ruby mلI hلطي *اودلا نم H&$لا اذهو )M-لا4 ةصاb vذه ةقي0طلا j?
ة$Bا!لا *اودلا static .
*ا ب&لAJ j? &ه و rقE د'او }\ ‚ق$ي نكلو )ةب2للا 5مSE 5@? اذكه .. اد] ,م] Get/Set " =>م] Tل D
,م26 MB K02ي j? l/ي )n.ص 9دUت!A Lذلا ,U6 get و set nلU6 نكلو VداI (ل ةب!$لا4 .. *&قحلا
‚Zا-Uلا &هو ديد] 9&S.م 5مدe تاGل د]&6 .. V&W&E mب\ nمام? Lذلا د&كلا j? Tح6 Properties (هو
yل MلG6 نI 7رابI get/set

deF name
[name
end


deF name=&name,
[name=name
end
a+med=1uman#ne.&?a+med?2 ?m?2 ?.+ite?,
puts a+med#name
Coutput: a+med
O 40 P
*+ا,لا لصفلا : ى")ملا ة-).ا/ ة0م$1لا 2 OOP 3
a+med#name=?yousseF?
puts a+med#name
Coutput: yousseF
؟ اه ةط!4
attr_accessor
*ا 9ادUتAJ (هو r!4? ةقي0t 9دق6 34ور ) ,B (لI attr_accessor =>ثم (لاتلاB

attr;accessor :name
a+med=1uman#ne.&?a+med?2 ?m?2 ?.+ite?,
puts a+med#name
Coutput: a+med
a+med#name=?yousseF?
puts a+med#name
Coutput: yousseF
,م26 j? 5بب'? اذ•E getter/setter V+ل Attribute 9دUتA•E attr_accessor C
attr_reader
نكلو ة.-لا ,2/6 j? دي06 n@J g0E (لI sex اSل get " mI&@ 0Gي j? د'? دي06 :لو T4 D
9دUت!$!E attr_reader
attr;reader :seI
a+med=1uman#ne.&?a+med?2 ?m?2 ?.+ite?,
puts a+med#seI
Coutput: m
a+med#seI=?F?
CL66R64
attr_writer
اSل ة.-لل j&ك6 j? دي06 n@J g0E (لI set 9دUتA•E T4 attr_writer
Note *ا 9دUت!6 :ل " attr_writer ,م26 j? Rطت!6 .. (Iاد jود4 set 3@ابلا *=>b نم اSلقحل
O 41 P
*+ا,لا لصفلا : ى")ملا ة-).ا/ ة0م$1لا 2 OOP 3
ةGار$لا
*ا4 <ها.ملا <ها د'ا (ه ةQار&لا OOP jو0bkلا (Sت@J N' نم ?دبلا (هو
(تلا *ا2E+لاو تا.-لا ‹24 mل KاDم jا!@J :لJ &هام Mx&ملا نكلو Mx&ملل Mص <م-6 j? دي06 n@J g0E (لI
؟ nلذB Tل? )ن.x&ملا 8م6
class Lmployer M 1uman
deF initialiVe&name2 seI2 color2 salary,
super&name2 seI2 color,
[salary=salary
end

ةقي0t 9ادUتAJ w':ل super (لI+لا M-لا4 €اUلا 3@ابلا Lدا$6 ةقي0t (هو super class ,م26و set *&قحلل
*ا &هو jا!@”>ل Tلو m!.@ Mx&ملا4 €اUلا ,قحلاو m4 ةصاUلا salary *ا نم ددحA Employer Constructor
is_a(class)
.0bo Mص نم hتFم اذه نZاكلا ,ه 0بتU6
instance_of?(class)
؟:ل 9? M-لا نم نZاB &ه نZاكلا jاB اذJ 0بتU6 ةقي0t (ه
kind_of?(class)
؟:ل 9? ن2م Mص &ه نZاكلا ,صا jاB اذJ 0بتU6 ةقي0t 3ه
+"=1uman#ne.&?a+med?2 ?m?2 ?.+ite?,
puts ?+" is an instance oF +uman? iF +"#instance;oFO&1uman, Can RbSect oF t+e
1uman class
puts +"#class C1uman
emp"=Lmployer#ne.&?a+med?2 ?m?2 ?.+ite?2 '(((,
puts ?emp" is an instance oF Lmployer class? iF emp"#instance;oFO&Lmployer,
puts emp"#class CLmployer
puts +"#is;aO&1uman, Cis +" a +uman O
puts +"#is;aO&Lmployer, C is +" an employer O
puts emp"#is;aO&Lmployer, C is emp" an employerO
puts emp"#is;aO&1uman, C is emp" a +uman O
puts emp"#instance;oFO&1uman,
O 42 P
*+ا,لا لصفلا : ى")ملا ة-).ا/ ة0م$1لا 2 OOP 3
puts emp"#kind;oFO&1uman,
Coutput:
+" is an instance oF +uman
1uman
emp" is an instance oF Lmployer class
Lmployer
true
False
true
true
False
true
70ك.لا nل 1Wو دقتIاو اد] r!4 *اثملا
*ا .. ,م] Employer " ‹E0ي و? ,م2ي m@? ,ثم m4 ةصاUلا *ا2E+لا ‹24 mل D
",ما\ *اثم
class 1uman


[[N8P9L6;RH;18P=NS=(
C>+e Constructor
deF initialiVe&name2 seI2 color,
[name =name
[seI =seI
[color=color
[legs='
[eyes='
[[N8P9L6;RH;18P=NS D= "
end

attr;accessor :name2 :color
attr;reader :seI
deF mo!e&,
Cmo!ing code
end

deF sleep&,
Csleeping code
end

deF drink&,
Cdrinking code
end

deF 1uman#numberRF1umans
return [[N8P9L6;RH;18P=NS
end
end
O 43 P
*+ا,لا لصفلا : ى")ملا ة-).ا/ ة0م$1لا 2 OOP 3
class Lmployer M 1uman
attr;accessor :salary
deF initialiVe&name2 seI2 color2 salary,
super&name2 seI2 color,
[salary=salary
[state=??
end

deF .ork
C.orking code
end

deF get1ired&salary,
[salary=salary
[state=?+ired?
end

deF getHired
[salary=( Cno money :&
[state=?Fired?

end

deF empKnFo
s=?Name: ? MM [name
s MM ?2 State: ? MM [state
s MM ?2 Salary: ? MM [salary#to;s
return s
end

end
class Hirm
deF initialiVe&Firm;name2 manager,
[Firm;name=Firm;name
[manager=manager
[emps=-/
end

deF raiseLmployer&emp2 raise,
emp#salary D= raise
puts [manager D ?: ? D emp#name D ? is raised4\n?
end

deF +ire&emp2 salary,
emp#get1ired&salary,
[emps#pus+&emp,
puts [manager D ?: ? D emp#name D ? is +ired#\n?
end

deF Fire&emp,
emp#getHired
[emps#delete&emp,
O 44 P
*+ا,لا لصفلا : ى")ملا ة-).ا/ ة0م$1لا 2 OOP 3
puts [manager D emp#name D ?: ? D ?is Fired#\n?
end

deF employersGist
return [emps
end
end
Cdemo
Firm=Hirm#ne.&?1ig+->ec+?2 ?=+med :ousseF?,
\a+med=Lmployer#ne.&?a+med?2 ?m?2 ?.+ite?2 '(((,
\tina =Lmployer#ne.&?c+ristina?2 ?F?2 ?.+ite?2 B(((,
\.ael= Lmployer#ne.&?.ael?2 ?m?2 ?.+ite?2 *(((,
deF empsKnFo
puts ?----------------?
lst=-\a+med2 \tina2 \.ael/
For emp in lst
puts emp#empKnFo
end
puts ?----------------?
end
puts ?1iring###?
Firm#+ire&\a+med2 '5((,
Firm#+ire&\tina2 '5((,
Firm#+ire&\.ael2 *(((,
empsKnFo
puts ?Hiring .ael###?
Firm#Fire&\.ael,
empsKnFo
Firm#raiseLmployer&\a+med2 "(((,
empsKnFo
puts ?--------------?
puts ?>+e list oF employers: ?
For emp in Firm#employersGist
puts emp#name
end
"`0Uلا
1iring###
=+med :ousseF: a+med is +ired#
=+med :ousseF: c+ristina is +ired#
=+med :ousseF: .ael is +ired#
----------------
Name: a+med2 State: +ired2 Salary: '5((
Name: c+ristina2 State: +ired2 Salary: '5((
O 45 P
*+ا,لا لصفلا : ى")ملا ة-).ا/ ة0م$1لا 2 OOP 3
Name: .ael2 State: +ired2 Salary: *(((
----------------
Hiring .ael###
=+med :ousseF.ael: is Fired#
----------------
Name: a+med2 State: +ired2 Salary: '5((
Name: c+ristina2 State: +ired2 Salary: '5((
Name: .ael2 State: Fired2 Salary: (
----------------
=+med :ousseF: a+med is raised4
----------------
Name: a+med2 State: +ired2 Salary: *5((
Name: c+ristina2 State: +ired2 Salary: '5((
Name: .ael2 State: Fired2 Salary: (
----------------
--------------
>+e list oF employers:
a+med
c+ristina
C{د&B (E jاكم V? (E mمدUت!6 j? Rطت!6 9اI 0Gتم m@J vا$2م • mقب!ي 0Gتم Vا ةبAا$ملا4 اها
7د'&لا نI ا$ملك6 امل 0Bذ6 ,ه Module ؟ 78ها] *اود و 54ا&Q و K&.ص ةI&م/م اS@? ا$لeو
yل K&.-لا نم vا$لمI Lذلا ,B دbX6 n@? &ه nلI ام ,B empsInfo
=>ثم <S4 rب60م <Aا4 mم!6و ) Mلم 3E <S2D6و Business Mلملا ةياد4 3E 3لاتلا 0ط!لا RD6 و
module Business
mتياS@ 3E اذهو
end
"3لاتلا ,كFلل *&حتA
module 9usiness
class 1uman


[[N8P9L6;RH;18P=NS=(
C>+e Constructor
deF initialiVe&name2 seI2 color,
[name =name
[seI =seI
[color=color
[legs='
[eyes='
[[N8P9L6;RH;18P=NS D= "
end

attr;accessor :name2 :color
attr;reader :seI
deF mo!e&,
O 46 P
*+ا,لا لصفلا : ى")ملا ة-).ا/ ة0م$1لا 2 OOP 3
Cmo!ing code
end

deF sleep&,
Csleeping code
end

deF drink&,
Cdrinking code
end

deF 1uman#numberRF1umans
return [[N8P9L6;RH;18P=NS
end
end
class Lmployer M 1uman
attr;accessor :salary
deF initialiVe&name2 seI2 color2 salary,
super&name2 seI2 color,
[salary=salary
[state=??
end

deF .ork
C.orking code
end

deF get1ired&salary,
[salary=salary
[state=?+ired?
end

deF getHired
[salary=( Cno money :&
[state=?Fired?

end

deF empKnFo
s=?Name: ? MM [name
s MM ?2 State: ? MM [state
s MM ?2 Salary: ? MM [salary#to;s
return s
end

end
class Hirm
deF initialiVe&Firm;name2 manager,
[Firm;name=Firm;name
[manager=manager
[emps=-/
end
O 47 P
*+ا,لا لصفلا : ى")ملا ة-).ا/ ة0م$1لا 2 OOP 3

deF raiseLmployer&emp2 raise,
emp#salary D= raise
puts [manager D ?: ? D emp#name D ? is raised4\n?
end

deF +ire&emp2 salary,
emp#get1ired&salary,
[emps#pus+&emp,
puts [manager D ?: ? D emp#name D ? is +ired#\n?
end

deF Fire&emp,
emp#getHired
[emps#delete&emp,
puts [manager D emp#name D ?: ? D ?is Fired#\n?
end

deF employersGist
return [emps
end
end
deF empsKnFo
puts ?----------------?
lst=-\a+med2 \tina2 \.ael/
For emp in lst
puts emp#empKnFo
end
puts ?----------------?
end
deF demo

Firm=Hirm#ne.&?1ig+->ec+?2 ?=+med :ousseF?,
\a+med=Lmployer#ne.&?a+med?2 ?m?2 ?.+ite?2 '(((,
\tina =Lmployer#ne.&?c+ristina?2 ?F?2 ?.+ite?2 B(((,
\.ael= Lmployer#ne.&?.ael?2 ?m?2 ?.+ite?2 *(((,
puts ?1iring###?
Firm#+ire&\a+med2 '5((,
Firm#+ire&\tina2 '5((,
Firm#+ire&\.ael2 *(((,
empsKnFo
puts ?Hiring .ael###?
Firm#Fire&\.ael,
empsKnFo
Firm#raiseLmployer&\a+med2 "(((,
empsKnFo
O 48 P
*+ا,لا لصفلا : ى")ملا ة-).ا/ ة0م$1لا 2 OOP 3
puts ?--------------?
puts ?>+e list oF employers: ?
For emp in Firm#employersGist
puts emp#name
end
end
end
.7د'&لا vذه (لI ,مFي Mص ,مIا ةtا!4 ,ك4 9ادUتA”>لو
9ادUتA•4 7د'&لا (Iدت!6 *و:لا require ,م26و include 9ادUتA•4 اSل include
require ?business?
class Dem
include 9usiness
end
p=Dem#ne.
p#demo
Public/Private/Protected
public m]راb وا M-لا ,bاد cا&A <لا2لا ,كل اSلJ *&ص&لا jا اها$2م "
private rقE M-لا ,bادل اSلJ *&ص&لا j? اها$2م "
protected M-لا T.@ نم ةقتFملا K&.-لل وا M-لل اSلJ *&ص&لا jJ اها$2م "
class =
deF met+od"

end

deF met+od'

end

deF met+od*

end

end
class 9 M =
O 49 P
*+ا,لا لصفلا : ى")ملا ة-).ا/ ة0م$1لا 2 OOP 3

end
"تا$ZاB ,م2@
a==#ne.&,
b=9#ne.&,
j? w':ل method1, method2, method3 تا$Zاكلا *=>b نم <SمدUت!6 j? Rطت!6 A, B <S@+ل اذهو Public
.3Wا0تEا ,كF4
*ا (E C 9دUت!@ ا$@? K02@ نح@ =>ثم ^ private (]راUلا <لا2لل cا&A %0طلا 9ادUتAإل *&ص&لا ةلمI ددح@ (ت'
.3لاحلا M-لا نم hتFتA 3تلا K&.-لل وا
(E نكل Ruby *ا j+ل MلتUي RW&لا private ƒ اSE protected 3ه اSل&صو تا'=>ص ةقي0طلا j? ($2م4 Private
.ةقتFملا K&.-لا (E اSمدUت!6 j? Rطت!6
*و+لا4 اها$لe (للا 7دIاقلا (لI ا$Iد نكل
public m]راb وا M-لا ,bاد cا&A <لا2لا ,كل اSلJ *&ص&لا jا اها$2م "
private rقE M-لا ,bادل اSلJ *&ص&لا j? اها$2م "
protected M-لا T.@ نم ةقتFملا K&.-لل وا M-لل اSلJ *&ص&لا j? اها$2م "
اط!4 :لاثم ب0D@
(لJ h4ا!لا *اثملا ا$لدI اذJ
class =
deF met+od"
puts ?met+od"?
end

deF met+od'
puts ?met+od' calling met+od*?
met+od*
end

deF met+od*
puts ?met+od*?
end

pri!ate :met+od"2 :met+od*
end
class 9 M =
O 50 P
*+ا,لا لصفلا : ى")ملا ة-).ا/ ة0م$1لا 2 OOP 3
deF met+odB
puts ?met+odB calling met+od*?
met+od*
end
end
"تا$Zاكلا c3F$$A
a==#ne.&,
b=9#ne.&,
b'#met+odB
Coutput:
met+odB calling met+od*
met+od*
*ا j? w'=>تA a, b (لJ *&صو <Sل Tل method1, method3 <S@+ل C private تالم2لا (E <SمدUت!6 ردق6
.m!.@ M-لل ةلbادلا
"اذكه
deF met+odB
puts ?met+odB calling met+od*?
met+od*
end
($2م اذSE private .ةtا!4 ,ك4
نI <لكت@ نل protected j+ل Ruby ,ثم اSتل2/E اSم&S.م 5لمها public _"
#*طلا /يمح)
7ر&-4 ن4إلا M-لا وا 3I0.لا M-لا (E 3AاA+لا M-لا و? ب+لا M-لا (E 7د&]&م ةقي0t Mي026 7داIJ (هو
m2م <Z=>ت6
"*اثملا w':ل
class =
deF met+od"
puts ?=@s met+od" is called?
end

deF met+od'
puts ?=@s met+od' calling met+od*##?
met+od*
end

deF met+od*
O 51 P
*+ا,لا لصفلا : ى")ملا ة-).ا/ ة0م$1لا 2 OOP 3
puts ?=@s met+od* is called?
end

pri!ate :met+od"2 :met+od*

end
class 9 M =
deF met+od' Co!erride t+e protected met+od
puts ?9@s met+od' is called?
end

deF met+odB
puts ?Calling met+od"###?
met+od"
end

end
"<SمدUت!@
a==#ne.&,
CCalling t+e parent@s met+od'
a#met+od'
b=9#ne.&,
CCalling t+e o!erriden met+od' in t+e c+ild
b#met+od'

"`0Uلا
=@s met+od' calling met+od*##
=@s met+od* is called
9@s met+od' is called
jJ w':ل method2 *ا (E B *ا (E اS$I امام6 ة.لتUم 5حبصا A *ا (E اS.ي026 دI? اS@? lب!4 اذهو B
:اكH=ا دد4)
*=>b نم 7ديدI *اك\? د]&ي m@? &هو )r!4و اد] ,SA اها8Gم j? Rم [ا$لا نم 0ثB ةAا!' 0ث6 ةملكلا vذه
:لا' اS'0\XAو )د'او <AJ
"3لاتلا *اثملا w':ل
class =
deF met+od"
puts ?=@s met+od" is called##?
end
end
class 9
O 52 P
*+ا,لا لصفلا : ى")ملا ة-).ا/ ة0م$1لا 2 OOP 3
deF met+od"
puts ?9@s met+od' is called##?
end
end
<Aإلا T.$4 نتقي0t د&]و w':ل method1
,م2@ objects
a==#ne.&,
b=9#ne.&,
9دUت!@ method1 "ن$Qإلا4 7د&]&ملا
a#met+od"
b#met+od"
"7ر&-لا vذS4 وا
obSects=-a2 b/
For obS in obSects
obS#met+od"
end
" j&كA `0Uلا نتلاحلا اتلB (E
Coutput:
C=@s met+od" is called##
C9@s met+od' is called##
freeze, frozen?
Freeze mلI ,يد2تلا نم نZاكلا R$م6 ةقي0t 3ه
frozen ذ.$6 <6 نZاكلا ,ه رابتbJ (E 9دUت!6 ةقي0t 3ه – freeze :ل 9? mلI
s=?1ello?
s#FreeVe
s MM ?2 3orld4? Ccan@t be done as ?s? is FroVen4
puts ?FreeVed4? iF s#FroVenO
to_s
9ادUتAJ ةلا' (E نZاكلا نI 0ب26 ةقي0t (ه puts ‡3-$لا ,ثمتلا| m2م
to_i
* mلي&ح6 ةلواحم ةلا' (E نZاكلا نI 0ب26 ةقي0t (ه int
O 53 P
*+ا,لا لصفلا : ى")ملا ة-).ا/ ة0م$1لا 2 OOP 3
to_f
* mلي&ح6 ةلا' (E نZاكلا نI 0ب26 ةقي0t (ه float
to_a
* mلي&ح6 ةلا' (E نZاكلا نI 0ب26 ةقي0t (ه array
to_hash
* mلي&ح6 ةلا' (E نZاكلا نI 0ب26 ةقي0t (ه hash
O 54 P
#با,لا لصفلا : 4ا$!تلا
H
ا
+
IJ
!
ا :)
*
ا
E
F
ا ل
ص
فل
ا
times
times را0ك6 (E 9دUت!6 block *ا نم code 70م نم 0ثBا
5#times T
puts ?1ello4?
U

lتك@ام *د4 ا$ه
puts 01ello45
puts 01ello45
puts 01ello45
puts 01ello45
puts 01ello45
ا$مدUتAا times
"vذه 7ر&-لا4 اSمدUت!6 ردق6
5#times do
puts ?1ello4?
end
9ادUتA•4 do .. end
upto/downto
"#upto&B,T
puts ?1ello4?
U
5#do.nto&(,T
puts ?1ello4?
U
*ا نم C70ك.لا T.$ل mS4اFم 1 *ا (ت' 4 *ا ذ.@ Block *ا ديpو (لاتلا 1
نم ة@اثلاو 5 دحل 0 *ا ذ.@ block ‚ق@و (لاتلا 1
؟ m'0ط@ امو vدي8@ ام ةE02م jود4 ‚ق@و ديp ا$لe ا$@J 5i':ل ,ه
&ه ةtا!4 ,ك4 iterator *=>b ددحتي 0Gتم &هو loop
O 55 P
#با,لا لصفلا : 4ا$!تلا
=>ثم (لاتلا w':ل
5#times TYiY
puts ?K: ? MM i#to;s
U
"`0Uلا
K: (
K: "
K: '
K: *
K: B
*ا jJ 5i':ل ,ه i ةيادبلا (E 5@اB اSتمe 0 *ا ذ.$تي 70م ,B (Eو Block دي86 1 ؟
(لاتلاB اSبتك6 j? Rطت!6
5#times doYiY
puts ?K: ? MM i#to;s
end
*ا K&F@ ا$Iد )ةب2للا 5مSE jkلا 5@ا ,م] upto *او downto "ة@اQ
(#upto&5, do YiY
puts ?K: ? MM i#to;s
end
"3لاتلاB j&كA `0Uلا
K: (
K: "
K: '
K: *
K: B
K: 5
— ˜ *ا 9ادUتA•4 ةلاتلا 7ر&-لا (لI اSبتك6 j? Rطت!6
(#upto&5, TYiY
puts ?K: ? MM i#to;s
U
*ا jJ w':ل iterator *ا نم ?دب6 ا$ه mتمe 0 رادقم4 mتمe دي86 70م ,Bو 1 ƒ mتمeام دحل 5 *ا ذ.$6 (Sت$يو
Block
5#do.nto&(, T YiY
puts ?K: ? MM i#to;s
U
(لاتلاB j&كA `0Uلا
K: 5
K: B
O 56 P
#با,لا لصفلا : 4ا$!تلا
K: *
K: '
K: "
K: (
*ا jJ w':ل iterator *ا نم ت?د4 ا$ه mتمe 5 *ا دحل 0 رادقم4 mتمe ‚ق$ي 70م ,B (Eو 1
ب (لاتلاB اSمدUت!6 j? Rطت!6 do.. end
5#do.nto&(, do YiY
puts ?K: ? MM i#to;s
end
while
را0كتلا ةقل' (ه تاGللا <i2م (E را0ك6 ةقل' 0S\? ) ,م] while
loopCounter=( Cset
.+ile loopCounterM5 Ccondition
puts 0Goop Counter: C 0 MM loopCounter#to;s
loopCounter D= " Cincrement
end
*ا املاt loopCounter نم ,eا 5 _™0FلاY
*ا ذ.@ Block (لاتلا
puts 0Goop Counter: C 0 MM loopCounter#to;s
loopCounter D= "
*ا ةمe دي8@و counter رادقم4 1 (لJ mتمe ,-6ام دحل اذكهو 5 *ا نم `0Uيو loop
"`0Uلا
GoopCounter: C (
GoopCounter: C "
GoopCounter: C '
GoopCounter: C *
GoopCounter: C B
(لpا$6 اSلم2@ j? Rطت!@ 9اi$لا T.$4
loopCounter=5 Cset
.+ile loopCounter(
puts ?GoopCounter: C ? MM loopCounter#to;s
loopCounter -= " Cdecrement t+e counter
end
"`0Uلا
GoopCounter: C 5
GoopCounter: C B
GoopCounter: C *
GoopCounter: C '
GoopCounter: C "
O 57 P
#با,لا لصفلا : 4ا$!تلا
0b? *اثم
loopCounter=(
.+ile loopCounterM"(
puts ?GoopCounter: C ? MM loopCounter#to;s
loopCounter D= ' Cincrement t+e counter
end
اهرادقم 7داي8ل ا$ه `0Uلا 2
GoopCounter: C (
GoopCounter: C '
GoopCounter: C B
GoopCounter: C %
GoopCounter: C $
"*اثم
loopCounter="( Cset
.+ile loopCounter(
puts ?GoopCounter: C ? MM loopCounter#to;s
loopCounter -= ' Cdecrement t+e counter
end
"`0Uلا
GoopCounter: C "(
GoopCounter: C $
GoopCounter: C %
GoopCounter: C B
GoopCounter: C '
for
را0ك6 ةقل' for تاGللا <i2م (E را0كتلا تاقل' 0S\? د'? (ه
(E ا$هو Ruby را0كتلا ةقلحل ب0eا (ه foreach
"*اثم
ary=-"2 '2 *2 B2 5/
For el in ary
puts ?el: ? MM el#to;s
end
w':ل
for .. in .. end
O 58 P
#با,لا لصفلا : 4ا$!تلا
*ا (E 0-$I ,كل (402لا4 ary *ا ذ.@ Block (لاتلا
(E اSمدUت!6 j? Rطت!6 while (لاتلاB
ary=-"2 '2 *2 B2 5/
indeI=(
.+ile indeIMary#lengt+
puts ?el: ? MM ary-indeI/#to;s
indeI D= "
end
Mص Array * V0bا ةقي0t 9دقي for
ary=-"2 '2 *2 B2 5/
ary#eac+TYelY
puts ?el: ? MM el#to;s
U
9ادUتA•4 (هو iterator اDيا
"`0Uلا
el: "
el: '
el: *
el: B
el: 5
ب (لاتلاB اSمدUت!6 j? Rطت!6و do.. end
ary#eac+ do YelY
puts ?el: ? MM el#to;s
end
until
until تاGللا <i2م (E 70ثك4 7د&]&م 0z را0ك6 ةقل' (ه
lc=( Cset
until lc5 Ccondition
puts ?lc: ? MM lc#to;s
lc D= " Cincrement
end
"`0Uلا
lc: (
lc: "
lc: '
lc: *
lc: B
O 59 P
#با,لا لصفلا : 4ا$!تلا
lc: 5
"3لاتلا اها$2م را0كتلا ةقل' jJ w':ل
*ا 1ب-6 (ت' lc *ا نم š 5 (لاتلا {&لبلا ذ.@
puts ?lc: ? MM lc#to;s
lc D= " Cincrement
O 60 P
5ماثلا لصفلا : ة-ص)لا ل6ل,لا
9
:K
LM
ا ل
N
ل
E
F
ا :O
P
ا

!
ا ل
ص
فل
ا
ةي;"لا /,Iسلا ةسلJK
s=?1ello2 3orld# Sup4O?
puts s
Coutput: 1ello2 3orld# Sup4O
length
ة-@ ةل!لA 3E Kو0' ددI د26 ةقي0t (ه string
s=?t+is is a string?
puts s#lengt+
Coutput: "%
upcase/upcase 0
*ا upcase Kو0ح4 نكلو ة-$لا ةل!ل!لا نم ةU!@ 7داI•4 9&ق6 ةقي0t (ه uppercase
*ا upcase (ه C destructive * اSلي&ح6و اS!.@ ة-$لا ةل!ل!لا (لI ,يد2تلا4 9&ق6 ةقي0t uppercase
I=s#upcase Creturns a copy ?upcase?
puts I
Coutput: 1LGGR2 3R6GD# S8<4O
puts s
Coutput: 1ello2 3orld# Sup4O
s#upcase4 Cdestructi!e met+od4
puts s
Coutput: 1LGGR2 3R6GD# S8<4O
downcase/downcase 0
*ا downcase Kو0ح4 نكلو ة-$لا ةل!ل!لا نم ةU!@ 7داI•4 9&ق6 ةقي0t (ه lowercase
*ا downcase (ه C destructive (لJ Kو0حلا ,ي&ح6و اS!.@ ة-$لا ةل!ل!لا (لI ,يد2تلا4 9&ق6 ةقي0t lowercase
O 61 P
5ماثلا لصفلا : ة-ص)لا ل6ل,لا
I=s#do.ncase Creturns a copy ?do.ncase?
puts I
Coutput: +ello2 .orld# sup4O
puts s
Coutput: 1LGGR2 3R6GD# S8<4O
s#do.ncase4 Cdestructi!e met+od
puts s
Coutput: +ello2 .orld# sup4O

capitalize/capitalize 0
capitalize j&كي mE K0' *وا نكلو ة-$لا ةل!ل!لا نم ةU!@ 7داI•4 9&ق6 ةقي0t (ه capital
capitalize (ه C destructive (لJ mE K0' *وا ,ي&ح6و اS!.@ ة-$لا ةل!ل!لا (لI ,يد2تلا4 9&ق6 ةقي0t capital
reverse/reverse .
reverse Kو0حلا [&ك2م نكلو ة-$لا ةل!ل!لا نم ةU!@ 7داI•4 9&ق6 ةقي0t (ه
reverse اS!.@ ة-$لا ةل!ل!لا TكI (E 9دUت!6 ةقي0t (ه
s=?t+is is a string?
re!S=s#re!erse Creturns a re!ersed copy oF t+e string
puts re!S
Coutput: gnirts a si si+t
s#re!erse4 CmodiFy t+e obSect2 re!erse it4
puts s
Coutput: gnirts a si si+t
indexing
نI :لد4 اه0بت26و ة-@ ةل!لA mمAا 3F4 K0ت26 :ل تاGللا ‹24 (E .تاE&.-ملا4 ةقQو ةe=>I اSل ة-$لا ,A=>!لا
*ا ,ثم Kو0حلا نم ةE&.-م د0/م nلذ C C
.0.-لا4 اDي? ?دب6و ) [راS.لا ,ثم تاE&.-ملاو ة-$لا ,A=>!لا ن4 ةB0تFملا cا\+لا ‹24 (.E
ب د&]&ملا K0حلا (لI ,-ح6 j? Rطت!6 5@•E index ن2م
String[index]
&ه mA0SE j&كA K0' *وا 0
*ا ةمe 3ه دا2تA 3تلا ةمقلا j? w':ل char *ا *ود] نم ASCII 9دUت!$A اذلE chr method *ا ,ي&حتل
O 62 P
5ماثلا لصفلا : ة-ص)لا ل6ل,لا
integer value (لJ character
d &ه mA0SE j&كA 0b+لا K0حلا 1
puts s-(/CFirst2 returns t+e ascii !alue
Coutput: ""%
puts s-(/#c+r Ccon!erted to a c+ar
Coutput: t
puts s--"/ Clast2 returns t+e ascii !alue
Coutput: "(*
puts s--"/#c+r Ccon!erted to a c+ar
Coutput: g
String[From..To]
*ا نم Kو0حلا ,B 7داI•4 9&ق6 From ,ل To
puts s-"##B/
Coutput: +is
index
(E Kو0حلا l606 ا$ل د26 ةقي0t 3ه string (لاتلا =>ثم 0ب2تل ر&Sx *وا l606 و?
s=?t+is is a string?
puts s#indeI&?i?,
Coutput "
*وا l606 ا$ل د26 ةقي0طلا ا$ه i *ا (E (هو اSتل4اe (ه index 2
*ا MD@ ا$@•4 9ادUتAإلا ة.B *د2@ ردق@ offset *ا4 Nحبلا (E ةيادبلا jاكم وا string (لاتلاB
s=?t+is is a string?
puts s-$##s#lengt+/
Ca string
puts s#indeI&?i?2 $,
Coutput: "*
count
را0ك6 تا0م ددI 7داI•4 9&ق6 ةقي0t (ه char وا expression *ا (E string (لاتلاB =>ثم
s=?t+is is a string?
puts s#count&?i?, Ccount +o. many iS in s
Coutput: *
empty?
*ا jاB اذJ 0بتU6 ةقي0t (ه string :ل 9? ›راE
O 63 P
5ماثلا لصفلا : ة-ص)لا ل6ل,لا
puts ?it@s empty? iF ?+ello?#emptyO
include?(char)/include?(substring)
د&]و اS4 0بتU@ char وا substring *ا (E string :ل 9? m!.@
puts s#includeO&?.?, C+as ?.? c+ar
Coutput: true
puts s#includeO&?+e?, C+as ?+e? substring O
Coutput: true
chomp/chomp .
chop/chop C
chop *ا نم 0b+لا K0حلا Kذح4 9&ق6 ةقي0t (ه" string œ 5S]او اذJو n\r Cن$Qإلا امه <SEذح4 9&قت4
..اSمادUتAJ ذبحملا 0z نم
chop *ا (لI ,يد2تلا4 9&ق6 نكلو ةق4ا!لا ,مI T.@ (ه C string ةقي0t اS@+ل destructive
9ادUتAJ ,D.ي "دا\رJ chomp
chomp/chomp!(separator)
chomp 0b+لا K0حلا Kذ' Rم ة-$لا ةل!ل!لا نم ةU!@ 7داI•4 9&ق6 ةقي0t (ه separator œ &هو n œ وا r وا
*ا 5@J تدد' اذJ :لJ اWا0تEا امه=>B separator
chomp *ا (لI j&ك4 ,يد2تلا ام@Jو ةق4ا!لا ,مI T.@ (ه C string اS@+ل m!.@ destructive د&ثم
puts ?1ello2 3orld\n?#c+omp
Coutput: 1ello2 3orld
puts ?1ello2 3orld\r\n?#c+omp
Coutput: 1ello2 3orld
puts ?1ello2 3orld\r?#c+omp
Coutput: 1ello2 3orld
puts ?1ello2 \n3orld?#c+omp
Coutput:1ello2
C3orld
puts ?1ello2 3orld#?#c+omp&?d#?,
Coutput: 1ello2 3orl
O 64 P
5ماثلا لصفلا : ة-ص)لا ل6ل,لا
puts ?1ello2 3orld4?#c+omp&?4?,
Coutput: 1ello2 3orld
concat
نم 0ثBا sمد (E 9دUت!6 ةقي0t (ه string
s=?1ello2 ?
s#concat&?3orld4?,
puts s
Coutput: 1ello2 3orld4
s=?1ello2 ?
s MM ?3orld4?
puts s
Coutput: 1ello2 3orld4
s=?1ello2 ?
s D= ?3orld4?
puts s
Coutput: 1ello2 3orld4
Comparing
s"=?1ello?
s'=?1ello?
s*=?+ello?
puts ?s" equals s'? iF s"#eqlO&s',
puts s"#casecmp&s*, Cit doesn@t care about t+e case
puts ?s" == s*? iF s"==s* Cit doesn@t matc+ it4
Coutput:
s" equals s'
(
scan(pattern)
ةE&.-م 7ر&ص (E <هد26و ة-$لا ةل!ل!لا (E ن2م rم@ (لI 1!م ,مI (E 9دUت!6 ةقي0t (ه
c+ars=s#scan&/#/,
p c+ars Cinspect
Coutput: -?+?2 ?e?2 ?l?2 ?l?2 ?o?2 ?2?2 ? ?2 ?.?2 ?o?2 ?r?2 ?l?2 ?d?2 ?#?2 ? ?2
?s?2 ?u?2 ?p?2 ?4?2 ?O?/
s=?1ello2 3orld2 >est2 .+atO?
scann=s#scan&/\.D/, Cscan For .ordDc+ar
p scann
Coutput: -?1ello?2 ?3orld?2 ?>est?2 ?.+at?/
O 65 P
5ماثلا لصفلا : ة-ص)لا ل6ل,لا
split(separator)
,مI (E 9دUت!6 ةقي0t (ه split * separator د26و array ..
.ords=s#split&@ @, Csplit t+e space separator#
p .ords
Coutput: -?+ello2?2 ?.orld#?2 ?sup4O?/
join(separator)
,مI (E 9دUت!6 ةقي0t (ه join *ا 0صا$2ل array 7داIJو string
SoinedString=.ords#Soin&@ @,
CSoin t+e array elements .it+ a separator
puts SoinedString
Coutput: +ello2 .orld# sup4O
SoinedString=.ords#Soin&@M@,
CSoin t+e array elements .it+ a separator
puts SoinedString
Coutput: +ello2M.orld#Msup4O
gsub(pat, replacement), gsub!(pat, replacement)
*ادبتAJ (E 9دUت!6 ةقي0t (ه pat ب replacement
s=?1ello?
puts s#gsub&@e@2 @*@,
Coutput: 1*llo
puts s#gsub&@lo@2 @"(@,
Coutput: 1el"(
s=?K GRJL 6uby4?
puts s#gsub&/6uby/2 ?<yt+on?,
Coutput: K GRJL <yt+on4
freeze, frozen?
Freeze mلI ,يد2تلا نم نZاكلا R$م6 ةقي0t 3ه
frozen ذ.$6 <6 نZاكلا ,ه رابتbJ (E 9دUت!6 ةقي0t (ه – freeze :ل 9? mلI
s=?1ello?
s#FreeVe
O 66 P
5ماثلا لصفلا : ة-ص)لا ل6ل,لا
s#
s MM ?2 3orld4? Ccan@t be done as ?s? is FroVen4
puts ?FreeVed4? iF s#FroVenO
to_s
*ا نI 0ب26 ةقي0t (ه object 9ادUتAJ ةلا' (E puts vا2م
to_i
*ا نI 0ب26 ةقي0t (ه object * mلي&ح6 ةلواحم ةلا' (E int
to_f
*ا نI 0ب26 ةقي0t (ه object * mلي&ح6 ةلا' (E float
to_a
*ا نI 0ب26 ةقي0t (ه object * mلي&ح6 ةلا' (E array
to_hash
*ا نI 0ب26 ةقي0t (ه object * mلي&ح6 ةلا' (E hash
*ا (لI *اثم OOP/Strings ,ل 7ديد] ةقي0t ةEاW•4 String Class (هو atbash
class String
deF atbas+
a=&@a@##@V@,#to;a
a6e!=a#re!erse
s=selF#do.ncase
res=??
For c+ar in s#scan&/#/,
iF a#includeO&c+ar,
res MM a6e!-a#indeI&c+ar,/
else
res MM c+ar
end
end
return res
end
end
s=?1ello?
puts s#atbas+
Coutput: s!ool
O 67 P
5ماثلا لصفلا : ة-ص)لا ل6ل,لا
*ا jJ w':ل self *ا (ه string C
*ا ةEاWJ &هو 0bo *اثم rot13 method ,ل String Class
class String
deF rot"*
selF#tr&?=-Pa-mN-]n-V?2?N-]n-V=-Pa-m?,
end
end
puts 0a+med5#rot"*
Coutput: nuVrq
Heredoc string
&ه ةtا!4 ,ك4 string 0طA نم 0ثBا (لI دتمي multi-line string
+eredocString=MMstart
1ello2
i
am
a multi-line
string
start
puts +eredocString
*ا ددح6 Lذلا 5@? n@J w':ل tag *ا اS4 ,.ق6 n@J ™0F4 T4 ةيادبلا4 €اUلا string 5مدUتAJ ا@ا ا$هو start
C=>ثم
Coutput:
1ello2
i
am
a multi-line
string
*ا €&-U4 ةx&حلم symbols
*ا 0بتIا symbol m@اB string *ا *=>b 0Gتي نلو runtime *ا4 ةصاUلا %0طلل mE `اتح6 نلو string
ا$ه Rلtا دي8ملل
http://www.troubleshooters.com/codecorn/ruby/symbols.htm
O 68 P
#6اتلا لصفلا : IO 2 ل7دلا 8 9$&لا 3
:)
Q
ا
R
!
ا ل
ص
فل
ا IO GST
U5
ا Vل
W
د(ا 6
jا اق4اA ا@0Bذ puts/print/gets j+ل Rبطلا4 Xطb ة4ا]إلا ؟ nلذB Tل? )*اود نI 7رابI اS@? Ruby is Fully OOP
K puts/print/gets *ا 5ح6 ا&2قي Kernel module (هو module L? ,ثم اSلثم module اS@J اه8مي Lذلا نكلو
implemented by default
puts
*ا (لI ةIابطلا ةلمI (E 9دUت!6 stdout
print
*ا (لI ةIابطلا ةلمI (E 9دUت!6 stdout
gets
7cا0e (E 9دUت!6 string *ا نم stdin
ruby 9دقت4 تاGللا نم ديد2لا ,ثم اSلثم classes تا.لملاو تادل/ملا Rم ,ما2تلل 78ها]
تادلLملا
*ا 9دUت!@ تادل/ملا Rم ,ما2تلل Dir Class
'لاحلا معلا
pwd, getwd
*ا (ل2ل&-حلا (E امدUت!ي نتقي0t امه Current Path/Current Working Dir
,1جملا ريي2ت
chdir(to)
O 69 P
#6اتلا لصفلا : IO 2 ل7دلا 8 9$&لا 3
3ا,1جملا 4ا56%
mkdir(new_dir_name, permissions)
ةقي0طلا 9دUت!@ mkdir cاF@J (E directory *ا (E <Aإلا ددح@ ا$@J Nح4 ديد] new_dir_name (E 1يرا-تلاو
*ا permissions
3ا,1جملا 789
Dir#delete&dir;name,
Dir#unlink&dir;name,
Dir#rmdir&dir;name,
(هو ةلاتلا %0طلا 9دUت!@ delete, unlink, rmdir
,1جم ح#:ت
entries
0SiA )ت=>bدملا ةE&.-م (لI ةط!4 را0ك6 ةقل' ,مI د$Iو )دل/ملا تاي&تحم ,B اSE ةE&.-م د26 ةقي0t (ه
"ةE&.-ملا تاي&تحم ,B ا$يدل
Dir#c+dir&@C:\ruby\bin\\@, Cc+ange to t+e dir
For entry in Dir#entries&Dir#get.d,
puts entry
end
Dir#entries&Dir#get.d, TYentryY
puts entry
U
Dir#entries&Dir#get.d, do YentryY
puts entry
end
Dir#Foreac+&Dir#get.d, TYentryY
puts entry
U
Dir#Foreac+&Dir#get.d, do YentryY
puts entry
O 70 P
#6اتلا لصفلا : IO 2 ل7دلا 8 9$&لا 3
end
3ا,1جملا 3ان;ا$
,م26 ردق6 Dir Stream وا Dir Object وا Dir Handler ةقي0t 9ادUتA•4 ةم!تلا g0.6 )دي06 ام l!' (لI
open وا new *ا نم Dir Class
)ة$2م ةمSم 8/$تل ب&Aاحلا 5لG\ 5@? =>ثم ... mل.قي j? l/ي c3\ 1ت.ي Lذلا .. <Sم }\ (لI h.ت@ ا$Iد :لوا
.pاS/لا ,.ق6 اهpا/@J د2بE
*ا Rم ,ما2تلا (E 9اi$لا T.@ Dirs/Files
5حتE اذاو .. mل.ق6 j? l/ي دل/م 5حتE اذJ File اDي? mل.ق6 j? l/ي
*ا 1ت.@ ...اد] ,م] Stream (لاتلاB
dirRbS=Dir#open&@C:\ruby\bin\\@,
dirRbS#close
ةقي0طلا 9دUت!6 j? Rطت!6 new .. اق4اA ت0\ا املثم
path
*ا (لI *&-حلل path
puts dirRbS#pat+
Coutput: C:\ruby\bin\
tell
*ا د26 current position *ا (E Directory
read
*ا ?0ق6 next entry
O 71 P
#6اتلا لصفلا : IO 2 ل7دلا 8 9$&لا 3
rewind
,م26 set ,ل current position *ا (E Directory d ةيادبلا (لJ 0 d
each
*ا ةلواحم (E اSمدUت!@ iteration *ا (لI entries
dirRbS#eac+ do YentryY
puts entry
end
dirRbS#eac+ TYentryY
puts entry
U
"*اثم
dirRbS=Dir#open&@C:\ruby\bin\\@, Copen
CdirRbS=Dir#ne.&@C:\ruby\bin\\@,
puts dirRbS#pat+
puts dirRbS#tell Ct+e current position
puts dirRbS#read C.+at@s it O
puts dirRbS#tell Ct+e current position
puts dirRbS#read C.+at@s it O
puts dirRbS#tell Ct+e current position
puts dirRbS#read C.+at@s it O
Cset t+e current position to (
dirRbS#re.ind
puts dirRbS#tell
dirRbS#close
Coutput
C:\ruby\bin\
(
#
"
##
'
erb#bat
(
O 72 P
#6اتلا لصفلا : IO 2 ل7دلا 8 9$&لا 3
تا<لملا
"<Sم }\ (لI h.ت@ ا$Iد :لوا
r = read only
rD = read/.rite
. = .rite only
.D = .rite/read
a = append at t+e end or create
aD = append at t+e start or create
b = binary
delete(file_path)
Mلملا Kذحل 9دUت!6 ةقي0t (ه
(لاتلاB اSمدUت!6 j? Rطت!6 وا
delete&File"2 File'2 File*,
rename(from, to)
,مI (E 9دUت!6 ةقي0t (ه rename ,ل file
ctime(file_path) (creation time)
*ا cاF@ا #يرا6 د26 ةقي0t (ه file
mtime(file_path) (last modification time)
*ا (لI ,يد26 0bا #يرا6 د26 ةقي0t (ه file
atime (file_path) (last access time)
*ا 9ادUتAJ <6 70م 0bا #يرا6 د26 ةقي0t (ه file
puts Hile#mtime&?C:\\ls#rb?,
puts Hile#ctime&?C:\\ls#rb?,
puts Hile#atime&?C:\\ls#rb?,
Coutput:
Pon No! '% ($:*5:5* LL> '(()
Pon No! '% ($:*5:5* LL> '(()
Pon No! '% ($:*5:5* LL> '(()
O 73 P
#6اتلا لصفلا : IO 2 ل7دلا 8 9$&لا 3
zero?
.:ل 9? 0.ص Lوا!ي Mلملا *&t jاB اذJ 0بتU6
*ا (ه اSل *اثم ب0eا files 9ادUتAا4 c(F$6 (للا touch
exist?(file_name)
*ا ,ه رابتbJ (E 9دUت!6 ةقي0t (ه file :ل 9? د&]&م
exists?(file_name)
نكلو اSتق4اA T.@ obsolete
readable?(file_name)
7co0قلل ,4اe Mلملا ,ه
writeable?(file_name)
ة4اتكلل ,4اe Mلملا ,ه
Executable?(file_name)
ذ.$تلل ,4اق.لملا ,ه
size(file_name), size?(file_name)
ة'ا!ملا
ftype
.Mلملا H&@ ديدح6 (E 9دUت!6
directory?(file_name)
.:ل 9? (ط2ملا <A=>ل h4اطم ,ه دل/م رابتbJ (E 9دUت!6
sما@0بلا ةياS@ 3E ,.قي j? l/ي رد-مل 9ادUتAا L? ا$ق.6ا املثم
O 74 P
#6اتلا لصفلا : IO 2 ل7دلا 8 9$&لا 3
ة'اتكلا
syswrite, puts, putc
syswrite و puts ة4اتB (E ا&مدUت!ي string *ا (E file
putc ة4اتB (E 9دUت!ت4 character
CCreate a File RbSect in 3rite mode2 iF it eIists it@ll be o!er.ritten
FileRbS=Hile#ne.&@C:\tst6uby#tIt@2 @.@,
C<rint a string to t+e File by using sys.rite met+od oF t+e File RbSect
FileRbS#sys.rite&?1ello From 6uby KR4?,
CClose t+e File obSect4
FileRbS#close
6eading
FileRbS=Hile#ne.&@C:\tst6uby#tIt@2 @r@,
C6ead it
FileSrc=FileRbS#gets
Cprint it
puts FileSrc
FileRbS#close&, CClose it#
each_byte
,مI (E 9دUت!6 ةقي0t (ه iteration ,B (لI char
CCreate a File RbSect in read mode
FileRbS=Hile#ne.&@C:\tst6uby#tIt@2 @r@,
C6ead it
FileRbS#eac+;byte TYc+arY
putc c+ar
U
CClose it
FileRbS#close&,
HileCopy#rb
O 75 P
#6اتلا لصفلا : IO 2 ل7دلا 8 9$&لا 3
O 76 P
$:اعلا لصفلا : ا;ا)ثت6لا ة0لاعم
"ا XYلا 9
>5
ا
ع
م
:@
Z
ا
ع
ل
ا ل
ص
فل
ا
1ق$تلا ةلمI Debugging sما@0بلا 3E ,-' &لو .. %&!لا *8$ي j? ,بe رابتbإلا 5ح6 sما@0بلا RWو ةلمI 3ه
<هرود4 <هو ),صاحلا XطUلا H&$4 0ي&طتلا hي0.ل ,A0A 9دUت!ملاو 9دUت!ملل 70\ابم 0SiA m@•E ..ام Xطb
.. اذكهو %&!لا 3E mلا8@J و V0b? 70م sما@0بلا رابتbا4 ا&م&قي اهد24و sما@0بلل ةيرد-ملا 70.\ 3E XطUلا ا&/لا2A
تاcاثتA:لا ة/لا2م (م!6 70Fت$م V0b? ة$ق6 د]&6 Exceptions ةحWاو ةلاAر راSxJو XطUلا ة/لا2م4 9&ق6 Nح4
؟ nلذB Tل? 70ملا4 ةقط$م 0z 0.ص (لI ةم!قلا ةلمI =>ثم .. ةم&S.م ةقي0ط4 9دUت!ملل
"3لاتلا د&كلا ذ.$6 j? ب0]
puts "/(
"3لاتلا د/تA
di!ided by ( &]eroDi!isionLrror,
؟ةلم2لا vذS4 <كحت$A MكE
9ادUتA•4 &ه ,حلا begin/rescue/end
ةما2لا 7ر&-لا
begin
Cyour normal code goes +ere#
Cerror4
rescue
Crescue code
ensure
Cal.ays +appen4
end
begin
puts "/(
rescue ]eroDi!isionLrror
puts ?Ne!er studied mat+2 you geniusO :ou are di!iding by Vero4?
eIit&", Cterminate
end
(E ةلكFملا ة/لا2م4 9&ق$A rescue Y وا catch/except block "ةلاتلا ةلاAر 0Si@ j•4 _
?Ne!er studied mat+2 you geniusO :ou are di!iding by Vero4?
9ادUتA•4 sما@0بلا نم `0U@و exit
5eو (6? jkلا ) ,م] ensure *اY finally _
O 77 P
$:اعلا لصفلا : ا;ا)ثت6لا ة0لاعم
ensure Cذ.$6 j? l/ي اSلتB
begin
puts "/(
rescue ]eroDi!isionLrror
puts ?Ne!er studied mat+2 you geniusO :ou are di!iding by Vero4?
eIit&", Cterminate
ensure
puts ?>+is is al.ays appeared4?
end
نم 7دا.تAإلا m]و ام ‚U\ *XA اذJ ensure ؟
ensure اذكهو *ا-6ا وا Mلم =>ثم q&ت.ملا رد-م ,.e (E اSمدUت!6 j? Rطت!6
,-.6 j? Rطت!6 ),م] Exceptions " n]ا8م (لI D
"(قبط6 *اثم
؟Me&ملا Rم ,ما2تA MكE )9داقلا نم K02ي :ل &هو ) [0/لا %د RمA ,.طلا j? g0E (لI
mE02م 0Siي mل2/$A id ؟nلذB Tل? )ةt0Fلا lلطي و بابلا ,.قA :لJو
,م2$A *و+لا Error [0/لا %د ابي0z ا-U\ j? (هو ا$تلكFم نI 0ب2ل €&-Uم
class KntruderQnocksLrror M StandardLrror

end
"ا@د&B RD$A ) اذه د24
begin
door;closed=False

.+o=?intruder?
iF .+o==?intruder?
raise KntruderQnocksLrror2 ?Can@t open For intruders#?
end
rescue KntruderQnocksLrror = e
puts e#message CLrror message
p e#backtrace Cbacktrace4 & 3+ere .as t+e eIception raised ,
puts ?s+o. me some id## -y/n/: ?
ans=gets#c+omp4#do.ncase
iF ans==@y@
.+o=?kno.n?
puts ?+i2 i kno. you4?
else
puts ?you .anna kidnap meO calling police##?
eIit "
end

ensure
O 78 P
$:اعلا لصفلا : ا;ا)ثت6لا ة0لاعم
door;closed=true
end
70ك.لا T.@ m$كل Reا&لا نI اد] MلتUي اده 9=>كلا ا2بt
EوI9
Reflection
*ا Rم ,ما2تلا (لI 7ردقلا (ه ةtا!4 ,ك4 meta data Lو? Mص L? و رد-ملا 3E اذام K026 j? Rطت!6 Nح4
...اذكهو ةقي0t ,B اهذbX6 0تمرا4 L? و )اSي&تحي ةقي0t
s=?1ola4?
نZاB د0/م 3ه object VداI
class
*ا H&@ (لI *&-حلا (E 9دUت!6 ةقي0t (ه class *ا ,مI <6 Vذلا object m$م
Cs is an obSect From .+atO
puts s#class
Coutput: String
class.name
*ا <AJ (لI *&-حلا (E 9دUت!6 class
C.+at is t+e name oF t+is class O
puts s#class#name
Coutput: String
superclass
*ا (لI *&-حلا (E 9دUت!6 parent class ,ل class
CFrom .+at String .as in+eritedO
puts String#superclass CobSect is t+e parent class oF @em all4
included_modules
*ا (لI *&-حلل modules V&ت'ا اSل ,مI 3تلا include *ا (E class
Ct+e included modules in String
p String#included;modules
Coutput: -Lnumerable2 Comparable2 Qernel/
O 79 P
$:اعلا لصفلا : ا;ا)ثت6لا ة0لاعم
p RbSect#included;modules
Coutput: -Qernel/
object_id
,B object mل unique id *ا nلذ (لI *&-حلا (E 9دUت!6 ةقي0طلا vذهو id
Ceac+ obSect +as a unique id#
s"=?1i?
puts s"#obSect;id C%
s'=?1i?
puts s'#obSect;id C$
*ا T.$ل او0Fي ن$ZاB jJ *ا' (E (لاتلا w':ل object
s"=?1i?
s'=s" Cpoints to s"
puts s"#obSect;id C%
puts s'#obSect;id C%
constants
د26 ةقي0t (ه array *ا ,B اSE constants 7د&]&ملا
p Pat+#constants
Coutput: -?L?2 ?<K?/
local_variables
*ا (لI *&-حلا (E 9دUت!6 local variables sما@0بلا (E
Cget t+e local !ariables
p local;!ariables
Coutput: -?s?2 ?s"?2 ?s'?/
global_variables
*ا (لI *&-حلا (E 9دUت!6 global variables
\globString=?3+at@s upOOOO?
\anot+erXlobString=?C68LG2 3R6GD4?
Cgets t+e global !ariables
p global;!ariables
Coutput
C-?\HKGLN=PL?2 ?\GR=DLD;HL=>86LS?2 ?\anot+erXlobString?2
C ?\JL69RSL?2 ?\globString?2 ?\<6RX6=P;N=PL?2 ?\GR=D;<=>1?2
C ?\DL98X?2 ?\stdin?2 ?\QCRDL?2 ?\stderr?2 ?\stdout?2 ?\deFout?2
C ?\deFerr?2 ?\-K?2 ?\^?2 ?\-Q?2 ?\\\?2 ?\;?2 ?\4?2
O 80 P
$:اعلا لصفلا : ا;ا)ثت6لا ة0لاعم
C ?\\??2 ?\-a?2 ?\-d?2 ?\_?2 ?\[?2 ?\O?2 ?\?2
C ?\=?2 ?\M?2 ?\:?2 ?\(?2 ?\#?2 ?\/?2 ?\2?2 ?\-n?2
C ?\E?2 ?\S=HL?2 ?\D?2 ?\-p?2 ?\Z?2 ?\@?2 ?\\?2 ?\-l?/
instance_variables
*ا (لI *&-حلل 9دUت!ت4 instance_variables
p instance;!ariables C -/
methods
*ا (لI *&-حلل 9دUت!6 methods *ا4 ةصاUلا class
Cget all oF t+e met+ods in string
met+ods=ry=String#met+ods
p met+ods=ry
Coutput:
C-?ne.?2 ?superclass?2 ?allocate?2 ?in+erited?2 ?initialiVe;copy?2
#####################
C ?class;e!al?2 ??2 ?M?2 ?pri!ate;class;met+od?2
2
C ?protected;met+ods?2 ?nilO?2 ?FreeVe?2 ?is;aO?2 ?eqlO?/
instance_methods
*ا (لI *&-حلا (E 9دUت!6 methods *ا4 ةصاUلا instance
Cinstance met+ods
p String#instance;met+ods
private_methods
*ا (لI *&-حلا (E 9دUت!6 private methods C7د&]&ملا
Cget t+e pri!ate met+ods#
p String#pri!ate;met+ods
public_methods
*ا (لI *&-حلل public methods
protected_methods
*ا (لI *&-حلل protected methods
singleton_methods
*ا (لI *&-حلا (E 9دUت!ي singleton methods
protected_methods
*ا (لI *&-حلل protected methods
O 81 P
$:اعلا لصفلا : ا;ا)ثت6لا ة0لاعم
Rم }Fلا T.@و
[private|protected|public]_instance_methods
respond_to?(:methodName)
ةقي0طلا 9ادUتAJ نكمي ,ه ةE02م (E 9دUت!6 methodName *ا Rم object :ل 9ا
s=?1ello?
puts s#respond;toO&:upcase, Ctrue: can use upcase met+od
puts s#respond;toO&:keys, CFalse: can@t use keys met+od
class hierarchy
0ي&طتلا ة;4 ددح6 MB 5لcا!6 ,ه IDEs *ا super classes (لاتلا *اثملا R4ا6 ؟ <S$م hتFم ن2م M-4 ةصاUلا
CC V0b? 70م راتح6 نلو
class Hirst M String

end
class Second M Hirst

end
class >+ird M Second
end
c=>+ird
.+ile c
print&c,
print&? M ?,
c=c#superclass

end
puts
Coutput: >+ird M Second M Hirst M String M RbSect

O 82 P
$<= >+ا"لا لصفلا : XML & Ruby
:@
[\
]D
ا
^
5
ا ل
ص
فل
ا XML & Ruby
* را-تbا (ه EXtensible Markup Language اS@X4 ةحWاو (هو Markup language *ا ,ثم HTML نكلو
CC %0E د]&ي
XML vs HTML
9&Aو (لI jاي&تحي jا$Qإلا tags *ا j? %0.لا نكل HTML 9دUت!6 tags *ا نكلو 78ها]و 7ددحم XML 5@ا
*ا اSE H0تU6 Lذلا tags .n4 ةصاUلا
j? &هو اDي? اد] <Sم %0E HTML *ا نكل g02لل XML (E اSمدUت!6 j? Rطت!6 دBX6 ,ك4 )تا@ابلا ني8Uتل
.H&W&ملا اذه نم ا$Eده اذه Tل نكلو g02لا
*ا (لI دمت26 0ثB تا$ق6 {ا$ه XML ةم&A0لا تاS]ا&لا ,مI 3E (ت' ) GUI (E تام&ل2ملا w.تح6 N' C
ب Mلم xml syntax . *ا jا 5لe اذا اGلابم j&B? نل .. NET *او Java *ا (لI نمZاe xml .70ثB cا\? (E
(لاتلاB Mلم nلم@ ا$@ا g0E (لI books.xml
MOIml !ersion=?"#(? encoding=?8>H-$?O
M4--
Document : books#Iml
Created on : =pril "A2 '(($2 "(:(" =P
=ut+or : a+med
Description:
<urpose oF t+e document Follo.s#
--
Mbooks
Mbook
Mid"M/id
MnameKntroduction to 6ubyM/name
Maut+or=+med :ousseFM/aut+or
Mprice'(M/price
M/book

Mbook
Mid'M/id
MnameKntroduction to <yt+onM/name
Maut+or=+med :ousseFM/aut+or
MpriceB(M/price
O 83 P
$<= >+ا"لا لصفلا : XML & Ruby
M/book
Mbook
Mid*M/id
MnameKntroduction to C M/name
Maut+or=+med PostaFaM/aut+or
Mprice)(M/price
M/book

Mbook
MidBM/id
MnameKntroduction to <erlM/name
Maut+orP;Sp=.NM/aut+or
MpriceB(M/price
M/book

M/books
* <!قمو <i$م m@? 1Wاو books <D6و book element <Dي <SE د'او ,Bو id و name و author و price
• 7ر&-لا vذS4 <Aو Vا w':ل start></start 0-$I (م!ي š element
*ا XML *ا (E configuration files . (E د0/ملا ‚$لا Tك24 )vذه ,ثم ةحي0م j&ك6 ini (E (ت' وا =>ثم
دادIا تا.لم grub و lilo
"تا&طb 7دI (لI <ت6 اSلB ةلم2لا
1 ,م26 n@? d import * REXML (لاتلاB
CS>L< " &import reIml module,
require ?reIml/document?
include 6LXPG
2 *ا Mلم (لI ,bد6 n@ا d xml cاF@ا hي0t نI =>ثم )(لاتلاB Document object *ا نم Document class
*ا (E د&]&ملا unit "اS$لم' (تلا
CS>L< ' &load t+e Iml document,
ImlDRC=Document#ne.&Hile#ne.&?books#Iml?,,
*ا 9دUت!6 j? Rطت!6 HEREDOC string *ا H&W&م l'? :ل نكلو Hard Coding sما@0بلا وا 540ك!لا (E
m!.@
Mلم 3E باتB ,B <Aا Rبط@ jX4 nي?ر ام xml ؟
ImlDRC#elements#eac+&?books/book/name?, do YelementY
puts element#teIt
O 84 P
$<= >+ا"لا لصفلا : XML & Ruby
end
CRutput
Kntroduction to 6uby
Kntroduction to <yt+on
Kntroduction to C
Kntroduction to <erl
<A•4 0-$I ,كل name *ا ةIابt <تتA text jا w':لY element 0z دي0@ :ل نح@و m6اذ4 ,قت!م Mص &ه text _
5بتB اذا puts element "3لاتلاB }\ nل 0SiA
MnameKntroduction to 6ubyM/name
MnameKntroduction to <yt+onM/name
MnameKntroduction to C M/name
MnameKntroduction to <erlM/name
*ادبتAا4 9&ق$A )ن.لˆملا cامA? ا@در? اذJ 70ك.لا T.$4و name ب author
"ة2ي0A ةحمل
aut+ors=-/
ImlDRC#elements#eac+&?books/book/aut+or?, do YelementY
aut+ors#pus+&element#teIt,
end
Cuniq4 it
aut+ors#uniq4
p aut+ors
Coutput -?=+med :ousseF?2 ?=+med PostaFa?2 ?P;Sp=.N?/
3لكلا lتكلا 02A ةE02م دي0@ ا$@? (هو ةحمل 0bo
Cget sum oF prices O
sum=(
ImlDRC#elements#eac+&?books/book/price?, do YelementY

sum D= element#teIt#to;i
end
puts ?>otal: ?D sum#to;s
Coutput: ")(
ب&لA? (م!ي 3لاحلا ب&لA:لا DOM (لI دمت2ي &هو tree (E Mلملا ,B j8Uي N'Y tree structure (E
_70Bاذلا
ب&لA? &هو )jو0ثB L?0لا ا$قEا&ي و mلDE? ا-U\ )0bo ب&لA? د]&ي SAX *ا (لI دمت2ي &هو events (E ?دبي m@ا
tag *ا Rم ,ما2تي &SEY ن2م tag *او attributes _
O 85 P
$<= >+ا"لا لصفلا : XML & Ruby
Mstart attr"=!al attr'=!al' D=>= M/start
تا@ابلا 0يديو
Mstart attr"=!al attr'=!al' D=>= M/start
(S$ي m@او tag ن2م
Mstart attr"=!al attr'=!al' D=>= M/start
Mلم نم lتكلل (لكلا نمثلا (لI *&-حلا4 €اUلا *اثملا T.@ ب0/@ ا$Iد books.xml
1 ,مIا d load ,ل rexml (لاتلاB
require ?reIml/document?
require ?reIml/streamlistener?
include 6LXPG
2 *ا }F@? d ContentHandler *ا وا Streamer _ P"ةي&\ 70QXم j&ث4Y
*ا K02$A callbacks (E ?د4 اذا =>ثم tag *ا4 ةصاUلا تا@ابلا (E ?د4 وا tag *ا (S$ي وا tag "(لاتلاB
class 9ooksStreamer
include StreamGistener

deF initialiVe
[in<rice=False
[sum=(
end
deF tag;start&tag;name2 attrs,
puts 0Starting CTtag;nameU5
iF &tag;name==?price?, t+en
[in<rice=true
end
end
deF tag;end&tag;name,
Cputs ?Lnding CTtag;nameU?
[in<rice=False
end
deF teIt&data,
iF [in<rice t+en
[sum D= data#to;i
end
end

deF get;total;sum

return [sum
O 86 P
$<= >+ا"لا لصفلا : XML & Ruby

end

end
:لوا tag_start *وا (ه callback *ا امل (Iدت!6 parser (E ?دبي tag *ا jا w':لو ن2م parser *ا (لI 0مA
tag_name m-Zا-bو
Mstart attr"=!al attr'=!al' #### D=>= M/start
deF tag;start&tag;name2 attrs,
iF &tag;name==?price?, t+en

end
end
0G4 <تS@ نل j:لا نح@ )د] price tag *ا ,bد اذ•E parser (E tag <A•4 price ةم&ل2ملا vذه 1W&@ j? دي0@ ا$@•E
9دUت!@ ا$@ا? (هو نZاكلل instance variable *ا (E ا$@ا (لJ 0Fي price tag (لاتلاB
[in<rice=true
"(لJ *&حتتE
deF tag;start&tag;name2 attrs,
Cputs ?Starting CTtag;nameU?
iF &tag;name==?price?, t+en
[in<rice=true
end
end
a• <Aو (لI 0!.ملا 0م اذJ hط$ملا4و ..اد] ,م] price ؟ š
Mstart attr"=!al attr'=!al' D=>= M/start
3E j&كي نل &ه دBX6 ,كبE price tag ,م2$E reset *ا ,ه 0Fي Lذلا 0Gتملل parser (E price tag "(لاتلاB :ل وا
deF tag;end&tag;name,
Cputs ?Lnding CTtag;nameU?
[in<rice=False
end
H&م/ملا (لI *&-حلا (لJ &هو }\ r!4+ل 36X@
1 K0

I d instance variable ’ <A•4 sum ƒ ةمe mطI?و 0
deF initialiVe
[in<rice=False
[sum=(
end
O 87 P
$<= >+ا"لا لصفلا : XML & Ruby
2 *ا ةZ8] (E d data (لJ اSل&' integer ’ *ا (لI اS.W?و sum
Mstart attr"=!al attr'=!al' D=>= M/start
=>ثم (لاتلاB
deF teIt&data,
iF [in<rice t+en
[sum D= data#to;i
end
end
*ا (لI *&-حلل sum ,مIا getter (لاتلاB
deF get;total;sum

return [sum

end
؟اذه ا$.ص 9دUت!@ MB (قبي )اد] ,م]
1 *ا نم ةU!@ ,مIا d BooksStreamer "(لاتلاB
bs=9ooksStreamer#ne.
2 نZاB و رد-ملا Mلم ر0م d BooksStreamer (لJ Document.parse_stream "3لاتلاB
Document#parse;stream&Hile#ne.&?books#Iml?,2 bs,
j&كي اذكه bs (E n6اقبط6 l!' ا8ها] start, end, text (Iدت!6 n@ا &ه nلI ام ,كE get_total_sum
"(لاتلاB
puts bs#get;total;sum
Coutput: ")(
O 88 P
$<= يناثلا لصفلا : Ruby Gems
:@
[\
ى
ن
ا

!
ا ل
ص
فل
ا Ruby Gems
yل mS4اFم 7اد? 5مدUتAا دقE Tك$ل ة-$م Rم 5لما26ا دe 5$B اذا apt-get .تابتكملاasما0بلا ,محتل
*ا 9اi$لا T.$4 gem .,ثم Tل نكل و ةم8حB ,مI ةبتكم و? hبط6 &ه deb . وا rpm . نكل gem (لI Ruby
Platform اد] ,م]
تاMيمملا
1 *ا Kذ'a5بث6 ة@اكما d Gem Packages
2 تايدامتI:لا ةلكFم cاGلJ d
3 98حلا 3E <كحتلا ةل&SA d
4 70E&تملا 98حلا g0Iو Nحبلاa9=>2تA:لا d
5 98حلا cاF@J ةل&SA d
6 *ا4 ةصاUلا hZاQ&لا (لI H=>tإلا ةل&SA d gemS
(يبثتلا
1 ةلاتلا ةلص&لا نم ,محتلا4 <e d http://rubyforge.org/frs/?group_id=126
2 Mلم ,G\ d setup.rb (لاتلاB
ruby setup#rb
lتBا
gem -!
وا
gem `!ersion
Coutput:
(#A#B
.sما@0بلا 5بQ اذS4
*ا g0Iا environment
gem environment
(لاتلاB
C:\Documents and Settings\a+medgem en!ironment
6ubyXems Ln!ironment:
O 89 P
$<= يناثلا لصفلا : Ruby Gems
- JL6SKRN: (#A#B &(#A#B,
- KNS>=GG=>KRN DK6LC>R6:: c:/ruby/lib/ruby/gems/"#$
- XLP <=>1:
- c:/ruby/lib/ruby/gems/"#$
- 6LPR>L SR86CLS:
- +ttp://gems#rubyForge#org
تارايNلا @ها
1 < install
5بثت4 9اقلل gem cا&A ن2م local وا remote
gem install rake
gem install rails
2 < list
*ا g02ل gems
gem list H
*ا g0I gems *ا K0ح4 ?دبت4 (للا F
3 < uninstall
Kذحل gem ة$2م
gem uninstall rails
4 < search
نI Nحبلل gem ام
searc+ Mgem --local
‰ 9ادUتAا local Nحبلا *ا' (E local
searc+ Mgem --remote
‰ 9دUتAا remote Nحبلل remote
O 90 P
$<= يناثلا لصفلا : Ruby Gems
searc+ Mgem --bot+
‰ 9دUتAا both Nحبلل local و remote
5 < help
7دIا!ملا (لI *&-حلل
€&-U4 7دIا!ملا (لI *&-حلل command ن2م
gem +elp Mcommand
*ا ,B g02ل commands
gem +elp commands
9ادUتAا 9ادUتAإلا (لI ةلثما g02ل
gem +elp eIamples
*ا4 ةصاUلا hZاQ&لا Rم ,ما2تلل gems *ا ,G\ gem_server (لاتلاB
gem;ser!er
URLShorter
*ا 0G-6 ةمدb 9دق6 70ثB Reا&م د]&6 url ,ثم tinyurl C*اثملا ,بA (لI
1 5بثت4 <e d shorturl gem (لاتلاB
C:\Documents and Settings\a+medgem install s+orturl
SuccessFully installed s+orturl-(#$#B
Knstalling ri documentation For s+orturl-(#$#B###
Knstalling 6Doc documentation For s+orturl-(#$#B###
2 ,مIا d import (لاتلاB
require @s+orturl@
3 *ا 0G-تل mمدUت!6 8ياI (للا Re&م Vا TE0!لا راتbا d urls ؟
lتBا puts ShortURL.valid_services =>ثم qاتملا ,ك4 ةمZاe nل g02A
puts S+ort86G#!alid;ser!ices
=>ثم
lns
s+ortiFy
makeas+orterlink
Fyad
rubyurl
orV
skinnylink
d%'
s+orl
tinyurl
moourl
O 91 P
$<= يناثلا لصفلا : Ruby Gems
4 (IدتAا d Shorten method (لاتلاB
S+ort86G#s+orten&link2ser!ice,
5 (E 70م نم 0ثBا 9ادUتA=>ل ,4اe j&كي Nح4 ا$4 €اUلا د&كلا lتك@ d class وا =>ثم Function (لاتلاB
require @s+orturl@
require @s+orturl@
deF s+ortiVe&url,
return S+ort86G#s+orten&url2 :tinyurl,
end
puts s+ortiVe&?...#linuIac#org/Forum?,
Coutput: +ttp://tinyurl#com/5rrela
*ا Rم 78ملا 1DتتA ا2بt URLs .اد] ةلي&طلا
FasterCSV
*ا CSV * را-تbا (ه Comma Separated Values (لاتلاB ة@8Uم تا$4 =>ثم
name2 age2 seI
,ثم
a+med2 "$2 male
tina2 "$2 Female
omar2 "$2 male
ayman2 ")2 male
rogina2 '(2 Female
1 *ا 5بQ d fastercsv gem (لاتلاB
C:\Documents and Settings\a+medgem install Fastercs!
SuccessFully installed Fastercs!-"#'#*
Knstalling ri documentation For Fastercs!-"#'#*###
Knstalling 6Doc documentation For Fastercs!-"#'#*###
2 ,مIا d import * fastercsv (لاتلاB
require @Fastercs!@
3 *ا 9دUتAXA ) m]راb وا Mلملا T.@ (E cا&A تا@ابلا دد' d HEREDOC string .70ملا vذه
data =MMd
a+med2 "$2 male
tina2 "$2 Female
omar2 "$2 male
ayman2 ")2 male
rogina2 '(2 Female
d
O 92 P
$<= يناثلا لصفلا : Ruby Gems
*ا (IدتAا parse method (لاتلاB
HasterCSJ#parse&data, do YrecY
name=rec-(/
age =rec-"/
seI =rec-'/
puts ?Name: CTnameU2 SeI: CTseIU2 =ge:CTageU?
end
CRutput:
Name: a+med2 SeI: male2 =ge: "$
Name: tina2 SeI: Female2 =ge: "$
Name: omar2 SeI: male2 =ge: "$
Name: ayman2 SeI: male2 =ge: ")
Name: rogina2 SeI: Female2 =ge: '(
" ردا-ملا
1 d http://rubygems.org a
2 d Practical Ruby Gems
O 93 P
$<= ثلاثلا لصفلا : انا-1لا د=ا'? 2 SQLite/ActiveRecords 3
6 "_
ا
`
ab
ا دc
ا
-
d
:@
[\
'
!
ا

!
ا ل
ص
فل
ا SQLite/ActiveRecords G
(E ةط!4 ة.لb " تابلطتملا SQL
jkلا SQLite ) ةي0ي&ط6 تا;4 7دI 3E 3Wا0تEا ,كF4 د]&6و ) تاGللا <i2م تابتكم نم 3AاA? mب\ c8] 5حبص?
,ثم rails الا'
Mيمي ?ذلا ام SQLite ؟
1 0G-لا </حلا d
2 (SE 9داb (لJ `اتح6 :ل d Serverless Database Engine
3 تادادIإل `اتح6 :ل d Zero-Configurations
SQLite { تاقبطتلا نم اد] 0بB ددI (E ةمدUت!م backend Databse
http://www.sqlite.org/mostdeployed.html
9دUتAا ,DE? ا@ا SQLite اSليو MySQL وا Postgres نكلو hبطتلا l!' (لI دمت2ي اDيا اذهو SQLite will
just cut it C
1 ,م' d
+ttp://...#sqlite#org/do.nload#+tml
2 *ا 5بQ d gem (لاتلاB
gem install sqlite*-ruby
ةبAا$ملا تا'=>-لا4 ا2بt
3 ,مIا d import * sqlite3 (لاتلاB
require @sqlite*@
4 *ا <م-6 d Class (لاتلاB j&كA
require @sqlite*@
class <+onerGite
deF initialiVe&dbname,
[dbname=dbname
[db=SaGite*::Database#ne.&dbname,
[db#results;as;+as+=true Cro.-@username@/
create;table
O 94 P
$<= ثلاثلا لصفلا : انا-1لا د=ا'? 2 SQLite/ActiveRecords 3
end
deF create;table
CCreate table#
begin
sqlstmt=?C6L=>L >=9GL users &id KN>LXL6 <6KP=6: QL: =8>RKNC6LPLN>2
username J=6C1=6&55, 8NKa8L2 p+onenum J=6C1=6&55,,?
[db#eIecute&sqlstmt,
Cputs ?>able Created##?
rescue LIception = e
Cputs ?>able eIists#?

puts e#message
Ce#backtrace
end
end
deF display;user&user,
puts ?----------------------?
id=user-@id@/#to;s
puts ?KD: CTidU?
puts ?Name: CTuser-@username@/U?
puts ?<+one: CTuser-@p+onenum@/U?
puts ?----------------------?
end
deF Find;user&username,
begin
[db#eIecute&?SLGLC> E H6RP users 31L6L username GKQL @CTusernameU7@?, do
YuserY
display;user&user,
end
rescue LIception = e
puts ?Not Found4?
end
end
deF delete;user&username,
begin
delstmt=?DLGL>L H6RP users 31L6L username=@CTusernameU@?
[db#eIecute&delstmt,
puts ?CTusernameU is deleted#?
rescue LIception = e
puts e#message
end
end
deF add;user&username2 p+onenum,
begin
[db#eIecute&?KNSL6> KN>R users &username2 p+onenum, J=G8LS&O2O,?2
O 95 P
$<= ثلاثلا لصفلا : انا-1لا د=ا'? 2 SQLite/ActiveRecords 3
username2 p+onenum,
puts ?CTusernameU is added4?
rescue LIception = e
puts e#message
end
end

deF select;all
selectstmt=?SLGLC> E H6RP users?
begin
[db#eIecute&selectstmt, do YrecY
display;user&rec,
end
rescue LIception = e
puts e#message

end
end

deF delete;all
delstmt=?D6R< >=9GL users?
[db#eIecute&delstmt,
Ccreate ne. one
puts ?>able Dropped4?
create;table
end

deF update;user&username2 ne.p+one,
updatestmt=?8<D=>L users SL> p+onenum=@CTne.p+oneU@ 31L6L
username=@CTusernameU@?
begin
[db#eIecute&updatestmt,
puts ?CTusernameU is updated4?
rescue LIception = e
puts e#message
end
end

end
تا@ا4 7دIاe Rم *ا-6ا (لI *&-حلل SQLite (لاتلا 9دUت!@
SaGite*::Database#ne.&D9N=PL,
*ا (E ا$ل2Eام ,ثم Constructor (لاتلاB
deF initialiVe&dbname,
[dbname=dbname
[db=SaGite*::Database#ne.&dbname,
[db#results;as;+as+=true Cro.-@username@/
create;table
end
}F$$4 instance variable <A•4 db *ا نم SQLite3::Database class (E ة4&لطملا تا@ابلا 7دIاe <Aا mل ر0م@و
O 96 P
$<= ثلاثلا لصفلا : انا-1لا د=ا'? 2 SQLite/ActiveRecords 3
اهcاF@J <تA اهد&]و 9دI *ا' (Eو *ا-6ا ,مI <تA اهد&]و *ا'
9ادUتAا results_as_hash ,ل *&ص&لا ة@اكمإل data (E اS@XB hash *ا <Aا 9ادUتA•4 =>ثم Column
record-@username@/
*ا 9ادUتAا4 تا$بلا mE j8U@ *ود] }F$@ *واح$A *ا-6إلا <تي ام د0/م4 create_table method (لاتلاB ةE02ملا
deF create;table
CCreate table#
begin
sqlstmt=?C6L=>L >=9GL users &id KN>LXL6 <6KP=6: QL: =8>RKNC6LPLN>2
username J=6C1=6&55, 8NKa8L2 p+onenum J=6C1=6&55,,?
[db#eIecute&sqlstmt,
Cputs ?>able Created##?
rescue LIception = e
puts ?>able eIists#?

puts e#message
Ce#backtrace
end
end
*ا Database Connection Object ’ &ه (للا db *ا 9ادUتAا4 *ود/لا (لI 0ماو? ذ.$6 ة@اكما mل execute
method
نم j&كتي *ود/لا اذه 3 columns
1 *ا &ه d id &هو PrimaryKey اZاقل6 داد84 و
2 *ا d username &هو VARCHAR(55 j&كي 9p:لو _ UNIQUE
3 *ا d phonenum &هو VARCHAR(55 نم j&.ل6 j&كي j? نكمي ,ه .. _ 55 نيد24 #ي0ملا ,ها VداI C؟ <eر
" ةي&\ D
*ا ةلم] *ا ذ.$@ *واح@ SQL qدقA vد&]و *ا' (E نكلو د&]&م نكي <ل اذJ *ود/لا cاF@إل raise *
Exception ..د&]&م *ود/لا j•4 ةلاAر Rبط@ j•4 vد\0@ نح@و
hي0t نI <تA 9دUت!م ةEاWاf add_user method (لاتلاB ةE02ملا
deF add;user&username2 p+onenum,
begin
[db#eIecute&?KNSL6> KN>R users &username2 p+onenum, J=G8LS&O2O,?2
username2 p+onenum,
puts ?CTusernameU is added4?
rescue LIception = e
puts e#message
end
end
O 97 P
$<= ثلاثلا لصفلا : انا-1لا د=ا'? 2 SQLite/ActiveRecords 3

ةلمI insert اد] ةط!4
تا0Gتملا ب 9اS.تAإلا تاملI *دبتAا <تA m@X4 w':ل username, phonenum
9دUت!م تا@ا4 g0If
deF display;user&user,
puts ?----------------------?
id=user-@id@/#to;s
puts ?KD: CTidU?
puts ?Name: CTuser-@username@/U?
puts ?<+one: CTuser-@p+onenum@/U?
puts ?----------------------?
end
*ا jا w':ل user نI 7رابI ةقي0طلا vذه vذbXتA Lذلا row تا@ابلا 7دIاe نم
*ا row (لاتلاB K02تم
id Y username Y p+onenum
,B ةمقل ,ص&6 ردق6 column m@XB vا2م ,ما2تلا4 hash *ا cاF@ا ةياد4 (E اذكه ا@دد' ا$@J ام4 Connection
Object 0Bذ6 &ل
[db#results;as;+as+=true Crecord-@username@/
نمدUت!م نI 9=>2تAإلاf
deF Find;user&username,
begin
[db#eIecute&?SLGLC> E H6RP users 31L6L username GKQL @CTusernameU7@?, do
YuserY
display;user&user,
end
rescue LIception = e
puts ?Not Found4?
end
end
ةلم] SELECT/LIKE ؟ nلذB Tل? ةط!4
*ا ,B lل/A ا$ه rows *ا اSE 3تلا username column * m4اFم username
9دUت!م Kذ'f
deF delete;user&username,
begin
delstmt=?DLGL>L H6RP users 31L6L username=@CTusernameU@?
[db#eIecute&delstmt,
puts ?CTusernameU is deleted#?
rescue LIception = e
puts e#message
O 98 P
$<= ثلاثلا لصفلا : انا-1لا د=ا'? 2 SQLite/ActiveRecords 3
end
end
9دUت!م تا@ا4 Nيدح6f
deF update;user&username2 ne.p+one,
updatestmt=?8<D=>L users SL> p+onenum=@CTne.p+oneU@ 31L6L
username=@CTusernameU@?
begin
[db#eIecute&updatestmt,
puts ?CTusernameU is updated4?
rescue LIception = e
puts e#message
end
end
,كلا Kذ'f
deF delete;all
delstmt=?D6R< >=9GL users?
[db#eIecute&delstmt,
Ccreate ne. one
puts ?>able Dropped4?
create;table
end

,م26 n@ا H0A:لا نم m@ا Vر? ا@ا DROP _" mلB *ود/لل
,كلا g0If
deF select;all
selectstmt=?SLGLC> E H6RP users?
begin
[db#eIecute&selectstmt, do YrecY
display;user&rec,
end
rescue LIception = e
puts e#message

end
end
ةلم] select ؟ nلذB Tل? ةط!4
*ا 9ادUتAا &ه nلI ام ,B ا0bا class (لاتلاB =>ثم
deF get;user
print&?8sername: ?,
return gets#c+omp4
end
deF get;p+one
print&?<+one: ?,
O 99 P
$<= ثلاثلا لصفلا : انا-1لا د=ا'? 2 SQLite/ActiveRecords 3
return gets#c+omp4
end
deF entry;point
plite=<+onerGite#ne.&?mydb?,
mnu=MMP
"-=dd 8ser
'-Delete 8ser
*-8pdate
B-Jie. =ll
5-Clear
%-auit
P
.+ile true
puts mnu
opt=gets#c+omp#to;i
iF not &"##%,#includeO&opt,
puts ?8nkno.n Rption?
end
iF opt=="
plite#add;user&get;user2 get;p+one,
elsiF opt=='
plite#delete;user&get;user,
elsiF opt==*
plite#update;user&get;user2 get;p+one,
elsiF opt==B
plite#select;all
elsiF opt==5
plite#delete;all
elsiF opt==%
eIit
end
end

end
entry;point
Chبطتلا nZاS@ا (لI {و0بم ),م]
ActiveRecords
<Aا4 تا@ا4 7دIاe }F@ا phonerdb_development 3لاتلاB *ود] اSEو
O 100 P
$<= ثلاثلا لصفلا : انا-1لا د=ا'? 2 SQLite/ActiveRecords 3

*ا ActiveRecords 9ادUتAا jود4 *ود/لا sلا26 n@X4 nل 1م!6 SQL *ا 9ادUتA•4 نكلو OO
9دUت!6 n@? د0/م4 ا$ه ActiveRecord *ا4 €اUلا *ود/لا Rم ,ما2تلا <تA users hي0t نI Model (Ruby
Class _
؟ ,2.لا نI اذام ),م] 9=>كلا
" ةي&\ نUA ,bاد nلك\ Y D _
1 ,مIا d Import ,ل active record (لاتلاB
require @acti!e;record@
2 hي0t نI *ا-6إلا4 ةصاUلا تادادIإلا rبWا d ActiveRecord::Base.establish_connection (لاتلاB
=cti!e6ecord::9ase#establis+;connection&
:adapter = @mysql@2
:+ost = @local+ost@2
:username = @root@2
:pass.ord = <=SS3R6D;XRLS;1L6L2
:database = @p+onerdb;de!elopment@

,
*ا ا@دد' ا$ه
adapter cا&A m4 ,م2$A Lذلا mysql وا postgres اه0z وا
host 9داUلا jاكم نI 0ب2ي &هو
username و password *ا-6إلا cاF@إل
database اها2م *ا-6إلا <تA (تلا تا@ابلا 7دIاe
ة4اتB :لJ ‚ق$ي :ل )9ام6 Model *ا Rم ,ما2تA Lذلا Table (لاتلاB
CPapping/Jalidating#
class 8ser M =cti!e6ecord::9ase
!alidates;numericality;oF :p+onenum
!alidates;uniqueness;oF :name
!alidates;presence;oF :name
!alidates;presence;oF :p+onenum

end
CrقE
*ا <Aا ني? table C؟
jا ة4ا]إلا <ممه Ruby/ActiveRecords Rم] &ه *ود/لا j? ا&E02ل ةيا.B نBذ User ($2ي users
O 101 P
$<= ثلاثلا لصفلا : انا-1لا د=ا'? 2 SQLite/ActiveRecords 3
Conventions over Configurations
9ادUتAا4 ايودي ا2بt *ود/لا ددح6 j? Rطت!6 set_table_name =>ثم 3لاتلاB
class 8ser M =cti!e6ecord::9ase
set;table;name&?users?,
!alidates;numericality;oF :p+onenum
!alidates;uniqueness;oF :name
!alidates;presence;oF :name
!alidates;presence;oF :p+onenum

end
CC m$I n60بb? Lذلا 0ح!لا K&F@ (لا26 )د]
ة=ا>ا? user
deF add;user&username2 p+one,
user=8ser#ne.
user#name=username
user#p+onenum=p+one
user#sa!e4 Ct+ro.s an eIception#
end
}F$6 n@ا &ه nلI ام ,B object *ا نم User class *ا ا$ه ا$لاثم (E )د&مI ,B 3E mتمe ددح6و name *او
phonenum
,م26و save نم *د4 C insert statement ؟ ,SA? اذه Tل?
3ا6اي- @ر+? user
deF display;user&user,
puts ?--------------------?
puts ?KD: CTuser#id#to;sU?
puts ?Name: CTuser#nameU?
puts ?<+one: CTuser#p+onenumU?
puts ?--------------------?
end
Aحبلا?
deF Find;user&username,
CGimit to "#
user=8ser#Find&:First2 :conditions =-?name=O?2 username/,
Cu may use 8ser#Find;by;name met+od# god bless reFlection :,
O 102 P
$<= ثلاثلا لصفلا : انا-1لا د=ا'? 2 SQLite/ActiveRecords 3
Cuser=8ser#Find;by;name&username,

return user
end
9دUت!6 j? Rطت!6 5@ا )ة.e&ل `اتح@ ا$ه
user=8ser#Find&:First2 :conditions =-?name=O?2 username/,
*ا jا Nح4 mل4اقA ,/A *و? R]0A ا$ه name *ا Vوا!ي username ب&لطملا
ة2Zار 78م 9دUت!6 j? اDي? Rطت!6و
user=8ser#Find;by;name&username,
<Aا j? 5E0I MB column <A•4 name *ا R]ار <ممه ؟ reflection _"
م,BتCم A!,حت?
deF update;user&username2 ne.p+one,
user=Find;user&username,
user#p+onenum=ne.p+one
user#sa!e4
end
*ا (لI ,-حتA object ةلمI 9ادUتA•4 ب&لطملا find اها$بتB 3تلا ةلادلا 9ادUتA•4 find_user ,م26و mلI *د26و
save " *ود/لا (E w.حلا <6و C D
Dلا @ر+?
deF select;all
8ser#Find&:all,TYuserY
yield user
U
end
,-حA نكلو g02لا <تي نل ا$ه yield ,B record (E mمادUتA:ل display_user ةبAا$م 7ر&-4
789?
deF delete;user&username,
user=Find;user&username,
user#destroy
puts ?Deleted4?
end
(لI ,-'ا object *ا (IدتAاو destroy method m4 ةصاUلا
O 103 P
$<= ثلاثلا لصفلا : انا-1لا د=ا'? 2 SQLite/ActiveRecords 3
Dلا 789?
deF delete;all
8ser#delete;all
end
ددح6 j? Rطت!6 conditions _" (Iام/لا Kذحلا ةلمI (E ا2بt
*ا (Eام ,B ,GتAا ا0bا module =>ثم (لاتلاB
deF get;user
print&?8sername: ?,
return gets#c+omp4
end
deF get;p+one
print&?<+one: ?,
return gets#c+omp4
end
deF entry;point
mnu=MMP
"-=dd 8ser
'-Delete 8ser
*-8pdate
B-Jie. =ll
5-Clear
%-auit
P
.+ile true
puts mnu
opt=gets#c+omp#to;i
iF not &"##%,#includeO&opt,
puts ?8nkno.n Rption?
end
iF opt=="
add;user&get;user2 get;p+one,
elsiF opt=='
delete;user&get;user,
elsiF opt==*
update;user&get;user2 get;p+one,
elsiF opt==B
For user in select;all
display;user&user,
end
elsiF opt==5
delete;all
elsiF opt==%
eIit
end
end

O 104 P
$<= ثلاثلا لصفلا : انا-1لا د=ا'? 2 SQLite/ActiveRecords 3
end
entry;point
" ادB T4و Y D _
O 105 P
$<= #با$لا لصفلا : Ruby on Rails
:@
[\
)
*
ا
+
,
ا ل
ص
فل
ا Ruby on Rails
5ت.ل Ruby +لا 70ت.لا (E vابت@إلا lب!4 nلذو 70بB 7ر&-4 70b Rails اS@ا ديد2لا <ل2ي امB (هو Web
Framework *ا ?دبم hبط6 MVC }\ ,BY Cjام+لاو 7cا.كلاو ةtا!بلاو ةI0!لا &ه *و+لا اSEده ..ةلماكتم ةA=>!4
?دبم (Iا06و _؟ nلذB Tل? m]اتح6 DRY (Don't Repeat Yourself CRم/لا را2\ j&كي j? g0ت.ملا نم &هو _
nل 9دق6 scaffolding ,م26 j? Rطت!6 n@ا ($2م4 _ةلbاد 78مY manipulate ,ل models *اY Database Tables
ةAاA+لا تالم2لا *=>b نم _ CRUD *ا دل&6 و views .r!بم 0م? و? رp ةطGW *=>b نم اذه ,Bو <S4 ةصاUلا
crud: (create read update delete)
:ا ن9 @لكت7 8لا4) MVC IيلO
*ا MVC `ذ&م@ &ه pattern Y g02لا *ا ,-.6 mEو تا/م0بلا ةAد$ه (E ا0ثB mلI دمت2ي view <كحتملا نI _
*ا نI MODEL "*اثم4 اSحW&@ (لا2تE nل-6 <ل 70ك.لا j? نكمي )
باتB MDي jX4 =>ثم <كحتملل lلt N2بي 9دUت!ملا
amazon/books/add
domain/controller/action
*ا ا$ه controller *ا &ه books
*او action &ه add
*ا books *ا و? تا@ابلا 7دIاقلل تامل26 3ط2ي Lذلا &ه <!قملا mبFي model 0ثB? نحWاو j&ك$ل
<A•4 *ود] ا@د$I jا g0E (لI books MD6 j? Rطت!6 j? n@ا g0ت.ملا اذه *ود/لا .. تا@ابلا 7دIاe (E
,يد26| ب&لطملا 0م+لا ,-ي N' ),-حي Lذلا rبDلا4 اذكهو )Kذح6 وا د&]&م باتB *د26و ديد] باتB
*ا (لI vذ.$ي &هو <كحتملل |mلاpا ةEاWا model Cةtا!4 ,ك4
yل lلt ,Aر? اذه ا$لاثم (E books controller ب action &ه add * 9دUت!ملا m]&ي m@ا Nح4 view ةط!4
Cتا@ابلا 7دIاe (E vدمت2يو باتكلا تا@ا4 ,bدي jX4 mل 1م!6
O 106 P
$<= #با$لا لصفلا : Ruby on Rails
"0bا *اثم
amazon/books/edit/12
domain/controller/action/param
*ا *اثملا (E ا$ه action &ه edit l'اص باتكلا &ه ة4ا]إلا ؟ باتB Vا نكلو |د&]&م باتكل ,يد26 ,م2$A‡ id
= 12 nل RلطE C view Cباتكلا دمت26و *د26 ردق6 ةط!4
}F$6 n@ا د0/م4 اذه ,B ذ.$6 ردق6 scaffold .9داقلا hبطتلا (E V0تA املثم
" تا i'=>مل ا ‹24
Rails *ا =>ثم اS$م cا8]? 7د2ل <!ق6 Controller *ا Rم ,ما2تلا (لI d ا$لe املثمd 7ردقلا nط2يو actions ة4&لطملا
.ةل&SA ,ك4
(E ActiveRecord نI 7رابI &هو Object/Relational Mapping (ORM *ا ن4 ةطي0Uلا ,ثم ,م24 9&قي _
model و table *ا4 ةصاUلا ا6ادلا mE j8Uي model ,م26 3تلا تا@ابلا 7دIاe ةI&$4 nلا4 ,GF6 نل ($2م4 )
*ا ةtا!4 ,ك4 و m@+ل اSلI ActiveRecord CnلBاFم ,B nل ,حA
*ا Database Migrations Cاه0zو <SZامAا 0G6 وا د&مI ةEاWا وا *ود] ةEاWا =>ثم تا0Gتلا cا0]•4 nلحم!6 "
ةملB model *ا (E اSل }Eاكم تا@ابلا 7دIاe (E *ود] د]&ي m@ا اها$2م ActiveRecord
Pيبط) ةليكيه Rails
O 107 P
$<= #با$لا لصفلا : Ruby on Rails
*ا &ه dالا' ا$مSي Lذلاd اS$م <!e اذكل mلbاد (E <!قي hبطتلا app *ا <D4و controllers *او models *او
views *ا (لJ KاWإلا4 helper classes
*ا config *ا ,مFي Configurations *ا4 ةصاUلا Applications *ا ,ثم database.yml تا@ا4 ددحي اSEو
.تا@ابلا 7دIاقلا H&@و *&bدلا
*ا اSE j&كيو routes *ا ددح6 (تلا mapping *ا ن4 url *او controller
*ا db دل/م ,مFي و migrate *ا j8Uي mEو migrations اSلB
*ا public ,ثم ةما2لا تا]احلا mE j8Uي style sheets, jsS, images,.. etc
7XF@ نI دي8ملا ةE02مل Rails R]ار اSت.!لEو
http://en.wikipedia.org/wiki/Ruby_on_Rails
Rake
* ةS4اFم 7اد? (ه make (E UNIX/UNIX-Like
9اSملا نم اقب!م K02م ددI ذ.$6 ة@اكما nل 9دق6
O 108 P
$<= #با$لا لصفلا : Ruby on Rails
9اSملا (لI H=>t”>ل
http://www.tutorialspoint.com/ruby-on-rails/rails-and-rake.htm
WebRick
&ه HttpServer library ب ب&تكم Ruby
ا$ه mص&-U4 0ثBا تام&ل2م (لI H=>tإلا Rطت!6و
http://www.webrick.org
HolaRails
5بثت4 <e ":لوا rails
gem install rails --include-dependencies
ب ?دب$A 7دا2لاB Hello World
*ا نم commandline *ا وا terminal lتBا
rails +ola6ails
9ادUتA•4 ديد] hبط6 ,م24 ا$مe ا$هو rails 5ماقE rails *ا cاF@•4 skeleton hبط6 ةلكه (E ا$ملك6 املثم hبطتلل
RoR
C:\ruby\r<roSectsrails +ola6ails
create
create app/controllers
create app/+elpers
create app/models
create app/!ie.s/layouts
create conFig/en!ironments
create conFig/initialiVers
create db
create doc
create lib
create lib/tasks
create log
create public/images
create public/Sa!ascripts
create public/styles+eets
create script/perFormance
create script/process
create test/FiItures
create test/Functional
create test/integration
create test/mocks/de!elopment
create test/mocks/test
O 109 P
$<= #با$لا لصفلا : Ruby on Rails
create test/unit
create !endor
create !endor/plugins
create tmp/sessions
create tmp/sockets
create tmp/cac+e
create tmp/pids
create 6akeFile
create 6L=DPL
create app/controllers/application#rb
create app/+elpers/application;+elper#rb
create test/test;+elper#rb
create conFig/database#yml
create conFig/routes#rb
create public/#+taccess
create conFig/initialiVers/inFlections#rb
create conFig/initialiVers/mime;types#rb
create conFig/boot#rb
create conFig/en!ironment#rb
create conFig/en!ironments/production#rb
create conFig/en!ironments/de!elopment#rb
create conFig/en!ironments/test#rb
create script/about
create script/console
create script/destroy
create script/generate
create script/perFormance/benc+marker
create script/perFormance/proFiler
create script/perFormance/request
create script/process/reaper
create script/process/spa.ner
create script/process/inspector
create script/runner
create script/ser!er
create script/plugin
create public/dispatc+#rb
create public/dispatc+#cgi
create public/dispatc+#Fcgi
create public/B(B#+tml
create public/B''#+tml
create public/5((#+tml
create public/indeI#+tml
create public/Fa!icon#ico
create public/robots#tIt
create public/images/rails#png
create public/Sa!ascripts/prototype#Ss
create public/Sa!ascripts/eFFects#Ss
create public/Sa!ascripts/dragdrop#Ss
create public/Sa!ascripts/controls#Ss
create public/Sa!ascripts/application#Ss
create doc/6L=DPL;HR6;=<<
create log/ser!er#log
create log/production#log
create log/de!elopment#log
create log/test#log

O 110 P
$<= #با$لا لصفلا : Ruby on Rails
,G\ ,م] webrick (لاتلاB
ruby script/ser!er
*ا ةx&حلمdd WebRick تر&4 9دUت!A 3000 9دUتA•4 اذه 0z تدد' اذJ *ا' (E :لJ
d p option
" =>ثم (لاتلاB
ruby script/server -p 9000
تر&4 (لI ,م2A اذكه 9000 *ا Rم ا$لb *ا' ,B (لI Default
C:\ruby\r<roSects\+ola6ailsruby script/ser!er
= 9ooting 3L9rick###
= 6ails application started on +ttp://(#(#(#(:*(((
= Ctrl-C to s+utdo.n ser!er; call .it+ --+elp For options
-'(($-(5-"* (":B):('/ KNHR 3L9rick "#*#"
-'(($-(5-"* (":B):('/ KNHR ruby "#$#% &'(()-(A-'B, -i*$%-ms.in*'/
-'(($-(5-"* (":B):('/ KNHR 3L9rick::1>><Ser!erCstart: pid='BB$ port=*(((
jا&$2لا (E lتBاو 1.-تملا 1تEا
local+ost:*(((
hبطتلا lتك@ j? :لJ hبتي <ل ) RZار
1 ,م2$A d Controller (لاتلا Rم ,ما2تلا (E <كحتل ديد]
local+ost/say/+ello
O 111 P
$<= #با$لا لصفلا : Ruby on Rails
ةلاAر ا$ل g026 Hello World =>ثم
و
local+ost/say/goodbye
ةلاAر ا$ل g02ي Good Bye
local+ost/say/time
*ا ا$ل g02ي time
*ا Controller <A•4 ا$ه say mلو action <A•4 hello
9دUتAا script/generate controller (لاتلاB mلI ا$ق.6ا Lذلا ذ.$تل
C:\ruby\r<roSects\+ola6ailsruby script/generate controller say +ello goodbye
time
eIists app/controllers/
eIists app/+elpers/
create app/!ie.s/say
eIists test/Functional/
create app/controllers/say;controller#rb
create test/Functional/say;controller;test#rb
create app/+elpers/say;+elper#rb
create app/!ie.s/say/+ello#+tml#erb
create app/!ie.s/say/goodbye#+tml#erb
create app/!ie.s/say/time#+tml#erb
<A•4 <كحتم XF@? ادB say *ا 5;F@او views ,ك4 ةصاUلا action ,B اSE g02تA Lذلا‡ action .„
Mلم 1تEا app/controllers/say_controller.rb *ا (E Editor Mلم vد/تA nيدل ,D.ملا ruby اد] VداI
" (لاتلاB
class SayController M =pplicationController
deF +ello
end
deF goodbye
end
deF time
end
end
*ا نم Žر&ي <كحتم L? jا w'=>$A ApplicationController class *ا (ه اSي&حي 3تلا %0طلاو Actions ةصاb
.m4
*ا 1تEا .. ب0/@ j? دي0@ ,م] lt Browser lتBاو localhost:3000/say/hello
nل 0SiتA
O 112 P
$<= #با$لا لصفلا : Ruby on Rails
SayC+ello
Hind me in app/!ie.s/say/+ello#+tml#erb
ةلاAر nل ت0Sx اذا error * mS4اFم
no suc+ File to load -- sqlite*
5بث6 9p=>E sqlite3, sqlite3-ruby *ا 5حبصا اS@+ل Default DB (E rails 0ي&طتلا ة;بل اد] ةبAا$م اS@ا lب!4
*ا اذكه action *ا 3E *د2@ j? :لJ hبي <ل )د] ,كF4 ,م2ي 1بص? view 9دUت!ملل 0SiتA 3تلا ةح.-لا وا
Mلملا (لI *دI app/views/say/hello.html.erb
=>ثم (لاتلاB nبAا$ي ام4
M+tml
M+ead
Mtitle1ola4M/title
M/+ead
Mbody
McenterM+"1ello 3orld4M/+"M/center
M/body
M/+tml

*ا 3E *د26 j? :لJ hبي <ل اد] 9ام6 views ,كل action ,ثم اهدي06 3تلا goodbye و time
,مIا edit * app/views/say/goodbye.erb.html
M+tml
M+ead
Mtitle=dios4M/title
M/+ead
Mbody
McenterM+"Xood 9ye4M/+"M/center
M/body
M/+tml
,مIاو save 1ت.6 ب0]و
local+ost:*(((/say/goodbye
O 113 P
$<= #با$لا لصفلا : Ruby on Rails
:لJ hبتي <ل time *ا 0Si@ MB <مممه T4 time ؟
,مIا 9ام6 edit ,ل view 3E app/view/time.erb.html (لاتلاB
M+tml
M+ead
Mtitle>ime Jie.M/title
M/+ead
Mbody
McenterM+"No.: M7=[no.7 M/+"M/center
M/body
M/+tml
CC؟ اذه ام
د0/م html mE نكلو VداI ruby code
M7=[no.7
0Gتملا ةمe 7داIا <تA ا$ه now nم=>B ,2.لا4 )اSE 0Siي 70م *وا vذه ؟=>صا 0Gتملا اذه K0I ني? Cةiحل نكل )
,مIا ..(لاتلاB 0Gتملا K02@ (لا26 )1حص edit ,ل SayController " (لاتلاB
class SayController M =pplicationController
deF +ello
end
deF goodbye
end
deF time
[no.=Date>ime#no.#to;s Cstring repr4
end
end
0Gتملا ا$E0I ا$@ا w':ل now *ا (E time action Vا (E mمدUت!6 j? Rطت!6 ةقي0طلا vذS4و ruby code اديدح6و
*ا ERB

O 114 P
$<= #با$لا لصفلا : Ruby on Rails
ةx&حلمdd ERB * را-تbا Embedded Ruby
:ا 8ل9 ة4ي*, E*Q7 database.yml
L? 9دUت!@ <ل h4ا!لا ا$\اق@ (E Database Engine اS$I 9=>كلا نم د4:ل نكلو )nلذ lلطتي <ل H&W&ملا j+ل
Mلم 1تEا config/database.yml ر0حم VX4
C SaGite !ersion *#I
C gem install sqlite*-ruby &not necessary on RS X Geopard,
de!elopment:
adapter: sqlite*
database: db/de!elopment#sqlite*
timeout: 5(((
C 3arning: >+e database deFined as @test@ .ill be erased and
C re-generated From your de!elopment database .+en you run @rake@#
C Do not set t+is db to t+e same as de!elopment or production#
test:
adapter: sqlite*
database: db/test#sqlite*
timeout: 5(((
production:
adapter: sqlite*
database: db/production#sqlite*
timeout: 5(((
w'=>تA
1 *ا jا d adapter &ه (Wا0تEإلا sqlite3 d 9ادUتA•4 mلد26 j? Rطت!6 v0G6 jا 5بب'? اذا d *ا ددح6و db engine
9ادUتAا (لI *اثم mysql
rails myapp -d mysql
2 `ات@إلا ةلمI ا0bاو رابتbإلاو 0ي&طتلل تا@ا4 دIا&e Ž=>Q د]&6 d
3 ب ب&تكم Mلملا d yml syntax *ا باتB (E ب&لAا &هو data * m4اFم xml žم m$كلو markup language
*ا نم 0S\ا j&كي نكميو xml ن/م0بم 7ا' (E ruby _"
(E ,ه parsers ,ل yml ؟
O 115 P
$<= #با$لا لصفلا : Ruby on Rails
yل Tلو Rبطلا4 دBا ruby تام&ل2ملا نم دي8ملل .. rقE
http://www.yaml.org a
Rails 2 Minutes PhoneBook
نI 7رابI j&كA ا$قبط6 PhoneBook تا@ا4 7دIاe (E ن2م 9دUت!م j&.ل6 <eرو <Aا ,/!6 mE <تي ,ماكتم
:3اب1&تملا
NetBeans 6.1
MySQL Server ,ثم 0bo تا@ا4 7دIاe {0حم 9دUتAا اذاY SQLite *ا ,يد2ت4 <e اه0z وا database.yml ام4
_nبAا$ي
File -> New Project -> Ruby -> Ruby on Rails Application
*ا دد' Project Name ب rPhone =>ثم
*ا (E Development Database ,مIا Create DB <Aإلا اSطI?و rphonebookdb
*ا cا\ jJ <6 اذB Skeleton ا$قبطت4 €اUلا
rGWا R.Click (لI rPhone
راتbا اS$مو Generate
راتbا scaffold *ا <Aا دد'و 7ر&-لا4 امB model ب person
*ا w':ل Attribute Pairs *ا اSE ا$ه name H&$لا نم string و phone_num H&$لا نم integer &ه nلIام ,B
,B ن4 ,-.6 n@ا field *او type " ب
O 116 P
$<= #با$لا لصفلا : Ruby on Rails
*ا cاF@ا <تA ا$ه model (person *ا cاF@ا <تAو _ Controller تالمI ‘ m4 €اUلا CRUD ةAاA+لا
(لاتلاB تا@ابلا 7دIاe }F@ا
R.Click on rPhone-> Run Rake Task -> db -> create
*ا ددح$A jkلا اد] 9ام6 Columns *ا (E (تلا table
Vا ةx&حلم model mل j&كي j? l/ي mلم26 table *ا 1ت.6 jا nلI Lذلا ,كE )mل ,4اقم DB Migrations
اS$م راتbاو (لاتلاB create_people.rb ةلاتلا 7&طUلا (لI ,bدا :ل اذJ .. }\ mE *د26 j? تدر? اذا
,م2@ j? Rطت!@ jkلا migrate 3لاتلاB ن$;مطم نح@و
O 117 P
$<= #با$لا لصفلا : Ruby on Rails
C{و0بم )T4و
C.. ني?و ني?و دا&B+لا ني? ؟ <2@
*ا model *او controller *او views ا$لمI j? د0/م4 تXF@? اSلB Generate * Scaffold 1تEا 3$eد-6 <ل اذا )
*ا controllers folder mE د/تAو people_controller.rb *ا اSمدقي 3تلا ةAاA+لا تالم2لا mEو scaffold
*ا نم اSل4اقيامو views
نتقeد دbا Vذلا hه0ملاو l2تملاو ,ي&طلا ا$/ما@04 ,G\ ا0bا
Run -> Run Main Project (F6)
O 118 P
$<= #با$لا لصفلا : Ruby on Rails
ةEاWإل record rGWا ديد] New person
rGWا create
O 119 P
$<= #با$لا لصفلا : Ruby on Rails
ر0ح6 j? Rطت!6 Edit =>ثم (لاتلاB
rGWاو Update
‘ <Aإلا *&t Vp تا@ابلا 7دIاقل اSلاbدا <تA (تلا تا$بلا ةم=>A jامDل ن@ا&قلا ‹24 RWو &ه ا$لIام ,B ا0bا
<eر j&كي 9p:ل j&.لتلا <eر jا
*ا 3E اذه MD$A ),م] person model
class <erson M =cti!e6ecord::9ase
O 120 P
$<= #با$لا لصفلا : Ruby on Rails
!alidates;lengt+;oF :name2 :.it+in = '##'(
!alidates;lengt+;oF :p+one;num2 :is = "(

!alidates;numericality;oF :p+one;num
!alidates;uniqueness;oF :name

end
ن4 j&كي m@ا Nح4 <Aإلا *&t (لI ‚حE ,م2ي jا v0ب/$A ا$ه 2 و 20 ب j&.لتلا 9اeرا ددI ددح$Aو 0ثكلا4 10
اSلم26 ردق6Y 9اeرا within 9دUتA•4 _<eر (لIاو <eر ,eا ددح6و اSتق4اA ,ثم
validates_length_of
(E mلاbدا <تA Lذلا ,B jا ‚ح.ي j? vدي0@ phone_num 9دUت!$!E rقE 9اeرا j&كي
validates_numericality_of
9دUت!$!E امZاد دي0E 9دUت!ملا <Aا j&كي j? دي0@و
validates_uniqueness_of
*ا ,B vذه Tل m@? w':ل validators lتك6 j? Rطت!6 نكل ..اSمدUت!6 j? Rطت!6 (تلا validator n4 €اb
.اDي?
(لاتلاB V0@و ةملA 0z تا@ا4 ,/A MD@ ب0/@ (لا26 د]
(لاتلا ا$ل 0SiA
O 121 P
$<= #با$لا لصفلا : Ruby on Rails
*ا 9دUت!ي j? lحي Lذلا H&$لا نم 5$B اذا command-line/terminal د0/مو 0ثB text editor j? Rطت!6
*ا R]ا06 commands *او 0E0A lي&لا ,GFت4 ةصاUلا generators ا$ه
http://www.tutorialspoint.com/ruby-on-rails/rails-quick-guide.htm
O 122 P
؟@Aا اذام
؟efا
ا
ذ ا م
CC 70ثB ت:لا/م (E q&ت.م nمام? hي0طلا
,ل m/ت6 j? Rطت!6 GUI *=>b نم wxRuby وا FXRuby
*=>b نم lي&لا 0ي&ط6 و? ROR
(E 70E&تملا ة2Zار نم 0تB+لا تاكبFلا تابتكم *=>b نم تاكب\ ة/م04 و? Ruby
ة4اتB و? Extensions * Ruby *ا *=>b نم C =>ثم
(لJ m/ت6 j? Rطت!6 و? Jruby (هو‡ 100 *ا4 Š Java „
(لا m/ت6 j? Rطت!6 و? IronRuby . *ا 7&e نم اSE د.ت!6و NET
ب ة4&تكم رد-ملا ة'&ت.م RيراFم R4ا6 Ruby (لI RubyForge
ب ةقل2تملا ردا-ملا ,B ruby (E اهد/تA RubyMatters
ا
g
h
i
jk
ا l
mn
Apress Beginning Ruby
Design Patterns in Ruby - Addison Wesley
The Ruby Programming Language
Agile Web Development with Rails
oo
ا
g
p

r
دق
ل
باتكلا نم nZاSت@ا (لI {و0بم
+
Is
0ك\ m]&ت4 9&eا jا l'ا €ا b *
Christina n6دIا!م jود4 (Aاe 5eو (E ‚لUه jاBام v0مI باتكلا "
Squall m4 8تIا 0UE امZاد ال n6د@ا!م "
* ا0ك\ OMLX و ة$قتلا Vداو RZا0لا <هد&S/مو ة2]ا0ملاو h!$تلا (لI
O 123 P
$!:
ا0ك\ SAMI RZار نم 0تB+لا K=>Gلا (لI
نم ,كل ا0ك\
St0rM, BlackRay, sAFA7_eLNeT, Muslim, Binary, Dj.Raidy, GreyHunter, HackoBacko,
MySQL ~
O 124 P

You're Reading a Free Preview

تحميل
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->