Ruby types, symbols, strings, arrays, hashes, blocks, procs, classes, modules and mixins.
39 cards · basic cards · AI-written, checked twice. Edit anything.
- What are the main primitive data types in Ruby?
Show answer
Integer, Float, String, Symbol, Boolean (true/false), and nil
- How do you create a symbol in Ruby?
Show answer
Precede an identifier with a colon: :symbol_name
- What is the key difference between a symbol and a string?
Show answer
Symbols are immutable and reused in memory; strings are mutable and create new objects each time
- How do you interpolate a variable into a double-quoted string in Ruby?
Show answer
Wrap the variable in #{}: "Hello, #{name}"
- What does the .to_s method do?
Show answer
Converts an object to its string representation
- How do you create an array in Ruby?
Show answer
Use square brackets: [1, 2, 3] or call Array.new
- How do you access the first element of an array?
Show answer
Use index 0 or call .first: array[0] or array.first
- How do you access the last element of an array?
Show answer
Use index -1 or call .last: array[-1] or array.last
- How do you add an element to the end of an array?
Show answer
Use .push or the << operator: array.push(x) or array << x
- What does the .pop method do to an array?
Show answer
Removes and returns the last element
- What is the key difference between an array and a hash?
Show answer
Arrays use numeric indices (0, 1, 2, ...); hashes use any object as a key
- How do you create a hash in Ruby?
Show answer
Use curly braces with key-value pairs: {key: value} or {"key" => value}
- How do you access a value in a hash?
Show answer
Use square brackets with the key: hash[:key]
- Are symbols commonly used as hash keys?
Show answer
Yes, symbols are the standard choice: {name: "John", age: 30}
- What does the .each method do?
Show answer
Iterates over each element in a collection and runs a block for each