You are on page 1of 12

RubyProgramming

Language
RubyScriptinglanguage,dynamicandobjectoriented
Allowsrapiddevelopmentandrapidprototypesofsoftware
Consistsofusefulstringoperationsandregularexpressions
EverythinginRubyisanobject
Hasautomaticgarbagecollection
Instantfeedbackwhendeveloping

MultipleOSsupportandopensource

RubyProgramming
Language
History
BornFeb23,1993YukihiroMatzMatsumoto
Ruby0.95announcedDec21,1995
OO,Inheritance,iterators,exceptionhandling,garbagecollection
Ruby1.0releasedDec25,1996
Ruby1.31999
AfteritsreleaseanEnglishRubymailinglistwascreated

RubyonRails2005
MVCwebframeworkcodedwithRuby

Ruby1.9.1Latestversion

RubyProgramming
Language
Philosophy
Rubyissimpleinappearance,butisverycomplexinside,
justlikeourhumanbodyMatz
POLSPrincipleofLeastSurpriseDesignthelanguagesoexperienced
Programmershaveminimalconfusion.

RubyProgramming
Language
RubyinAction
ToexecuteRubycodeusetheIRB(interativerubyinterpreter)orfroma
Terminaltype:%rubyfilename.rb
ClassicHelloWorldexampleusingirb:
:
c@claptop:~$irb
irb(main):001:0>puts"HelloWorld"
HelloWorld
=>nil
Usingafilenamedhello.rb

c@claptop:~/Documents/cs183$rubyhello.rb
HelloWorld

RubyProgramming
Language
RubyinBasics
Output:
printrubyrocks
putsrubyrocks//insertsaCRandend
Functions:
defwelcome(name)
putshey#{name}
end
:
Functionswithmultiplereturnvalues:
defget_cost_mpg
cost=20

mpg=20
Returncost,mpg
end
Hum_cost,Hum_mpg=get_cost_mpg

RubyProgramming
Language
RubyinBasics
Variables:
Globalsprefixwitha'$'
Instancevarsprefixwitha'@'
Comments:use'#'
SinceeverythinginRubyisanobjectwecanapplymethods:
1.5.floor()=>1
if(x>7&&x<12){...}inRubywecandosomethinglike:
ifx.between?(7,12)do...

RubyProgramming
Language
RubyinBasics
grade=10
school=casegrade
when0..5
"elementary"
when6..8
"middleschool"
when9..12
"highschool"
else
"college"
end
.

ArrayLoopusing'each'
foriin1..4
puts"hi#{i}"
end

animals=%w(lionstigersbears)
animals.each{|kind|printkind}

RubyProgramming
Language
RubyClass
classPerson
definitialize(fname,lname)
@fname=fname
@lname=lname
end
defto_s
"Person:#@fname#@lname"
end
end
person=Person.new("Augustus","Bondi")

printperson

RubyProgramming
Language
RubyFileIO
Performafile>stringoperation:Easyas:
file=File.new("t1.php")
mytext=file.read
Breakdownthefileintoanarrayoflines:
lines=IO.readlines("data.txt")
putslines[0]#printsthefirstline
Outputfilecontents:
IO.foreach("data.txt"){|line|putsline}

RubyProgramming
Language
RubyHTMLExtractorExample

require'openuri'
open('http://www.soe.ucsc.edu/classes/cmps183/Spring10/'){|f|printf.read
BenefitsofRuby:
Cleancode,Easytolearn,Documentionandsupport,OpenSource,OO
DrawbacksofRuby:
Interpreted{Read;Eval;Loop;}Performance<CompiledLanguages

RubyProgramming
Language
Links
http://www.fincher.org/tips/Languages/Ruby/
http://www.rubylang.org/en/about/
http://www.rubylang.org/en/documentation/quickstart/
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29
http://articles.sitepoint.com/article/learnrubyonrails

You might also like