
Ruby Basics Part 2 — Hashes, Arrays, Iterators, String Manipulation
Last post covered variables, methods, blocks, and classes. Now let's talk about the data structures you'll actually use every day: arrays, hashes, and the iterators that make them powerful. Arrays: Ordered Lists models = [ "gpt-4" , "claude-3" , "gemini" ] models [ 0 ] # => "gpt-4" models [ - 1 ] # => "gemini" models . length # => 3 models . first # => "gpt-4" models . last # => "gemini" Adding and removing: models << "llama-3" # append models . push ( "mistral" ) # same thing models . delete ( "gemini" ) # remove by value models . pop # remove and return last The << shovel operator is idiomatic Ruby. You'll see it everywhere. Hashes: Key-Value Pairs Hashes are Ruby's dictionaries. If you're building AI apps, you'll live in hashes — they map directly to JSON: config = { model: "gpt-4" , temperature: 0.7 , max_tokens: 1000 , stream: true } config [ :model ] # => "gpt-4" config [ :temperature ] # => 0.7 config [ :missing ] # => nil (no error!) Those :model , :temperature things are symbo
Continue reading on Dev.to Tutorial
Opens in a new tab

