From 3c610b4e8a3eea9277585af091fe223dd85e06ca Mon Sep 17 00:00:00 2001 From: Muhammad Iqbal Shaik Date: Sat, 13 Apr 2024 08:02:35 +0530 Subject: [PATCH] adding for local file access at github --- README.md | 19 ++++++++- app.rb | 19 +++++++++ chapters/chapter_01.md | 18 ++++++++ chapters/chapter_02.md | 25 +++++++++++ chapters/chapter_03.md | 40 ++++++++++++++++++ chapters/chapter_04.md | 46 +++++++++++++++++++++ chapters/chapter_05.md | 55 ++++++++++++++++++++++++ chapters/chapter_06.md | 79 +++++++++++++++++++++++++++++++++++ chapters/chapter_07.md | 94 ++++++++++++++++++++++++++++++++++++++++++ chapters/chapter_08.md | 64 ++++++++++++++++++++++++++++ chapters/chapter_09.md | 63 ++++++++++++++++++++++++++++ chapters/chapter_10.md | 84 +++++++++++++++++++++++++++++++++++++ home.txt | 46 +++++++++++++++++++++ 13 files changed, 651 insertions(+), 1 deletion(-) create mode 100644 app.rb create mode 100644 chapters/chapter_01.md create mode 100644 chapters/chapter_02.md create mode 100644 chapters/chapter_03.md create mode 100644 chapters/chapter_04.md create mode 100644 chapters/chapter_05.md create mode 100644 chapters/chapter_06.md create mode 100644 chapters/chapter_07.md create mode 100644 chapters/chapter_08.md create mode 100644 chapters/chapter_09.md create mode 100644 chapters/chapter_10.md create mode 100644 home.txt diff --git a/README.md b/README.md index 44a5a4b..468fbd7 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,25 @@ Welcome to our comprehensive Ruby programming course. Below you will find links to each chapter that will guide you through the different aspects of Ruby programming. + ## Course Chapters +1. [Introduction to Ruby](./chapters/chapter_01.md) +2. [Setting Up Your Ruby Environment](./chapters/chapter_02.md) +3. [Basic Ruby Syntax and Operations](./chapters/chapter_03.md) +4. [Control Structures and Loops](./chapters/chapter_04.md) +5. [Methods and Return Values](./chapters/chapter_05.md) +6. [Working with Arrays and Hashes](./chapters/chapter_06.md) +7. [Object-Oriented Programming in Ruby](./chapters/chapter_07.md) +8. [Error Handling and Debugging](./chapters/chapter_08.md) +9. [File Input/Output](./chapters/chapter_09.md) +10. [Advanced Ruby Features](./chapters/chapter_10.md) + + +--- + +## Course Chapters for sintra run + 1. [Introduction to Ruby](./chapter01) 2. [Setting Up Your Ruby Environment](./chapter02) 3. [Basic Ruby Syntax and Operations](./chapter03) @@ -13,4 +30,4 @@ Welcome to our comprehensive Ruby programming course. Below you will find links 7. [Object-Oriented Programming in Ruby](./chapter07) 8. [Error Handling and Debugging](./chapter08) 9. [File Input/Output](./chapter09) -10. [Advanced Ruby Features](./chapter10)# ruby_course +10. [Advanced Ruby Features](./chapter10)# ruby_course \ No newline at end of file diff --git a/app.rb b/app.rb new file mode 100644 index 0000000..fdd4ce9 --- /dev/null +++ b/app.rb @@ -0,0 +1,19 @@ +require 'sinatra' +require 'redcarpet' # This is a Markdown to HTML converter + +# Setup Redcarpet (Markdown parser) +markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML) + +# Define routes for each chapter +get '/' do + markdown.render(File.read('README.md')) +end + +(1..10).each do |chapter_number| + formatted_number = format('%02d', chapter_number) + get "/chapter#{formatted_number}" do + puts "chapter formatted number #{formatted_number}" + file_path = "chapters/chapter_0#{chapter_number}.md" + markdown.render(File.read(file_path)) + end +end diff --git a/chapters/chapter_01.md b/chapters/chapter_01.md new file mode 100644 index 0000000..a8d2db5 --- /dev/null +++ b/chapters/chapter_01.md @@ -0,0 +1,18 @@ +# Chapter 1: Introduction to Ruby + +## Overview +In this chapter, we'll explore what Ruby is, its history, and why it's a popular choice among programmers. We'll also briefly discuss Ruby's role in web development and other fields. + +## Why Ruby? +Ruby is known for its elegant syntax that is natural to read and easy to write. Here are a few reasons why Ruby is a favorite among developers: +- Easy to learn +- Highly expressive language +- Large and active community +- Powerful framework support (e.g., Ruby on Rails) + +## A Simple Ruby Program +Here's how you can print "Hello, World!" in Ruby: + +```ruby +puts "Hello, World!" +``` diff --git a/chapters/chapter_02.md b/chapters/chapter_02.md new file mode 100644 index 0000000..53e49c4 --- /dev/null +++ b/chapters/chapter_02.md @@ -0,0 +1,25 @@ +# Chapter 2: Setting Up Your Ruby Environment + +## Overview +This chapter guides you through the process of setting up Ruby on different operating systems, including Windows, macOS, and Linux. + +## Installing Ruby +### Windows +1. Download Ruby Installer from [RubyInstaller](https://rubyinstaller.org/). +2. Follow the on-screen instructions to install. + +### macOS +1. Use Homebrew by running: `brew install ruby` + +### Linux (Ubuntu) +1. Run: `sudo apt-get install ruby-full` + +## Setting Up Your Development Environment +Choose a code editor like VS Code or Atom, and install necessary Ruby plugins or extensions. + +## Testing Your Installation +Run the following command in your terminal or command prompt to check Ruby version: + +```bash +ruby -v +``` diff --git a/chapters/chapter_03.md b/chapters/chapter_03.md new file mode 100644 index 0000000..f1ee2cc --- /dev/null +++ b/chapters/chapter_03.md @@ -0,0 +1,40 @@ + +### `chapter_03.md` (Basic Ruby Syntax and Operations) + + +# Chapter 3: Basic Ruby Syntax and Operations + +## Overview +This chapter introduces the basic syntax and operations in Ruby. + +## Basic Syntax +Ruby has a clean, easy-to-read syntax that makes programming pleasant. Here are some fundamentals: + +### Variables +dfafa + +```ruby +name = "Ruby" +age = 30 +``` + +### Data types + +Ruby automatically determines the type based on the assigned value. + + +### methods +Methods in Ruby are defined using the def keyword. +```ruby +def greet(name) + puts "Hello, #{name}!" +end +``` + +### Arithmetic operations +Ruby supports all standard arithmetic operations. + +```ruby +sum = 1 + 2 # addition +product = 3 * 4 # multiplication +``` diff --git a/chapters/chapter_04.md b/chapters/chapter_04.md new file mode 100644 index 0000000..69237be --- /dev/null +++ b/chapters/chapter_04.md @@ -0,0 +1,46 @@ + +### `chapter_04.md` (Control Structures and Loops) + +# Chapter 4: Control Structures and Loops + +## Overview +Control structures and loops are fundamental in programming. This chapter covers their use in Ruby. + +## Conditional Statements +### If Statement + +```ruby +if temperature > 30 + puts "It's hot outside!" +elsif temperature < 10 + puts "It's cold outside!" +else + puts "The weather is nice!" +end +``` + + +### Unless Statement +```ruby +unless sun.shining? + puts "It might rain today." +end +``` + + +## Loops +### While Loop +```ruby +while counter < 5 do + puts "Counter at #{counter}" + counter += 1 +end +``` + + +### Times Loop +```ruby +5.times do |i| + puts "Iteration #{i}" +end +``` diff --git a/chapters/chapter_05.md b/chapters/chapter_05.md new file mode 100644 index 0000000..0df99bd --- /dev/null +++ b/chapters/chapter_05.md @@ -0,0 +1,55 @@ +# Chapter 5: Methods and Return Values + +## Overview +Methods are crucial for organizing and reusing code in Ruby. This chapter covers how to define methods, pass parameters, handle return values, and best practices for using methods in Ruby programming. + +## Defining Methods +In Ruby, you define a method using the `def` keyword, followed by the method name and any parameters. Here is a simple example: + +```ruby +def greet(name) + puts "Hello, #{name}!" +end +``` + +You can call this method by passing the required argument like this: + +```ruby +greet("Alice") +``` + +## Parameters +Methods can take multiple parameters, separated by commas. Here's an example of a method that takes two parameters: +```ruby +def add(a, b) + puts "The sum is #{a + b}" +end + +add(5, 3) +``` + + +## Default Parameters +You can also define default values for parameters that will be used if no argument is provided for that parameter: + +```ruby +def greet(name, greeting="Hello") + puts "#{greeting}, #{name}!" +end + +greet("Alice") # Outputs: Hello, Alice! +greet("Alice", "Hi") # Outputs: Hi, Alice! +``` + +## Return Values +In Ruby, a method returns the result of the last expression evaluated, but you can also use return to return a value early: + +```ruby +def largest_number(a, b) + return a if a > b + b +end + +puts largest_number(5, 10) + +``` \ No newline at end of file diff --git a/chapters/chapter_06.md b/chapters/chapter_06.md new file mode 100644 index 0000000..704c1b7 --- /dev/null +++ b/chapters/chapter_06.md @@ -0,0 +1,79 @@ +# Chapter 6: Working with Arrays and Hashes + +## Overview +Arrays and hashes are essential data structures in Ruby used to organize and store data. This chapter explores how to create, access, and manipulate these structures effectively. + +## Arrays +Arrays in Ruby are ordered, integer-indexed collections of any object. Here's how to create and manipulate an array: + +### Creating Arrays +```ruby +numbers = [1, 2, 3, 4, 5] +names = ["Alice", "Bob", "Charlie"] +``` + + +### Accessing Elements +Elements in an array are accessed by their index: + +```ruby +puts numbers[0] # Outputs 1 +puts names[2] # Outputs Charlie + +``` + +### Adding Elements +You can add elements to arrays using push or the << operator: + +```ruby +numbers.push(6) +names << "David" +``` + +### Iterating Over Arrays + +Use each to iterate over elements: +```ruby +names.each do |name| + puts "Hello, #{name}!" +end + +``` + +## Hashes +Hashes in Ruby are collections of key-value pairs. They are similar to dictionaries in other languages. + + +### Creating Hashes + +```ruby +ages = { "Alice" => 30, "Bob" => 25, "Charlie" => 35 } + +``` + +### Accessing Values + +Access values in a hash by using their keys: + +```ruby + +puts ages["Alice"] # Outputs 30 + +``` + +### Adding Key-Value Pairs +Add new pairs to hashes like this: + +```ruby +ages["David"] = 28 +``` + +### Iterating Over Hashes +Iterate over key-value pairs using each: + +```ruby +ages.each do |name, age| + puts "#{name} is #{age} years old." +end + +``` \ No newline at end of file diff --git a/chapters/chapter_07.md b/chapters/chapter_07.md new file mode 100644 index 0000000..f23f3d4 --- /dev/null +++ b/chapters/chapter_07.md @@ -0,0 +1,94 @@ +# Chapter 7: Object-Oriented Programming in Ruby + +## Overview +Object-Oriented Programming (OOP) is a programming paradigm that uses "objects" — data structures consisting of data fields and methods together with their interactions — to design applications and computer programs. Ruby is a powerful OOP language, and this chapter introduces the basic concepts of OOP in Ruby, including classes, objects, inheritance, and more. + +## Classes and Objects +In Ruby, classes are blueprints for creating objects (instances of classes). + +### Creating a Class +Here’s how to define a simple class in Ruby: + +```ruby +class Person + def initialize(name) + @name = name # @name is an instance variable + end + + def greet + "Hello, #{@name}!" + end +end +``` + +### Creating Objects + +You can create an object by calling the new method on a class: + +```ruby +alice = Person.new("Alice") +puts alice.greet # Outputs: Hello, Alice! +``` + +## Inheritance +Inheritance allows you to create a new class that is a type of an existing class. + + +### Defining a Subclass +Here's how you can create a subclass in Ruby: + +```ruby +class Employee < Person + def initialize(name, position) + super(name) # Calls the superclass's initialize method + @position = position + end + + def profile + "#{@name} works as a #{@position}." + end +end + + +``` + +### Using Subclasses + +```ruby +bob = Employee.new("Bob", "Developer") +puts bob.profile # Outputs: Bob works as a Developer. +puts bob.greet # Outputs: Hello, Bob! +``` + +## Encapsulation and Polymorphism +Encapsulation is the practice of keeping data (attributes) and the methods that act on the data in one object. Polymorphism is the ability of different classes to respond to the same message (method call) in different ways. + +### Encapsulation + +You hide the object's internal state and require all interaction to be performed through object's methods. + +### Polymorphism Example +```ruby +class Animal + def speak + "Some sound" + end +end + +class Dog < Animal + def speak + "Bark" + end +end + +class Cat < Animal + def speak + "Meow" + end +end + +animals = [Dog.new, Cat.new] +animals.each do |animal| + puts animal.speak # Outputs "Bark" for Dog and "Meow" for Cat +end +``` \ No newline at end of file diff --git a/chapters/chapter_08.md b/chapters/chapter_08.md new file mode 100644 index 0000000..0a577df --- /dev/null +++ b/chapters/chapter_08.md @@ -0,0 +1,64 @@ +# Chapter 8: Error Handling and Debugging + + +## Overview +Effective error handling and debugging are crucial for developing robust Ruby applications. This chapter covers common error types in Ruby, how to handle them gracefully, and techniques to debug your code efficiently. + +## Common Ruby Errors +Understanding common error types can help you quickly diagnose issues in your code. + +### Syntax Errors +Syntax errors occur when the code is not written in a valid Ruby format. + +### Example of a Syntax Error +```ruby +if x > 5 +puts "X is greater than 5" +end +``` + +### Runtime Errors +Runtime errors happen during the execution of the code, such as attempting to divide by zero. + + +### Example of a Runtime Error +```ruby +result = 10 / 0 + +``` + +## Error Handling +Ruby uses the begin, rescue, ensure, and end block to handle errors. + +### Example of Error Handling +```ruby +begin + result = 10 / 0 +rescue ZeroDivisionError + puts "Attempted to divide by zero." +ensure + puts "This code runs no matter what." +end +``` + + +## Debugging Techniques +Identifying and fixing bugs is an essential skill for any programmer. + +### Using `puts` and `p` +Simple debugging can involve placing puts or p statements to output the value of variables at certain points in your application. + +### Using Ruby's Debugger +Ruby includes tools like byebug or debugger which can be included in your code to stop execution and allow you to inspect variables and step through code. + +### Example Using byebug +```ruby +require 'byebug' + +def calculate_value(x, y) + byebug + x / y +end + +calculate_value(10, 0) +``` diff --git a/chapters/chapter_09.md b/chapters/chapter_09.md new file mode 100644 index 0000000..367009c --- /dev/null +++ b/chapters/chapter_09.md @@ -0,0 +1,63 @@ +# Chapter 9: File Input/Output + +## Overview +Working with files is a fundamental aspect of programming that allows you to store data permanently and process large amounts of information. This chapter covers how to handle file input and output operations in Ruby. + +## Reading from Files +Ruby makes reading from files straightforward with several useful methods. + +### Opening and Reading a File +You can open and read a file using the `File.open` method and read its contents in various ways: + +```ruby +File.open('example.txt', 'r') do |file| + while line = file.gets + puts line + end +end +``` + +### Reading the Entire File +To read an entire file at once, you can use File.read: + +```ruby +content = File.read('example.txt') +puts content +``` + +### Writing to Files +Ruby also allows you to write to files in several ways. + +### Writing to a File +You can write to a file by opening it in write mode ('w'). This will create the file if it does not exist or truncate it if it does. + +```ruby +File.open('output.txt', 'w') do |file| + file.puts "Hello, world!" +end + +``` + +### Appending to a file + +If you want to add content to the end of a file without truncating it, open it in append mode ('a'): + +```ruby +File.open('output.txt', 'a') do |file| + file.puts "This is a new line." +end +``` + +### Handling Files with Blocks +Using blocks with file operations ensures that the file is closed properly after the operations are complete. + +### Example with Block +```ruby +File.open('another_example.txt', 'r') do |file| + file.each_line do |line| + puts line + end +end +``` + + diff --git a/chapters/chapter_10.md b/chapters/chapter_10.md new file mode 100644 index 0000000..99a4175 --- /dev/null +++ b/chapters/chapter_10.md @@ -0,0 +1,84 @@ +# Chapter 10: Advanced Ruby Features + +## Overview +As you become more comfortable with the basics of Ruby, you can explore its advanced features to enhance your programming skills and create more sophisticated applications. This chapter introduces modules, mixins, metaprogramming, and other advanced topics. + +## Modules +Modules in Ruby serve as a way to group related methods, classes, and constants. They can be used for namespacing and as mixins to add functionality to classes. + +### Defining a Module +```ruby +module Greetable + def greet + "Hello, I'm #{name}!" + end +end + +## Mixins +Ruby does not support multiple inheritance directly, but mixins can be used to include multiple modules in a class. + + +### Including Modules in Classes +Modules can be included in classes to add their methods as instance methods: + +```ruby +class Person + include Greetable + + attr_accessor :name + + def initialize(name) + @name = name + end +end + +alice = Person.new("Alice") +puts alice.greet + +``` + +### Example of a Mixin +```ruby +module Walkable + def walk + "Walking..." + end +end + +class Robot + include Greetable + include Walkable +end + +robot = Robot.new +robot.name = "Robo" +puts robot.greet +puts robot.walk + +``` + + +## Metaprogramming +Metaprogramming in Ruby allows for the dynamic creation of methods and classes during runtime. + +### Defining Methods Dynamically + +```ruby +class Product + [:name, :brand, :category].each do |attr| + define_method("get_#{attr}") do + instance_variable_get("@#{attr}") + end + + define_method("set_#{attr}") do |value| + instance_variable_set("@#{attr}", value) + end + end +end + +``` + +## Other Advanced Features + Singleton Classes and Methods: Customize behavior for individual objects. + Lazy Evaluation: Use Enumerator::Lazy for handling large datasets efficiently. + Refinements: Safely extend core classes locally without affecting global scope. \ No newline at end of file diff --git a/home.txt b/home.txt new file mode 100644 index 0000000..d5e1fb7 --- /dev/null +++ b/home.txt @@ -0,0 +1,46 @@ +Great! We'll design a course outline for Ruby programming, consisting of ten chapters. Each chapter will be crafted to gradually build up the learner's proficiency in Ruby. Here’s the chapter list: + + Introduction to Ruby + Setting Up the Ruby Environment + Ruby Basics: Variables, Operators, and Comments + Control Structures in Ruby + Methods and Functions in Ruby + Working with Collections: Arrays and Hashes + Object-Oriented Programming in Ruby + Advanced Ruby: Blocks, Procs, and Lambdas + File Handling and I/O in Ruby + Ruby on Rails: An Introduction + + --- +Chapter 1: Introduction to Ruby + +This chapter introduces Ruby, covering its history, key features, and where it stands in the current programming landscape. It explains why Ruby is favored for web development, scripting, and data processing, highlighting its simplicity and elegance. The chapter includes a brief overview of popular Ruby applications and frameworks, like Ruby on Rails, to give context on its practical uses. +Chapter 2: Setting Up Your Ruby Environment + +In this chapter, learners will go through the process of installing Ruby on various operating systems. It provides step-by-step instructions on setting up a Ruby development environment, choosing a code editor, and understanding the Ruby version management with tools like RVM or rbenv. The chapter ends with running a simple "Hello, World!" script to validate the installation. +Chapter 3: Basic Ruby Syntax and Operations + +This chapter covers the foundational elements of Ruby programming. It starts with the basic syntax, data types, and variables. The chapter also explains operators, comments, and how to format Ruby code properly. By the end of the chapter, students will be able to write simple Ruby scripts using basic operations. +Chapter 4: Control Structures and Loops + +Learners will explore how to control the flow of Ruby programs in this chapter. It covers conditionals like if, unless, and case expressions, along with loops such as while, for, and Ruby-specific times loops. Practical examples will help solidify the use of these structures in real-world applications. +Chapter 5: Methods and Return Values + +This chapter dives into defining and using methods in Ruby. It discusses method declaration, parameters, and return values, along with best practices for naming and organizing methods. Several examples show how methods can be used to structure code effectively. +Chapter 6: Working with Arrays and Hashes + +Here, students learn to manipulate collections of data using arrays and hashes. The chapter focuses on creating, accessing, and traversing these structures, along with common methods for manipulating them like map, select, each, and reduce. +Chapter 7: Object-Oriented Programming in Ruby + +This chapter introduces the principles of object-oriented programming (OOP) in the context of Ruby. It covers classes, objects, inheritance, encapsulation, and polymorphism. Practical examples of designing a simple class hierarchy in Ruby are included to demonstrate OOP concepts in action. +Chapter 8: Error Handling and Debugging + +Learners will understand how to handle errors and debug Ruby code in this chapter. It covers the use of exceptions, begin and rescue blocks, and other debugging techniques such as using the Ruby Debugger or puts statements effectively to track down issues in code. +Chapter 9: File Input/Output + +This chapter focuses on reading from and writing to files using Ruby. It covers different methods to handle files, read content, write and append data, and manage file pointers. Practical examples include reading configuration files and logging information to a file. +Chapter 10: Advanced Ruby Features + +The final chapter explores more advanced Ruby features, such as modules, mixins, and metaprogramming. It also touches on using external libraries and gems to extend Ruby's functionality, as well as a brief introduction to building a simple web application with Ruby on Rails. + +With these chapters, the course would provide a comprehensive path from Ruby beginner to a proficient level, ready to take on more complex programming tasks or further specialize in Ruby on Rails. \ No newline at end of file