best ruby cheat sheet

Ruby Cheat Sheet:

  1. Variables: In Ruby, variables are used to store values. They are declared using the syntax: variable_name = value. For example, x = 10.

  2. Data Types: Ruby has several built-in data types, including Integer (whole numbers), Float (decimal numbers), String (text), Boolean (true/false), and Array (an ordered list of values). Example: x = 10, y = 3.14, name = "John", is_true = true, numbers = [1, 2, 3].

  3. Operators: Ruby supports various operators such as arithmetic operators (+, -, , /), comparison operators (==, !=, >, <, >=, <=), logical operators (&&, ||, !), and assignment operators (=, +=, -=, =, /=). Example: x + y, x > y, !is_true.

  4. Control Structures: Ruby provides control structures like if-else statements, loops, and switch statements. They help control the flow of the program based on conditions. Example: if condition, while condition, case variable_name.

  5. Methods: Methods are reusable blocks of code in Ruby. They are defined using the syntax: def method_name(parameters) ... end. Example: def say_hello(name) ... end.

  6. Classes and Objects: Ruby is an object-oriented language, and classes are used to define objects. Objects encapsulate data and behavior. Example: class Person, p = Person.new.

  7. Iterators: Iterators are used to loop over collections or execute a block of code repeatedly. Ruby provides iterators like each, map, and select. Example: numbers.each do |num| ... end.

  8. Exception Handling: Ruby allows you to handle and manage exceptions using the begin-rescue-end block. This helps prevent program crashes. Example: begin ... rescue ... end.

  9. File Operations: Ruby provides methods to read from and write to files. You can use File.open, File.read, and File.write methods for file operations. Example: File.open('filename.txt', 'w') do |file| ... end.

  10. Regular Expressions: Ruby supports regular expressions for pattern matching and manipulating strings. Example: /pattern/.match(string).

Remember to refer to the official Ruby documentation for detailed explanations and examples of these concepts.