Ruby Tutorial
Ruby CGI
Ruby Advanced
The case
statement in Ruby is used for more complex conditional flows where you might be used to using if...elsif...else
. It's a way to replace multiple if
statements and make your code more readable.
Here's a simple example of a case
statement:
value = 2 case value when 1 puts "One" when 2 puts "Two" when 3 puts "Three" else puts "Something else" end
In this example, the case
statement checks the value of value
. Depending on the value, it executes a different when
clause. If none of the when
clauses match, it executes the else
clause.
You can use the case
statement with any kind of object, not just numbers. For example:
class Car attr_accessor :brand def initialize(brand) @brand = brand end def brand_type case @brand when "Toyota" "Japanese Car" when "Ford" "American Car" when "Ferrari" "Italian Car" else "Unknown Car" end end end car = Car.new("Ferrari") puts car.brand_type # outputs "Italian Car" car.brand = "Ford" puts car.brand_type # outputs "American Car"
In this example, the case
statement is used to determine the type of car based on the brand.
The case
statement is a powerful tool in Ruby, and it can make your code more readable when you're dealing with complex conditional logic. It can be used in any context, not just within classes, and with any type of object, not just strings or numbers.
Ruby class definition:
class
keyword. It encapsulates data and behavior into a single unit.class Person def initialize(name, age) @name = name @age = age end def display_info puts "Name: #{@name}, Age: #{@age}" end end # Object instantiation person = Person.new("John", 25) person.display_info
Ruby class inheritance:
class Animal def speak puts "Animal speaks" end end class Dog < Animal def bark puts "Dog barks" end end # Object instantiation dog = Dog.new dog.speak # Inherited method dog.bark # Class-specific method
Ruby class methods:
self
keyword and can be called on the class itself.class MathOperations def self.square(number) number * number end end # Using the class method result = MathOperations.square(5)
Ruby case statement in class:
class GradeClassifier def classify(grade) case grade when 'A' puts "Excellent" when 'B', 'C' puts "Good" else puts "Needs improvement" end end end # Using the class method classifier = GradeClassifier.new classifier.classify('B')
Ruby switch case in class:
case
statement is similar to a switch statement in other languages.class TrafficLight def signal(color) case color when 'red' puts "Stop" when 'green' puts "Go" when 'yellow' puts "Proceed with caution" else puts "Invalid color" end end end # Using the class method traffic_light = TrafficLight.new traffic_light.signal('green')
Ruby class example:
class Car attr_accessor :make, :model def initialize(make, model) @make = make @model = model end def display_info puts "Make: #{@make}, Model: #{@model}" end end # Object instantiation car = Car.new("Toyota", "Camry") car.display_info
Ruby class variables:
@@
.class Counter @@count = 0 def increment @@count += 1 end def display_count puts "Count: #{@@count}" end end # Object instantiation counter1 = Counter.new counter2 = Counter.new counter1.increment counter2.increment counter1.display_count # Outputs "Count: 2"
Ruby class instance variables:
@
.class Person def initialize(name) @name = name end def display_name puts "Name: #{@name}" end end # Object instantiation person = Person.new("Alice") person.display_name
Ruby class access modifiers:
public
, private
, protected
) control the visibility of methods in a class.class BankAccount def deposit(amount) # Public method end private def withdraw(amount) # Private method end end
Ruby class constructor:
initialize
, is called when a new instance of a class is created.class Book def initialize(title, author) @title = title @author = author end end # Object instantiation with constructor parameters book = Book.new("Ruby Programming", "John Doe")
Ruby class initialization:
initialize
method).class Circle def initialize(radius) @radius = radius end end # Object instantiation with initialization circle = Circle.new(5)
Ruby class constants:
class Constants PI = 3.14159 def circle_area(radius) PI * radius**2 end end # Using the class constant area = Constants.new.circle_area(3)
Ruby class attributes:
class Product attr_accessor :name, :price def initialize(name, price) @name = name @price = price end end # Object instantiation with attribute assignment product = Product.new("Laptop", 999) puts "Product: #{product.name}, Price: $#{product.price}"
Ruby case-insensitive class comparison:
casecmp
.class StringComparison def compare_strings(str1, str2) result = str1.casecmp(str2) if result.zero? puts "Strings are equal" else puts "Strings are not equal" end end end # Using the class method comparer = StringComparison.new comparer.compare_strings("Ruby", "ruby") # Outputs "Strings are equal"