Skip to content

Commit 51c07ce

Browse files
authored
Merge branch 'CodeHarborHub:main' into main
2 parents 8c59a46 + 4e87329 commit 51c07ce

File tree

65 files changed

+6834
-1744
lines changed

Some content is hidden

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

65 files changed

+6834
-1744
lines changed

docs/Ruby/BlocksAndProcs.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
---
2+
id: ruby-blocks-and-procs
3+
title: Blocks And Procs
4+
sidebar_label: Blocks And Procs
5+
sidebar_position: 5
6+
description: Ruby Blocks And Procs
7+
tags: [Ruby,Introduction,oops,Blocks And Procs,programming Language]
8+
---
9+
10+
In Ruby, blocks and Procs are fundamental constructs that allow for flexible and powerful control over the flow of execution and behavior of your code.
11+
12+
### Blocks
13+
14+
Blocks in Ruby are chunks of code that can be passed around like objects. They are typically enclosed within either `do..end` or `{}` and are commonly associated with methods that yield control to them. Here's a basic example:
15+
16+
```ruby
17+
# Using a block with each method
18+
[1, 2, 3].each do |num|
19+
puts num * 2
20+
end
21+
```
22+
23+
In this example:
24+
- `do |num| ... end` defines a block that multiplies each element of the array `[1, 2, 3]` by 2.
25+
- `num` is the parameter passed to the block for each iteration.
26+
27+
Blocks can also be defined with curly braces `{}`:
28+
```ruby
29+
[1, 2, 3].each { |num| puts num * 2 }
30+
```
31+
32+
Blocks are most commonly used with iterator methods like `.each`, `.map`, `.select`, etc., allowing for concise and readable code.
33+
34+
### Procs
35+
36+
A Proc (short for procedure) is an object that holds a block of code and can be stored in a variable, passed to methods, and executed later. They provide a way to package blocks of code into reusable entities.
37+
38+
Here's how you define and use a Proc:
39+
40+
```ruby
41+
# Define a Proc
42+
multiply_proc = Proc.new { |x, y| x * y }
43+
44+
# Call the Proc
45+
puts multiply_proc.call(3, 4) # Outputs: 12
46+
```
47+
48+
In this example:
49+
- `Proc.new { |x, y| x * y }` creates a Proc that multiplies two numbers.
50+
- `multiply_proc.call(3, 4)` invokes the Proc with arguments `3` and `4`.
51+
52+
Procs are particularly useful when you want to store a block of code for later use or when you want to pass behavior as an argument to a method.
53+
54+
### Differences and Usage
55+
56+
- **Blocks** are anonymous and tied directly to method invocations. They are not objects themselves but can be converted to Procs implicitly by using the `&` operator.
57+
58+
- **Procs** are objects and can be manipulated like any other object in Ruby. They are useful for storing blocks of code that need to be executed multiple times or passed around as arguments.
59+
60+
```ruby
61+
# Example of passing a block as a Proc argument
62+
def execute_operation(x, y, operation)
63+
operation.call(x, y)
64+
end
65+
66+
puts execute_operation(5, 3, multiply_proc) # Outputs: 15
67+
```

docs/Ruby/CommunityAndRails.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
id: ruby-community-and-rails
3+
title: Community and Rails
4+
sidebar_label: Community and Rails
5+
sidebar_position: 9
6+
description: Ruby Community and Rails
7+
tags: [Ruby,Introduction,oops,Community and Rails,programming Language]
8+
---
9+
10+
The Ruby on Rails framework, commonly known as Rails, has a vibrant and supportive community that plays a significant role in its development, adoption, and ongoing evolution. Here’s a closer look at the community surrounding Rails and its impact:
11+
12+
### Community Involvement
13+
14+
1. **Open Source Collaboration**: Rails is open source, and its development relies heavily on contributions from a global community of developers. This community contributes code, identifies bugs, suggests improvements, and helps maintain the framework.
15+
16+
2. **Online Forums and Communities**: There are numerous online forums, discussion boards, and communities dedicated to Ruby on Rails. Platforms like Stack Overflow, Reddit (r/rails), and various Slack channels provide spaces for developers to ask questions, share knowledge, and discuss best practices.
17+
18+
3. **Conferences and Meetups**: Rails enthusiasts often gather at conferences and local meetups worldwide. These events serve as venues for networking, learning about new features, discussing challenges, and exploring the future direction of Rails.
19+
20+
4. **Documentation and Tutorials**: The community actively contributes to improving documentation and creating tutorials, guides, and educational resources. This helps newcomers get started with Rails and allows experienced developers to deepen their understanding.
21+
22+
### Support and Learning Resources
23+
24+
1. **Gem Ecosystem**: RubyGems, the package manager for Ruby, hosts thousands of gems (libraries) that extend Rails functionality. Many of these gems are maintained by community members, enhancing Rails’ capabilities in various domains such as authentication, testing, and deployment.
25+
26+
2. **Bloggers and Thought Leaders**: Influential developers and thought leaders within the Rails community regularly publish blog posts, articles, and books. These resources cover advanced topics, best practices, and updates related to Rails and its ecosystem.
27+
28+
3. **Contributions to the Ruby Language**: Since Rails is built on the Ruby programming language, the Ruby community’s contributions also indirectly support Rails development. Ruby itself benefits from a dedicated community that continues to refine and expand the language’s capabilities.
29+
30+
### Impact on Development Practices
31+
32+
1. **Agile Development**: Rails has played a significant role in popularizing agile development practices within web development. Its emphasis on convention over configuration and the DRY (Don't Repeat Yourself) principle encourages efficient and iterative development cycles.
33+
34+
2. **Industry Adoption**: Many startups, tech companies, and enterprises use Rails to build scalable and maintainable web applications. The framework’s reliability, productivity gains, and strong community support contribute to its widespread adoption.
35+
36+
### Evolution and Future Directions
37+
38+
The Rails community continues to evolve the framework, incorporating feedback, addressing security concerns, and adapting to changes in web development practices and technologies. Recent versions have introduced improvements in performance, support for modern JavaScript frameworks, and enhanced developer tooling.
39+

docs/Ruby/DynamicTyping.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
---
2+
id: ruby-dynamic-typing
3+
title: Dynamic Typing
4+
sidebar_label: Dynamic Typing
5+
sidebar_position: 3
6+
description: Ruby Support Dynamic Typing
7+
tags: [Ruby,Introduction,oops,Dynamic Typing,programming Language]
8+
---
9+
10+
Dynamic typing is a programming language feature where the type of a variable is determined at runtime rather than at compile time. In dynamically typed languages like Ruby, Python, JavaScript, and others, you do not need to explicitly declare the type of a variable when you define it. Instead, the type is inferred based on the value assigned to it at runtime.
11+
12+
### Key Aspects of Dynamic Typing:
13+
14+
1. **Type Inference**: Variables acquire their types dynamically based on the value assigned to them. For example, in Ruby:
15+
```ruby
16+
x = 5 # x is inferred to be an integer
17+
y = "Hello" # y is inferred to be a string
18+
```
19+
20+
2. **Flexibility**: Dynamic typing allows variables to hold values of different types at different points in the program's execution. This flexibility can simplify coding and prototyping.
21+
22+
3. **Implicit Type Conversion**: Operations involving variables of different types may implicitly convert types to perform the operation. For example:
23+
```ruby
24+
a = 5
25+
b = "10"
26+
c = a + b.to_i # c will be 15 (integer), after converting "10" to integer
27+
```
28+
29+
4. **Runtime Checks**: Since type checking occurs at runtime, type errors (e.g., adding a string to an integer) may not be caught until the corresponding code is executed. This can lead to runtime errors if not handled properly.
30+
31+
### Benefits:
32+
33+
- **Conciseness**: Dynamic typing often leads to shorter and more readable code since developers do not need to explicitly declare types.
34+
35+
- **Rapid Development**: It facilitates rapid prototyping and iteration, allowing developers to quickly modify and test code without strict type constraints.
36+
37+
- **Flexibility**: Dynamic typing supports a wide range of programming styles and paradigms, making it suitable for various domains and use cases.
38+
39+
### Considerations:
40+
41+
- **Runtime Errors**: Type errors may only be caught during runtime, potentially leading to unexpected behavior if type assumptions are not carefully managed.
42+
43+
- **Documentation**: Due to the lack of explicit type declarations, documentation and comments play a crucial role in conveying expected types and behavior.
44+
45+
### Example in Ruby:
46+
47+
In Ruby, variables are dynamically typed, as shown in the following example:
48+
49+
```ruby
50+
x = 10 # x is an integer
51+
y = "Hello" # y is a string
52+
53+
# Later in the code, we can change the type of the variable
54+
x = "World" # Now x is a string
55+
```
56+
57+
Dynamic typing is a fundamental characteristic of many modern scripting and interpreted languages, offering a balance of flexibility and simplicity while requiring careful management of type-related issues during development and testing.

docs/Ruby/GarbageCollection.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
id: ruby-garbage-collection
3+
title: Garbage Collection
4+
sidebar_label: Garbage Collection
5+
sidebar_position: 6
6+
description: Ruby Garbage Collection
7+
tags: [Ruby,Introduction,oops,Garbage Collection,programming Language]
8+
---
9+
10+
Garbage collection (GC) is a critical automated memory management process found in many modern programming languages, including Ruby. Its primary purpose is to automatically reclaim memory occupied by objects that are no longer in use, thereby making that memory available for future allocations. Here’s a deeper look into how garbage collection works in the context of Ruby:
11+
12+
### Key Concepts:
13+
14+
1. **Automatic Memory Management**: In Ruby, memory allocation and deallocation are handled automatically by the runtime environment. Developers do not need to explicitly allocate or deallocate memory as they might in lower-level languages like C or C++.
15+
16+
2. **Mark-and-Sweep Algorithm**: Ruby's garbage collector uses a mark-and-sweep algorithm. Here’s a simplified breakdown of this process:
17+
18+
- **Mark Phase**: The garbage collector traverses all reachable objects starting from known roots (global variables, local variables, and objects referenced directly or indirectly from these roots). Objects that are reachable are marked as live.
19+
20+
- **Sweep Phase**: Once marking is complete, the garbage collector sweeps through the entire heap (memory space allocated to the Ruby program) and deallocates memory for objects that are not marked as live (i.e., objects that are no longer reachable).
21+
22+
3. **Generational Garbage Collection**: Ruby also employs generational garbage collection, which is optimized for programs where the majority of objects are short-lived. It divides objects into different generations based on their age (how long they have been alive) and collects each generation with different frequencies. This approach aims to improve performance by focusing garbage collection efforts on younger, more frequently allocated objects.
23+
24+
4. **Tuning and Configuration**: While Ruby’s garbage collector generally works well out of the box, it can be tuned and configured for specific performance requirements. This includes adjusting thresholds, heap sizes, and tuning parameters in Ruby implementations like MRI (Matz's Ruby Interpreter) or JRuby.
25+
26+
### Benefits:
27+
28+
- **Ease of Use**: Automatic garbage collection simplifies memory management for developers, reducing the likelihood of memory leaks and segmentation faults common in manual memory management languages.
29+
30+
- **Improved Performance**: Effective garbage collection algorithms can enhance overall program performance by reducing the overhead associated with manual memory management.
31+
32+
- **Scalability**: Garbage collection supports scalability by automatically managing memory as the program scales up in complexity and size.
33+
34+
### Challenges:
35+
36+
- **Performance Overhead**: Garbage collection introduces overhead in terms of CPU cycles and pause times, particularly for large heaps or applications with real-time performance requirements.
37+
38+
- **Tuning Complexity**: While automatic, tuning garbage collection for specific use cases can be complex and require understanding of the underlying algorithms and implementation details.
39+
40+
### Example in Ruby:
41+
42+
```ruby
43+
# Example demonstrating automatic garbage collection in Ruby
44+
def create_objects
45+
1_000_000.times { |i| Object.new } # Creates a million objects
46+
end
47+
48+
# Method to demonstrate garbage collection
49+
def demonstrate_gc
50+
create_objects
51+
GC.start # Manually triggers garbage collection
52+
end
53+
54+
demonstrate_gc # Objects created in create_objects are now eligible for garbage collection
55+
```
56+
57+
In this example:
58+
- The method `create_objects` creates a large number of objects.
59+
- After `create_objects` finishes executing, the objects it created become eligible for garbage collection.
60+
- Calling `GC.start` manually triggers the garbage collection process to reclaim memory occupied by objects that are no longer referenced.
61+

docs/Ruby/Introduction.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
id: ruby-intro
3+
title: Welcome to Ruby
4+
sidebar_label: Welcome
5+
sidebar_position: 1
6+
description: Everything in Ruby is an object
7+
tags: [Ruby,Introduction,oops,programming Language]
8+
---
9+
10+
11+
Ruby is a dynamic, reflective, object-oriented programming language that was designed and developed in the mid-1990s by Yukihiro Matsumoto (often referred to as "Matz"). It combines syntax inspired by Perl with Smalltalk-like object-oriented features and is known for its simplicity and readability.
12+
13+
Key features of Ruby include:
14+
15+
1. **Object-Oriented**: Everything in Ruby is an object, including primitive data types like integers and booleans.
16+
17+
2. **Dynamic Typing**: Ruby uses dynamic typing, which means you don't have to declare the type of a variable when you create it.
18+
19+
3. **Mixins and Inheritance**: Ruby supports both single and multiple inheritance, as well as mixins, which allow classes to inherit features from multiple sources.
20+
21+
4. **Blocks and Procs**: Ruby has a powerful mechanism for handling code blocks, which are chunks of code that can be passed around and executed later.
22+
23+
5. **Garbage Collection**: Ruby has automatic memory management through garbage collection.
24+
25+
6. **Libraries and Gems**: Ruby has a rich ecosystem of libraries and frameworks, with tools like RubyGems for package management.
26+
27+
7. **Metaprogramming**: Ruby allows for extensive metaprogramming, meaning you can write code that writes code.
28+
29+
8. **Community and Rails**: Ruby on Rails, often simply called Rails, is a popular web application framework built on Ruby. It has a large and active community of developers.
30+
31+
Ruby's syntax emphasizes readability and simplicity, aiming to make programming more enjoyable for developers. It has been used to build a wide range of applications, from web applications to system utilities.

docs/Ruby/LibrariesAndGems.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
id: ruby-libraries-and-gems
3+
title: Libraries and Gems
4+
sidebar_label: Libraries and Gems
5+
sidebar_position: 7
6+
description: Ruby Libraries and Gems
7+
tags: [Ruby,Introduction,oops,Libraries and Gems,programming Language]
8+
---
9+
10+
In the context of Ruby and Ruby on Rails, libraries and gems play a crucial role in extending the functionality of the language and framework. Here’s a detailed overview of libraries, gems, and their significance:
11+
12+
### Libraries
13+
14+
**Libraries** in Ruby refer to reusable collections of code that provide specific functionalities. These can be included directly into your Ruby codebase using `require` statements. Ruby itself comes with a standard library that includes modules and classes for various tasks, such as file I/O, networking, and more.
15+
16+
Examples of Ruby standard libraries include `net/http` for HTTP communication, `json` for JSON parsing and generation, `csv` for working with CSV files, and `date` for date and time manipulation.
17+
18+
### Gems
19+
20+
**Gems** are Ruby's package manager system and consist of prepackaged libraries or applications. They are distributed through RubyGems.org, which is the primary repository for Ruby gems. Gems simplify sharing and managing third-party code across Ruby projects. Here are key aspects of gems:
21+
22+
1. **Installation**: Gems are installed using the `gem` command-line tool, which comes bundled with Ruby installations by default. For example, to install the popular Rails framework, you would use:
23+
```
24+
gem install rails
25+
```
26+
27+
2. **Dependencies**: Gems can depend on other gems, which are automatically installed when the main gem is installed. This dependency management simplifies handling complex libraries with multiple requirements.
28+
29+
3. **Versioning**: Gems adhere to Semantic Versioning (SemVer), specifying version constraints to ensure compatibility and manage updates. Version management is crucial for maintaining stability in a project's dependencies.
30+
31+
4. **Gemfile and Bundler**: In Ruby on Rails projects, dependencies are typically managed using a `Gemfile` where gems and their versions are listed. Bundler is used to install and manage these dependencies, ensuring consistent environments across development, testing, and production.
32+
33+
### Usage in Ruby on Rails
34+
35+
In the Ruby on Rails ecosystem, gems are extensively used to add features such as authentication, database management, testing frameworks, and more. Some widely used gems in Rails include:
36+
37+
- **Devise**: A flexible authentication solution for Rails applications.
38+
- **RSpec**: A behavior-driven development (BDD) framework for Ruby.
39+
- **CarrierWave**: Provides file uploads for Rails applications.
40+
- **Capistrano**: Automates deployment tasks.
41+
42+
### Benefits of Gems:
43+
44+
- **Rapid Development**: Gems allow developers to leverage existing solutions for common problems, speeding up development time.
45+
- **Community Contributions**: Gems are contributed by a large community of developers, ensuring continuous updates, bug fixes, and improvements.
46+
- **Modularity**: Using gems promotes a modular architecture, where functionality can be added or removed easily as project requirements evolve.
47+
- **Testing and Security**: Popular gems are often well-tested and audited for security vulnerabilities, providing a reliable foundation for application development.
48+
49+
### Considerations:
50+
51+
- **Dependency Management**: Careful attention must be paid to gem versions and dependencies to avoid conflicts and ensure compatibility across different environments.
52+
- **Maintenance**: Regularly updating gems is important to benefit from new features, bug fixes, and security patches.
53+

0 commit comments

Comments
 (0)