Académique Documents
Professionnel Documents
Culture Documents
5 Backtothetitlehelper
Wearenowinapositiontounderstandthefull_titlehelperfromListing4.2,6 whichappearswithcommented
annotationsinListing4.11.
Listing4.11: Anannotatedtitle_helper.
a p p / h e l p e r s / a p p l i c a t i o n _ h e l p e r . r b
module ApplicationHelper
# Returns the full title on a per-page basis.
def full_title(page_title = '')
base_title = "Ruby on Rails Tutorial Sample App"
if page_title.empty?
base_title
else
page_title + " | " + base_title
end
end
end
# Documentation comment
# Method def, optional arg
# Variable assignment
# Boolean test
# Implicit return
# String concatenation
Theseelementsfunctiondefinition(withanoptionalargument),variableassignment,booleantests,controlflow,and
stringconcatenation7 cometogethertomakeacompacthelpermethodforuseinoursitelayout. Thefinalelement
ismodule ApplicationHelper:modulesgiveusawaytopackagetogetherrelatedmethods,whichcanthenbemixed
intoRubyclassesusinginclude. WhenwritingordinaryRuby,youoftenwritemodulesandincludethemexplicitly
yourself,butinthecaseofahelpermoduleRailshandlestheinclusionforus. Theresultisthatthefull_titlemethod
isautomagicallyavailableinallourviews.
4.3 Otherdatastructures
Althoughwebappsareultimatelyaboutstrings,actuallymakingthosestringsrequiresusingotherdatastructuresas
well. Inthissection,welllearnaboutsomeRubydatastructuresimportantforwritingRailsapplications.
4.3.1 Arraysandranges
Anarrayisjustalistofelementsinaparticularorder. WehaventdiscussedarraysyetintheRailsTutorial,but
understandingthemgivesagoodfoundationforunderstandinghashes(Section4.3.3)andforaspectsofRailsdata
modeling(suchasthehas_manyassociationseeninSection2.3.3andcoveredmoreinSection13.1.3).
Sofarwevespentalotoftimeunderstandingstrings,andtheresanaturalwaytogetfromstringstoarraysusing
thesplitmethod:
>> "foo bar
baz".split
=> ["foo", "bar", "baz"]
Theresultofthisoperationisanarrayofthreestrings. Bydefault,splitdividesastringintoanarraybysplittingon
whitespace,butyoucansplitonnearlyanythingelseaswell:
>> "fooxbarxbazx".split('x')
=> ["foo", "bar", "baz"]
Asisconventionalinmostcomputerlanguages,Rubyarraysarezerooffset,whichmeansthatthefirstelementinthe
arrayhasindex0,thesecondhasindex1,andsoon:
>> a = [42, 8, 17]
=> [42, 8, 17]
>> a[0]
=> 42
>> a[1]
=> 8
>> a[2]
=> 17
>> a[-1]
=> 17
WeseeherethatRubyusessquarebracketstoaccessarrayelements. Inadditiontothisbracketnotation,Rubyoffers
synonymsforsomecommonlyaccessedelements:8
>> a
=> [42, 8, 17]
>> a.first
=> 42
>> a.second
=> 8
>> a.last
=> 17
>> a.last == a[-1]
=> true
# Comparison using ==
Thislastlineintroducestheequalitycomparisonoperator==,whichRubyshareswithmanyotherlanguages,alongwith
theassociated!=(notequal),etc.:
>> x = a.length
=> 3
>> x == 3
=> true
>> x == 1
=> false
>> x != 1
=> true
>> x >= 1
=> true
>> x < 1
=> false
Inadditiontolength(seeninthefirstlineabove),arraysrespondtoawealthofothermethods:
>> a
=> [42, 8, 17]
>> a.empty?
=> false
>> a.include?(42)
=> true
>> a.sort
=> [8, 17, 42]
>> a.reverse
=> [17, 8, 42]
>> a.shuffle
=> [17, 42, 8]
>> a
=> [42, 8, 17]
Notethatnoneofthemethodsabovechangesaitself. Tomutatethearray,usethecorrespondingbangmethods(so
calledbecausetheexclamationpointisusuallypronouncedbanginthiscontext):
>> a
=> [42, 8, 17]
>> a.sort!
=> [8, 17, 42]
>> a
=> [8, 17, 42]
Youcanalsoaddtoarrayswiththepushmethodoritsequivalentoperator,< < :
>> a.push(6)
# Pushing 6 onto an array
=> [42, 8, 17, 6]
>> a << 7
# Pushing 7 onto an array
=> [42, 8, 17, 6, 7]
>> a << "foo" << "bar"
# Chaining array pushes
=> [42, 8, 17, 6, 7, "foo", "bar"]
Thislastexampleshowsthatyoucanchainpushestogether,andalsothat,unlikearraysinmanyotherlanguages,Ruby
arrayscancontainamixtureofdifferenttypes(inthiscase,integersandstrings).
Beforewesawsplitconvertastringtoanarray. Wecanalsogotheotherwaywiththejoinmethod:
>> a
=> [42, 8, 17, 6, 7, "foo", "bar"]
>> a.join
# Join on nothing.
=> "4281767foobar"
>> a.join(', ')
# Join on comma-space.
=> "42, 8, 17, 6, 7, foo, bar"
Closelyrelatedtoarraysareranges,whichcanprobablymosteasilybeunderstoodbyconvertingthemtoarraysusing
theto_amethod:
>> 0..9
=> 0..9
>> 0..9.to_a
# Oops, call to_a on 9.
NoMethodError: undefined method `to_a' for 9:Fixnum
>> (0..9).to_a
# Use parentheses to call to_a on the range.
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Though0..9isavalidrange,thesecondexpressionaboveshowsthatweneedtoaddparenthesestocallamethodonit.
Rangesareusefulforpullingoutarrayelements:
>> a = %w[foo bar baz quux]
=> ["foo", "bar", "baz", "quux"]
>> a[0..2]
=> ["foo", "bar", "baz"]
Aparticularlyusefultrickistousetheindex1attheendoftherangetoselecteveryelementfromthestartingpointto
theendofthearraywithoutexplicitlyhavingtousethearrayslength:
>> a = (0..9).to_a
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>> a[2..(a.length-1)]
=> [2, 3, 4, 5, 6, 7, 8, 9]
>> a[2..-1]
=> [2, 3, 4, 5, 6, 7, 8, 9]
Rangesalsoworkwithcharacters:
>> ('a'..'e').to_a
=> ["a", "b", "c", "d", "e"]
Exercises
1.AssignatobetotheresultofsplittingthestringAman,aplan,acanal,Panamaoncommaspace.
2.Assignstothestringresultingfromjoiningaonnothing.
3.Splitsonwhitespaceandrejoinonnothing. UsethepalindrometestfromListing4.10toconfirmthattheresulting
stringsisnotapalindromebythecurrentdefinition. Usingthedowncasemethod,showthats.downcaseisa
palindrome.
4.Whatistheresultofselectingelement7fromtherangeoflettersathroughz? Whataboutthesamerange
reversed? Hint:Inbothcasesyouwillhavetoconverttherangetoanarray.