Master Ruby Programming with 50 free flashcards. Study using spaced repetition and focus mode for effective learning in Programming.
A Symbol is a lightweight, immutable identifier prefixed with a colon, e.g. :name. Symbols are stored in memory only once, making them more efficient than strings for keys and identifiers.
Example: :status, :id
An Array is an ordered collection created with square brackets or Array.new.
Examples:nums = [1, 2, 3]words = %w[hello world]empty = Array.new(5, 0)
A Hash is a collection of key-value pairs.
Examples:h = { name: "Alice", age: 30 }h = Hash.new(0)
Access values with h[:name].
A block is an anonymous chunk of code enclosed in do...end or curly braces { } that can be passed to a method.
Example:[1,2,3].each { |n| puts n }
Both define blocks, but curly braces { } have higher precedence than do...end.
Convention: use { } for single-line blocks and do...end for multi-line blocks.
A Proc is an object that encapsulates a block of code, allowing it to be stored in a variable and called later.
Example:greet = Proc.new { |name| puts "Hello, #{name}" }greet.call("Alice")
A Lambda is a special type of Proc that enforces the correct number of arguments and returns control to the calling method.
Example:square = ->(x) { x * x }square.call(5) # => 25
Key differences:Argument checking: Lambdas raise an error on wrong argument count; Procs assign nil to missing args.Return behavior: return in a Lambda exits the lambda; return in a Proc exits the enclosing method.
Use the class keyword:class Dog
def initialize(name)
@name = name
end
def bark
"Woof!"
end
end
initialize is the constructor method called automatically when you create a new object with ClassName.new. It is used to set up instance variables.
Example: def initialize(name)
@name = name
end
Instance variables start with @ and belong to a specific object instance. They persist for the lifetime of the object.
Example: @name = "Ruby"
Class variables start with @@ and are shared among all instances of a class and its subclasses.
Example:@@count = 0
Use with caution as subclasses share the same variable.
Flashcards
Flip to reveal
Focus Mode
Spaced repetition
Multiple Choice
Test your knowledge
Type Answer
Active recall
Learn Mode
Multi-round mastery
Match Game
Memory challenge