ruby
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseRuby
Ruby
A dynamic, interpreted language known for its elegant syntax.
一种以优雅语法著称的动态解释型语言。
When to Use
适用场景
- Web Development (Ruby on Rails)
- Scripting / Automation
- DevOps tools (Chef, Puppet)
- Prototyping
- 网页开发(Ruby on Rails)
- 脚本编写/自动化
- DevOps工具(Chef、Puppet)
- 原型开发
Quick Start
快速入门
ruby
puts "Hello, World!"
class Greeter
def initialize(name)
@name = name
end
def say_hi
puts "Hi #{@name}!"
end
end
g = Greeter.new("Alice")
g.say_hiruby
puts "Hello, World!"
class Greeter
def initialize(name)
@name = name
end
def say_hi
puts "Hi #{@name}!"
end
end
g = Greeter.new("Alice")
g.say_hiCore Concepts
核心概念
Everything is an Object
一切皆为对象
Numbers, strings, even nil are objects.
ruby
1.odd? # => true数字、字符串,甚至nil都是对象。
ruby
1.odd? # => trueBlocks & Iterators
代码块与迭代器
Functional-style constructs for iteration.
ruby
[1, 2, 3].each do |n|
puts n * 2
end用于迭代的函数式构造。
ruby
[1, 2, 3].each do |n|
puts n * 2
endMetaprogramming
元编程
Writing code that writes code (used heavily in Rails).
编写能生成代码的程序(在Rails中被大量使用)。
Best Practices
最佳实践
Do:
- Optimize for developer happiness (readability)
- Use standard style guide (RuboCop)
- Use blocks for resource management ()
File.open
Don't:
- Overuse monkey patching (modifying core classes)
- Write "Perl-ish" Ruby (keep it clean)
建议:
- 优先考虑开发者幸福感(可读性)
- 使用标准风格指南(RuboCop)
- 使用代码块进行资源管理(如)
File.open
避免:
- 过度使用猴子补丁(修改核心类)
- 编写“Perl风格”的Ruby代码(保持代码简洁)