Koj ua li cas thiaj tsim tau tus hash tshiab hauv Ruby?
Koj ua li cas thiaj tsim tau tus hash tshiab hauv Ruby?

Video: Koj ua li cas thiaj tsim tau tus hash tshiab hauv Ruby?

Video: Koj ua li cas thiaj tsim tau tus hash tshiab hauv Ruby?
Video: Koj ua neej li cas lawm Kablia Vwj Nkauj tawm tshiab 2019 2024, Tej zaum
Anonim

Tsim a Hash

Hauv Ruby koj ua tau tsim a Hash los ntawm kev muab tus yuam sij rau tus nqi nrog =>, cais cov yuam sij / tus nqi no nrog commas, thiab kaw tag nrho cov khoom nrog curly braces.

Kuj tseem paub yog, dab tsi yog hash hauv Ruby?

Ruby | Hashes Cov hauv paus. Hash yog cov qauv ntaub ntawv uas tuav ib pawg ntawm cov khoom uas raug hu ua tus yuam sij thiab txhua tus neeg koom nrog tus nqi nrog nws. Hauv cov lus yooj yooj yim, a hash yog ib phau ntawm cov yuam sij tshwj xeeb thiab lawv cov txiaj ntsig. Arrays ib txwm siv tus nqi sib npaug rau indexing whereas hashs siv qhov khoom.

Tsis tas li, koj yuav tshem tawm tus nqi tseem ceeb ntawm tus hash hauv Ruby li cas? Muaj ntau txoj hauv kev los tshem tawm tus yuam sij los ntawm hash thiab tau txais cov hash ntxiv hauv Ruby.

  1. hlais => Nws yuav rov qab xaiv cov yuam sij thiab tsis rho tawm ntawm tus thawj hash.
  2. rho tawm => Nws yuav rho tawm cov yuam sij xaiv los ntawm tus thawj hash (nws tuaj yeem txais tsuas yog ib tus yuam sij thiab tsis ntau tshaj ib qho).

Hais txog qhov no, koj yuav ua li cas ntxiv hash?

Rau ntxiv yuam sij thiab qhov tseem ceeb rau a hash rooj, siv hom lus hais hauv qab no. Piv txwv li, rau ntxiv ib tug "Lub Sijhawm" yuam sij nrog tus nqi ntawm "Tam sim no" rau lub hash lub rooj, siv hom ntawv hauv qab no. Piv txwv li, rau ntxiv ib tug "Lub Sijhawm" yuam sij nrog tus nqi ntawm "Tam sim no" rau lub hash lub rooj, siv hom ntawv hauv qab no.

Puas yog Ruby hashes xaj?

Hauv Ruby 1.9 Txawm li cas los xij, hash cov ntsiab lus yog iterated nyob rau hauv lawv insertion xaj , […] Yog li, nyob ntawm seb koj txhais cov kab ntawd los ntawm phau ntawv li cas thiab nyob ntawm seb "kev qhia tshwj xeeb-ish" koj txiav txim siab li cas phau ntawv, yog, xaj ntawm literals yog guaranteed.

Pom zoo: