Skip to content

[Feature]: Add Ruby course #3883

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions courses/Ruby/Overview.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
---
title: Ruby Course Outline
sidebar_label: Course Overview
sidebar_position: 1
description: Ruby Course Outline.
tags: [courses,Ruby,Overview]
---


#### Beginner Level

**1. Introduction to Ruby**
- What is Ruby & History
- Setting up the development environment

**2. Basic Syntax and Structure**
- Ruby syntax basics
- Input and output operations

**3. Control Structures**
- Conditional statements (if, unless, case-when)
- Loops (while, until, for, each)

**4. Functions and Methods**
- Defining methods
- Method arguments and return values

**5. Arrays and Hashes**
- Arrays
- Hashes

**6. Strings**
- String concept

#### Intermediate Level

**1. Advanced OOP Concepts**
- OOPS Overall View

**2. Enumerables and Iterators**
- Enumerables
- Custom iterators with Enumerator

**3. Metaprogramming**
- Introduction to metaprogramming
- Defining methods dynamically

**4. Gems and Bundler**
- Introduction & uses
- Creating and publishing your own gem

**5. Working with APIs**
- Dynamic Method
- Parsing JSON and XML

#### Advanced Level

**1. Advanced Metaprogramming**
- Intro & Concepts
- Defining DSLs (Domain-Specific Languages)

**2. Concurrency and Parallelism**
- Concurrency and Parallelism
- Synchronization techniques

**3. Network Programming**
- Introduction to network programming
- Building simple client-server applications

**4. Advanced File Handling**
- Introduction
- Using memory-mapped files

**5. Ruby on Rails**
- Introduction to Ruby on Rails
- MVC architecture in Rails

8 changes: 8 additions & 0 deletions courses/Ruby/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"label": "Ruby",
"position": 16,
"link": {
"type": "generated-index",
"description": "Learn Ruby programming Language."
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"label": "Advanced File Handling",
"position": 4,
"link": {
"type": "generated-index",
"description": "Learn Advanced File Handling in Ruby."
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
id: lesson-1
title: "Advanced File Handling in Ruby"
sidebar_label: Introduction
sidebar_position: 1
description: "Advanced File Handling in Ruby"
tags: [courses,Advance-level,Introduction]
---

Advanced file handling involves techniques for working efficiently with files, particularly when dealing with large files or when needing to perform high-performance I/O operations.

#### **1. Working with Large Files**

When dealing with large files, it's crucial to process the file in chunks rather than loading the entire file into memory. This approach helps manage memory usage and improves performance.

**Example**: Reading a Large File Line by Line

```ruby
File.open('large_file.txt', 'r') do |file|
while line = file.gets
# Process each line
puts line
end
end
```

**Example**: Writing to a Large File in Chunks

```ruby
File.open('large_file_output.txt', 'w') do |file|
(1..1000000).each do |i|
file.write("Line #{i}\n")
end
end
```

#### **2. File Locking Mechanisms**

File locking is used to prevent multiple processes from accessing a file simultaneously, which can lead to data corruption. Ruby does not have built-in file locking, but you can use system calls or libraries for this purpose.

**Example**: File Locking with `flock`

```ruby
require 'fcntl'

file = File.open('lockfile.txt', 'r+')
file.flock(File::LOCK_EX) # Exclusive lock

# Perform file operations
file.write("Some important data")

file.flock(File::LOCK_UN) # Unlock the file
file.close
```
:::note
`flock` is used for advisory locking, which means it's up to cooperating processes to respect the locks.
:::
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---
id: lesson-2
title: "Using Memory-Mapped Files"
sidebar_label: Memory-Mapped Files
sidebar_position: 2
description: "Using Memory-Mapped Files"
tags: [courses,Advance-level,Introduction]
---


Memory-mapped files allow a file to be mapped into the memory space of a process. This approach can be efficient for large files and can provide a way to access file contents as if they were in memory.

**Example**: Memory-Mapped Files with `mmap`

```ruby
require 'mmap'

File.open('large_file.txt', 'r') do |file|
length = file.size
map = Mmap.new(file.fileno, length, Mmap::ACCESS_READ)

# Access the mapped file
puts map[0..100] # Read the first 100 bytes

map.close
end
```

**Note**: The `mmap` gem provides memory-mapped file capabilities. Install it via `gem install mmap`.

#### **File I/O Performance Optimization**

To optimize file I/O performance, consider using buffering and minimizing the number of I/O operations.

**Example**: Buffered Writing

```ruby
File.open('buffered_output.txt', 'w') do |file|
file.sync = false # Disable auto-flushing

(1..1000000).each do |i|
file.write("Line #{i}\n")
file.flush if i % 1000 == 0 # Manually flush buffer periodically
end
end
```

**Example**: Using `IO#readpartial` for Efficient Reading

```ruby
File.open('large_file.txt', 'r') do |file|
buffer = ""
while file.readpartial(1024, buffer)
# Process the buffer
puts buffer
buffer.clear
end
end
```

:::tip
1. **Avoid Reading Entire Files into Memory**:
- For large files, process data in chunks to avoid high memory usage.

2. **Implement Proper File Locking**:
- Use file locking mechanisms to prevent concurrent access issues, especially in multi-process applications.

3. **Optimize I/O Operations**:
- Use buffering to reduce the number of I/O operations and improve performance. Consider memory-mapped files for large datasets.

4. **Efficient File Processing**:
- Process large files efficiently by reading and writing in chunks. Avoid loading entire files into memory.

5. **File Locking**:
- Use file locking to prevent data corruption in concurrent file access scenarios.

6. **Performance Optimization**:
- Optimize file I/O operations by using techniques such as buffering and memory-mapped files for better performance.
:::
92 changes: 92 additions & 0 deletions courses/Ruby/advanced-level/Advanced-metaprogramming/DSLs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
---
id: lesson-2
title: "Defining DSLs (Domain-Specific Languages)"
sidebar_label: DSLs
sidebar_position: 2
description: "Defining DSLs (Domain-Specific Languages)"
tags: [courses,Advance-level,Introduction]
---


- DSLs are specialized languages tailored to a particular domain. Ruby's flexible syntax makes it easy to define DSLs.

**Example**:
```ruby
class HtmlBuilder
def initialize
@html = ""
end

def method_missing(tag, content = nil, &block)
@html << "<#{tag}>"
@html << content.to_s
@html << block.call if block_given?
@html << "</#{tag}>"
end

def result
@html
end
end

builder = HtmlBuilder.new
builder.html do
builder.body do
builder.h1 "Hello World"
builder.p "This is a paragraph."
end
end

puts builder.result
```

**Output**:
```html
<html><body><h1>Hello World</h1><p>This is a paragraph.</p></body></html>
```

#### Advanced Use of Hooks and Callbacks

- Ruby provides hooks and callbacks that can be used to run code at certain points in an object's lifecycle.

**Example**:
```ruby
class MyClass
def initialize
puts "Initializing..."
end

def self.inherited(subclass)
puts "#{subclass} has been inherited"
end
end

class SubClass < MyClass
end
```

**Output**:
```bash
Initializing...
SubClass has been inherited
```

:::tip
1. **Singleton Methods**:
- Useful for defining methods on individual instances rather than classes. Ideal for adding custom behavior to specific objects.

2. **Dynamic Evaluation**:
- `class_eval` and `instance_eval` allow dynamic changes to classes and objects. Use them for meta-level programming tasks.

3. **Domain-Specific Languages (DSLs)**:
- Ruby’s syntax makes it easy to create DSLs. Leverage this to create expressive, domain-specific interfaces.

4. **Understand Performance Implications**:
- Metaprogramming can impact performance. Use it judiciously and profile if necessary.

5. **Maintain Readability**:
- Advanced metaprogramming can make code harder to understand. Ensure that your code remains readable and maintainable.

6. **Use Metaprogramming for Reusability**:
- Apply metaprogramming techniques to reduce code duplication and increase flexibility.
:::
Loading
Loading