Ampersand txhais li cas hauv Ruby?
Ampersand txhais li cas hauv Ruby?

Video: Ampersand txhais li cas hauv Ruby?

Video: Ampersand txhais li cas hauv Ruby?
Video: Data Science with Python! Filtering Data with pandas 2024, Plaub Hlis Ntuj
Anonim

4. 181. Nws hu ua Safe Navigation Operator. Introduced in Ruby 2.3. 0, nws cia koj hu rau txoj kev onobjects yam tsis muaj kev txhawj xeeb tias cov khoom yuav yog nil (Tsis txhob siv txoj kev anundefined rau nil: NilClass yuam kev), zoo ib yam li sim methodin Rails.

Ib yam li ntawd, lub cim txhais li cas hauv Ruby?

Koj tuaj yeem xav txog: id as lub ntsiab lus lub npe ntawm tus hloov pauv id, thiab id id lub ntsiab lus tus nqi ntawm thevariable. A Cim yog qhov yooj yim tshaj plaws Ruby khoom koj tsim tau. Cov cim muaj txiaj ntsig vim muab cim lub npe hais txog tib yam khoom thoob plaws a Ruby qhov kev pab cuam. Cov cim muaj txiaj ntsig ntau dua li cov hlua.

Tsis tas li ntawd, txoj hnyuv txhais li cas hauv Ruby? Colon hloov pauv hauv Ruby . Colon variable hais txog: abc hom variables koj yuav tau pom nyob rau hauv Ruby . Lawv hu ua Ruby cim. Hauv kev sib piv tooth languages, a Ruby lub cim tsis yog qhov sib txawv vim nws tsis tuaj yeem muab tus nqi.

Ntawm no, & Block hauv Ruby yog dab tsi?

Cov & thaiv yog ib txoj kev xa ib daim Ruby code nyob rau hauv ib txoj kev thiab tom qab ntawd ntsuam xyuas qhov code nyob rau hauv lub Scope ntawm txoj kev ntawd.

Cov txiaj ntsig ua haujlwm li cas hauv Ruby?

Analogy. Thaum koj tsav tsheb, yog koj pom cov tawm los kos npe, koj cia lwm lub tsheb hla dhau ua ntej koj nkag rau hauv txoj kev. Hauv Ruby , cov tawm los lo lus tseem ceeb tawm los kev khiav ntawm kev tswj mus rau lub chaws nyob rau hauv lub block. Yog li, cov lej hauv qhov thaiv yog raug tua thiab kev tua txuas ntxiv tom qab kab uas muaj cov tawm los.

Pom zoo: