jutsu-ruby

Advanced Ruby skills for object-oriented programming, metaprogramming, blocks, and gems.

View on GitHub
Author The Bushido Collective
Namespace @TheBushidoCollective/han
Category Technique
Version 1.0.0
Stars 66
Downloads 5
self.md verified
Table of content

Advanced Ruby skills for object-oriented programming, metaprogramming, blocks, and gems.

Installation

npx claude-plugins install @TheBushidoCollective/han/jutsu-ruby

Contents

Folders: scripts, skills

Files: CHANGELOG.md, README.md, han-plugin.yml

Documentation

Advanced Ruby programming skills covering object-oriented programming, metaprogramming, blocks, closures, gems, and the standard library.

What This Jutsu Provides

Validation Hooks

Skills

This jutsu provides the following skills:

Installation

Install with npx (no installation required):

han plugin install jutsu-ruby

Usage

Once installed, this jutsu automatically validates your Ruby code:

Requirements

Skills Overview

Object-Oriented Programming

Learn Ruby’s elegant OOP features:

Blocks, Procs, and Lambdas

Master Ruby’s functional programming:

Metaprogramming

Harness Ruby’s dynamic nature:

Gems and Bundler

Manage dependencies effectively:

Standard Library

Utilize Ruby’s built-in power:

Contributing

See CONTRIBUTING.md for guidelines.

License

MIT License - See LICENSE for details.

Included Skills

This plugin includes 5 skill definitions:

ruby-blocks-procs-lambdas

Use when working with Ruby blocks, procs, lambdas, and functional programming patterns including closures and higher-order functions.

View skill definition

Ruby Blocks, Procs, and Lambdas

Master Ruby’s functional programming features with blocks, procs, and lambdas. These are fundamental to Ruby’s expressive and elegant style.

Blocks

Basic Block Syntax

# Block with do...end (multi-line)
[1, 2, 3].each do |num|
  puts num * 2
end

# Block with {...} (single line)
[1, 2, 3].each { |num| puts num * 2 }

Yielding to Blocks

def repeat(times)
  times.times do
    yield  # Execute the block
  end
end

repeat(3) { puts "Hello" }

# With block parameters
def greet
  yield("World")
end

greet { |name| puts "Hello, #{name}!" }

Block Arguments

def process_data(data)
  result = yield(data)
  puts "Result: #{result}"
end

process_data(10) { |x| x * 2 }  # Result: 20

Checking for Blocks

def optional_block
  if block_given?
    yield
  else
    puts "No block provided"
  end
end

optional_block { puts "Block executed" }
optional_block

Block Local Variables

x = 10

[1, 2, 3].each do |num; local_var|
  local_var = num * 2  # local_var only exists in block
  puts local_var
end

puts x  # 10 (unchanged)

Procs

Creating Procs

# Using Proc.new
my_proc = Proc.new { |x| x * 2 }
puts my_proc.call(5)  # 10

# Using proc method (deprecated in some versions)
my_proc = proc { |x| x * 2 }

# Using -> (stabby lambda syntax for Proc)
my_proc = ->(x) { x * 2 }

Proc Characteristics

# Procs don't care about argument count
flexible_pro

...(truncated)

</details>

### ruby-gems-and-bundler

> Use when working with Ruby gems, Bundler for dependency management, creating gemspecs, and publishing gems to RubyGems.

<details>
<summary>View skill definition</summary>

# Ruby Gems and Bundler

Master Ruby's package management system with gems and Bundler. Learn to manage dependencies, create gems, and publish to RubyGems.

## Bundler Basics

### Gemfile

```ruby
source 'https://rubygems.org'

# Ruby version
ruby '3.3.0'

# Production gems
gem 'rails', '~> 7.1'
gem 'pg', '>= 1.1'
gem 'puma', '~> 6.0'

# Development and test gems
group :development, :test do
  gem 'rspec-rails'
  gem 'factory_bot_rails'
  gem 'faker'
end

# Development only
group :development do
  gem 'rubocop'
  gem 'rubocop-rails'
end

# Test only
group :test do
  gem 'capybara'
  gem 'selenium-webdriver'
end

# Git source
gem 'my_gem', git: 'https://github.com/user/my_gem.git'

# Local path (for development)
gem 'local_gem', path: '../local_gem'

# Specific branch
gem 'experimental_gem', git: 'https://github.com/user/repo.git', branch: 'develop'

# Require specific file or false to not auto-require
gem 'sidekiq', require: 'sidekiq/web'
gem 'bootsnap', require: false

Version Constraints

# Exact version
gem 'rails', '7.1.0'

# Pessimistic operator (allows patch updates)
gem 'rails', '~> 7.1.0'  # >= 7.1.0 and < 7.2.0
gem 'rails', '~> 7.1'    # >= 7.1.0 and < 8.0.0

# Greater than or equal
gem 'pg', '>= 1.1'

# Range
gem 'ruby-version', '>= 1.0', '< 2.0'

# Multiple constraints
gem 'nokogiri', '>= 1.10', '< 2.0'

Bundler Commands

# Install all gems from Gemfile
bundle install

# Install to specific path
bundle install --path vendor/bundle

#

...(truncated)

</details>

### ruby-metaprogramming

> Use when working with Ruby metaprogramming features including dynamic method definition, method_missing, class_eval, define_method, and reflection.

<details>
<summary>View skill definition</summary>

# Ruby Metaprogramming

Master Ruby's powerful metaprogramming capabilities to write code that writes code. Ruby's dynamic nature makes it exceptionally good at metaprogramming.

## Dynamic Method Definition

### define_method

```ruby
class Person
  [:name, :age, :email].each do |attribute|
    define_method(attribute) do
      instance_variable_get("@#{attribute}")
    end

    define_method("#{attribute}=") do |value|
      instance_variable_set("@#{attribute}", value)
    end
  end
end

person = Person.new
person.name = "Alice"
puts person.name  # "Alice"

class_eval and instance_eval

# class_eval - Evaluates code in context of a class
class MyClass
end

MyClass.class_eval do
  def hello
    "Hello from class_eval"
  end
end

puts MyClass.new.hello

# instance_eval - Evaluates code in context of an instance
obj = Object.new
obj.instance_eval do
  def greet
    "Hello from instance_eval"
  end
end

puts obj.greet

module_eval

module MyModule
end

MyModule.module_eval do
  def self.info
    "Module metaprogramming"
  end
end

puts MyModule.info

Method Missing

Basic method_missing

class DynamicFinder
  def initialize(data)
    @data = data
  end

  def method_missing(method_name, *args)
    if method_name.to_s.start_with?("find_by_")
      attribute = method_name.to_s.sub("find_by_", "")
      @data.find { |item| item[attribute.to_sym] == args.first }
    else
      super
    end
  end

  def respond_to_missing?(method_

...(truncated)

</details>

### ruby-object-oriented-programming

> Use when working with Ruby's object-oriented programming features including classes, modules, inheritance, mixins, and method visibility.

<details>
<summary>View skill definition</summary>

# Ruby Object-Oriented Programming

Master Ruby's elegant object-oriented programming features. Ruby is a pure object-oriented language where everything is an object.

## Class Definition

### Basic Class Structure

```ruby
class Person
  # Class variable (shared across all instances)
  @@count = 0

  # Constant
  MAX_AGE = 150

  # Class method
  def self.count
    @@count
  end

  # Constructor
  def initialize(name, age)
    @name = name  # Instance variable
    @age = age
    @@count += 1
  end

  # Instance method
  def introduce
    "Hi, I'm #{@name} and I'm #{@age} years old"
  end

  # Attribute accessors (getter and setter)
  attr_accessor :name
  attr_reader :age      # Read-only
  attr_writer :email    # Write-only
end

person = Person.new("Alice", 30)
puts person.introduce
person.name = "Alicia"

Method Visibility

class BankAccount
  def initialize(balance)
    @balance = balance
  end

  # Public methods (default)
  def deposit(amount)
    @balance += amount
    log_transaction(:deposit, amount)
  end

  def balance
    format_currency(@balance)
  end

  # Protected methods - callable by instances of same class/subclass
  protected

  def log_transaction(type, amount)
    puts "[#{type}] #{amount}"
  end

  # Private methods - only callable within this instance
  private

  def format_currency(amount)
    "$#{amount}"
  end
end

Inheritance

Single Inheritance

class Animal
  def initialize(name)
    @name = name
  end

  def s

...(truncated)

</details>

### ruby-standard-library

> Use when working with Ruby's standard library including Enumerable, File I/O, Time/Date, Regular Expressions, and core classes.

<details>
<summary>View skill definition</summary>

# Ruby Standard Library

Master Ruby's rich standard library. Ruby comes with powerful built-in classes and modules that handle common programming tasks elegantly.

## Enumerable

The Enumerable module provides iteration methods for collections.

### Common Enumerable Methods

```ruby
numbers = [1, 2, 3, 4, 5]

# map/collect - Transform elements
numbers.map { |n| n * 2 }  # [2, 4, 6, 8, 10]
numbers.map(&:to_s)        # ["1", "2", "3", "4", "5"]

# select/filter - Keep elements matching condition
numbers.select { |n| n.even? }  # [2, 4]
numbers.filter(&:odd?)          # [1, 3, 5]

# reject - Remove elements matching condition
numbers.reject { |n| n.even? }  # [1, 3, 5]

# find/detect - First element matching condition
numbers.find { |n| n > 3 }  # 4

# find_all - All elements matching condition (alias for select)
numbers.find_all { |n| n > 2 }  # [3, 4, 5]

# reduce/inject - Accumulate values
numbers.reduce(0) { |sum, n| sum + n }  # 15
numbers.reduce(:+)  # 15
numbers.reduce(:*)  # 120

# each - Iterate over elements
numbers.each { |n| puts n }

# each_with_index - Iterate with index
numbers.each_with_index { |n, i| puts "#{i}: #{n}" }

# each_with_object - Iterate with mutable object
numbers.each_with_object({}) { |n, hash| hash[n] = n * 2 }

# any? - True if any element matches
numbers.any? { |n| n > 4 }  # true
numbers.any?(&:even?)       # true

# all? - True if all elements match
numbers.all? { |n| n > 0 }  # true
numbers.all?(&:even?)       # false

# none? - True if no

...(truncated)

</details>

## Source

[View on GitHub](https://github.com/TheBushidoCollective/han)