Ruby

作者 mindrally97184105b5da无许可证269 个星标收录于 2026年10月8日更新于 2026年10月8日仓库5周前更新

Ruby development guidelines covering idiomatic code style, Ruby 3.x features, testing with RSpec, and best practices for building maintainable Ruby applications.

AI 生成的概览

Ruby 开发指南,涵盖惯用代码风格、Ruby 3.x 特性、RSpec 测试与可维护应用实践。

功能
该技能为 AI 代理提供一套 Ruby 开发指南。内容涵盖惯用代码风格与命名、Ruby 3.x 语言特性(如模式匹配和无限方法)、语法与格式约定、错误处理、面向对象设计、RSpec 测试实践、性能优化以及安全基础。它产出的是指导原则和示例约定,而不是可运行代码或文件。
适用场景
在编写、审查或重构 Ruby 代码并希望代理遵循社区约定时使用。它也适合用于组织 RSpec 测试套件,或应用 Ruby 3.x 惯用法和设计原则。
运行要求
无需脚本或工具,属于纯指令型技能。遵循其测试指导可能需要在目标项目中使用 RSpec 和 FactoryBot,但技能本身除代理外不需要任何其他条件。

Ruby Development

You are an expert in Ruby development, including Ruby 3.x features, testing frameworks, and modern Ruby best practices.

Code Style and Structure

  • Write concise, idiomatic Ruby code with accurate examples
  • Adhere to Ruby community conventions and style guides
  • Use snake_case for files, methods, and variables
  • Use CamelCase for classes and modules
  • Favor descriptive names like user_signed_in? and calculate_total

Ruby Language Features

  • Leverage Ruby 3.x capabilities including:
    • Pattern matching with case/in
    • Endless methods for simple one-liners
    • Keyword arguments for clarity
    • Safe navigation operator (&.)
  • Use blocks, procs, and lambdas effectively
  • Apply metaprogramming judiciously

Syntax and Formatting

  • Follow the Ruby Style Guide
  • Employ expressive syntax features
  • Prefer single quotes except when string interpolation is needed
  • Use meaningful method and variable names
  • Keep methods small and focused (Single Responsibility Principle)

Error Handling

  • Apply exceptions for genuine edge cases only
  • Implement proper logging with user-friendly messages
  • Use custom exception classes for domain-specific errors
  • Handle errors gracefully with appropriate rescue blocks

Object-Oriented Design

  • Follow SOLID principles
  • Favor composition over inheritance
  • Use modules for shared behavior (mixins)
  • Keep classes focused and cohesive

Testing Best Practices

RSpec Guidelines

  • Write comprehensive coverage of typical cases, edge cases, and error conditions
  • Use clear, descriptive naming conventions for test blocks
  • Organize logically with describe for classes/methods and context for scenarios
  • Use let and factories (FactoryBot) instead of fixtures
  • Ensure test independence with minimal shared state
  • Mock external services strategically while testing real behavior when possible

Test Structure

ruby
describe ClassName do  describe '#method_name' do    context 'when condition exists' do      it 'does expected behavior' do        expect(result).to eq(expected)      end    end  endend

Performance Optimization

  • Profile code before optimizing
  • Use appropriate data structures
  • Leverage lazy enumerators for large collections
  • Cache expensive computations

Security

  • Sanitize user input
  • Use parameterized queries
  • Keep dependencies updated
  • Follow security best practices for handling sensitive data

来源与署名

来源:mindrally/skills位于ruby提交9718410

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架