Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
pathbox
A-bit-of-ruby-code
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
A-bit-of-ruby-code
/
alias_method.rb
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
20 lines (16 loc) · 1.05 KB
master
Breadcrumbs
A-bit-of-ruby-code
/
alias_method.rb
Copy path
Top
File metadata and controls
Code
Blame
20 lines (16 loc) · 1.05 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#替换方法 Replacing methods
#有时候一个方法的实现不是你要的,或者只做了一半。
# 标准的面向对象方法是继承并重载,再调用父类方法。
# 仅当你有对象实例化的控制权时才有用,经常不是这种情况,继承也就没有价值。
# 为得到同样的功能,可以重命名(alias)旧方法,并添加一个新的方法定义来调用旧方法,并确保旧方法的前后条件得到保留。
# 通过这个方式, 可以一定意义上做到方法的重写. 因为新方法可以有新的行为,而且还保留着旧方法的访问
class String
alias_method :original_reverse, :reverse # alias_method :new_name_for_old_def, :old_to_new_def
def reverse
puts "reversing, please wait..."
end
end
"dog".reverse # reversing, please wait...
"dog".original_reverse # god
# 原来 String 只有 reverse方法没有original_reverse方法。
# 使用alias_method后, original_reverse 代替并保留reverse方法的功能(相当于reverse方法的重命名), reverse实现代码中新更改后的功能
You can’t perform that action at this time.