Skip to content

Commit 4a2db7a

Browse files
authored
Merge pull request #3883 from sivaprasath2004/sivaprasath-closes-issue-3836
[Feature]: Add Ruby course
2 parents 67032ac + 32999be commit 4a2db7a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+2754
-0
lines changed

courses/Ruby/Overview.md

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
---
2+
title: Ruby Course Outline
3+
sidebar_label: Course Overview
4+
sidebar_position: 1
5+
description: Ruby Course Outline.
6+
tags: [courses,Ruby,Overview]
7+
---
8+
9+
10+
#### Beginner Level
11+
12+
**1. Introduction to Ruby**
13+
- What is Ruby & History
14+
- Setting up the development environment
15+
16+
**2. Basic Syntax and Structure**
17+
- Ruby syntax basics
18+
- Input and output operations
19+
20+
**3. Control Structures**
21+
- Conditional statements (if, unless, case-when)
22+
- Loops (while, until, for, each)
23+
24+
**4. Functions and Methods**
25+
- Defining methods
26+
- Method arguments and return values
27+
28+
**5. Arrays and Hashes**
29+
- Arrays
30+
- Hashes
31+
32+
**6. Strings**
33+
- String concept
34+
35+
#### Intermediate Level
36+
37+
**1. Advanced OOP Concepts**
38+
- OOPS Overall View
39+
40+
**2. Enumerables and Iterators**
41+
- Enumerables
42+
- Custom iterators with Enumerator
43+
44+
**3. Metaprogramming**
45+
- Introduction to metaprogramming
46+
- Defining methods dynamically
47+
48+
**4. Gems and Bundler**
49+
- Introduction & uses
50+
- Creating and publishing your own gem
51+
52+
**5. Working with APIs**
53+
- Dynamic Method
54+
- Parsing JSON and XML
55+
56+
#### Advanced Level
57+
58+
**1. Advanced Metaprogramming**
59+
- Intro & Concepts
60+
- Defining DSLs (Domain-Specific Languages)
61+
62+
**2. Concurrency and Parallelism**
63+
- Concurrency and Parallelism
64+
- Synchronization techniques
65+
66+
**3. Network Programming**
67+
- Introduction to network programming
68+
- Building simple client-server applications
69+
70+
**4. Advanced File Handling**
71+
- Introduction
72+
- Using memory-mapped files
73+
74+
**5. Ruby on Rails**
75+
- Introduction to Ruby on Rails
76+
- MVC architecture in Rails
77+

courses/Ruby/_category_.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"label": "Ruby",
3+
"position": 16,
4+
"link": {
5+
"type": "generated-index",
6+
"description": "Learn Ruby programming Language."
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"label": "Advanced File Handling",
3+
"position": 4,
4+
"link": {
5+
"type": "generated-index",
6+
"description": "Learn Advanced File Handling in Ruby."
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
---
2+
id: lesson-1
3+
title: "Advanced File Handling in Ruby"
4+
sidebar_label: Introduction
5+
sidebar_position: 1
6+
description: "Advanced File Handling in Ruby"
7+
tags: [courses,Advance-level,Introduction]
8+
---
9+
10+
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.
11+
12+
#### **1. Working with Large Files**
13+
14+
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.
15+
16+
**Example**: Reading a Large File Line by Line
17+
18+
```ruby
19+
File.open('large_file.txt', 'r') do |file|
20+
while line = file.gets
21+
# Process each line
22+
puts line
23+
end
24+
end
25+
```
26+
27+
**Example**: Writing to a Large File in Chunks
28+
29+
```ruby
30+
File.open('large_file_output.txt', 'w') do |file|
31+
(1..1000000).each do |i|
32+
file.write("Line #{i}\n")
33+
end
34+
end
35+
```
36+
37+
#### **2. File Locking Mechanisms**
38+
39+
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.
40+
41+
**Example**: File Locking with `flock`
42+
43+
```ruby
44+
require 'fcntl'
45+
46+
file = File.open('lockfile.txt', 'r+')
47+
file.flock(File::LOCK_EX) # Exclusive lock
48+
49+
# Perform file operations
50+
file.write("Some important data")
51+
52+
file.flock(File::LOCK_UN) # Unlock the file
53+
file.close
54+
```
55+
:::note
56+
`flock` is used for advisory locking, which means it's up to cooperating processes to respect the locks.
57+
:::
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
---
2+
id: lesson-2
3+
title: "Using Memory-Mapped Files"
4+
sidebar_label: Memory-Mapped Files
5+
sidebar_position: 2
6+
description: "Using Memory-Mapped Files"
7+
tags: [courses,Advance-level,Introduction]
8+
---
9+
10+
11+
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.
12+
13+
**Example**: Memory-Mapped Files with `mmap`
14+
15+
```ruby
16+
require 'mmap'
17+
18+
File.open('large_file.txt', 'r') do |file|
19+
length = file.size
20+
map = Mmap.new(file.fileno, length, Mmap::ACCESS_READ)
21+
22+
# Access the mapped file
23+
puts map[0..100] # Read the first 100 bytes
24+
25+
map.close
26+
end
27+
```
28+
29+
**Note**: The `mmap` gem provides memory-mapped file capabilities. Install it via `gem install mmap`.
30+
31+
#### **File I/O Performance Optimization**
32+
33+
To optimize file I/O performance, consider using buffering and minimizing the number of I/O operations.
34+
35+
**Example**: Buffered Writing
36+
37+
```ruby
38+
File.open('buffered_output.txt', 'w') do |file|
39+
file.sync = false # Disable auto-flushing
40+
41+
(1..1000000).each do |i|
42+
file.write("Line #{i}\n")
43+
file.flush if i % 1000 == 0 # Manually flush buffer periodically
44+
end
45+
end
46+
```
47+
48+
**Example**: Using `IO#readpartial` for Efficient Reading
49+
50+
```ruby
51+
File.open('large_file.txt', 'r') do |file|
52+
buffer = ""
53+
while file.readpartial(1024, buffer)
54+
# Process the buffer
55+
puts buffer
56+
buffer.clear
57+
end
58+
end
59+
```
60+
61+
:::tip
62+
1. **Avoid Reading Entire Files into Memory**:
63+
- For large files, process data in chunks to avoid high memory usage.
64+
65+
2. **Implement Proper File Locking**:
66+
- Use file locking mechanisms to prevent concurrent access issues, especially in multi-process applications.
67+
68+
3. **Optimize I/O Operations**:
69+
- Use buffering to reduce the number of I/O operations and improve performance. Consider memory-mapped files for large datasets.
70+
71+
4. **Efficient File Processing**:
72+
- Process large files efficiently by reading and writing in chunks. Avoid loading entire files into memory.
73+
74+
5. **File Locking**:
75+
- Use file locking to prevent data corruption in concurrent file access scenarios.
76+
77+
6. **Performance Optimization**:
78+
- Optimize file I/O operations by using techniques such as buffering and memory-mapped files for better performance.
79+
:::
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
---
2+
id: lesson-2
3+
title: "Defining DSLs (Domain-Specific Languages)"
4+
sidebar_label: DSLs
5+
sidebar_position: 2
6+
description: "Defining DSLs (Domain-Specific Languages)"
7+
tags: [courses,Advance-level,Introduction]
8+
---
9+
10+
11+
- DSLs are specialized languages tailored to a particular domain. Ruby's flexible syntax makes it easy to define DSLs.
12+
13+
**Example**:
14+
```ruby
15+
class HtmlBuilder
16+
def initialize
17+
@html = ""
18+
end
19+
20+
def method_missing(tag, content = nil, &block)
21+
@html << "<#{tag}>"
22+
@html << content.to_s
23+
@html << block.call if block_given?
24+
@html << "</#{tag}>"
25+
end
26+
27+
def result
28+
@html
29+
end
30+
end
31+
32+
builder = HtmlBuilder.new
33+
builder.html do
34+
builder.body do
35+
builder.h1 "Hello World"
36+
builder.p "This is a paragraph."
37+
end
38+
end
39+
40+
puts builder.result
41+
```
42+
43+
**Output**:
44+
```html
45+
<html><body><h1>Hello World</h1><p>This is a paragraph.</p></body></html>
46+
```
47+
48+
#### Advanced Use of Hooks and Callbacks
49+
50+
- Ruby provides hooks and callbacks that can be used to run code at certain points in an object's lifecycle.
51+
52+
**Example**:
53+
```ruby
54+
class MyClass
55+
def initialize
56+
puts "Initializing..."
57+
end
58+
59+
def self.inherited(subclass)
60+
puts "#{subclass} has been inherited"
61+
end
62+
end
63+
64+
class SubClass < MyClass
65+
end
66+
```
67+
68+
**Output**:
69+
```bash
70+
Initializing...
71+
SubClass has been inherited
72+
```
73+
74+
:::tip
75+
1. **Singleton Methods**:
76+
- Useful for defining methods on individual instances rather than classes. Ideal for adding custom behavior to specific objects.
77+
78+
2. **Dynamic Evaluation**:
79+
- `class_eval` and `instance_eval` allow dynamic changes to classes and objects. Use them for meta-level programming tasks.
80+
81+
3. **Domain-Specific Languages (DSLs)**:
82+
- Ruby’s syntax makes it easy to create DSLs. Leverage this to create expressive, domain-specific interfaces.
83+
84+
4. **Understand Performance Implications**:
85+
- Metaprogramming can impact performance. Use it judiciously and profile if necessary.
86+
87+
5. **Maintain Readability**:
88+
- Advanced metaprogramming can make code harder to understand. Ensure that your code remains readable and maintainable.
89+
90+
6. **Use Metaprogramming for Reusability**:
91+
- Apply metaprogramming techniques to reduce code duplication and increase flexibility.
92+
:::

0 commit comments

Comments
 (0)