Understanding allocate

本文探讨了Ruby中allocate方法的使用场景,该方法允许创建未初始化的对象实例,这对于某些特定情况非常有用,例如当对象的状态完全由其访问器确定时。

Understanding allocate

In rare circumstances you might want to create an object without calling its constructor (bypassing initialize). For example, maybe you have an object whose state is determined entirely by its accessors; then it isn't necessary to call mew (which calls initialize) unless you really want to. Imagine you are gathering data a piece at a time to fill in the state of an object; you might want to start with an "empty" object rather than gathering all the data up front and calling the constructor.

The allocate method was introduced in Ruby 1.8 to make this easier. It returns a "blank" object of the proper class, yet uninitialized.

class Person
attr_accessor :name, :age, :phone

def initialize(n,a,p)
@name, @age, @phone = n, a, p
end
end

p1 = Person.new("John Smith",29,"555-1234")

p2 = Person.allocate

p p1.age # 29
p p2.age # nil
name: 🐛 Bug Report # title: " " description: Problems with YOLOv8 labels: [bug, triage] body: - type: markdown attributes: value: | Thank you for submitting a YOLOv8 🐛 Bug Report! - type: checkboxes attributes: label: Search before asking description: > Please search the [issues](https://github.com/ultralytics/ultralytics/issues) to see if a similar bug report already exists. options: - label: > I have searched the YOLOv8 [issues](https://github.com/ultralytics/ultralytics/issues) and found no similar bug report. required: true - type: dropdown attributes: label: YOLOv8 Component description: | Please select the part of YOLOv8 where you found the bug. multiple: true options: - "Training" - "Validation" - "Detection" - "Export" - "PyTorch Hub" - "Multi-GPU" - "Evolution" - "Integrations" - "Other" validations: required: false - type: textarea attributes: label: Bug description: Provide console output with error messages and/or screenshots of the bug. placeholder: | 💡 ProTip! Include as much information as possible (screenshots, logs, tracebacks etc.) to receive the most helpful response. validations: required: true - type: textarea attributes: label: Environment description: Please specify the software and hardware you used to produce the bug. placeholder: | - YOLO: Ultralytics YOLOv8.0.21 🚀 Python-3.8.10 torch-1.13.1+cu117 CUDA:0 (A100-SXM-80GB, 81251MiB) - OS: Ubuntu 20.04 - Python: 3.8.10 validations: required: false - type: textarea attributes: label: Minimal Reproducible Example description: > When asking a question, people will be better able to provide help if you provide code that they can easily understand and use to **reproduce** the problem. This is referred to by community members as creating a [minimal reproducible example](https://docs.ultralytics.com/help/minimum_reproducible_example/). placeholder: | ``` # Code to reproduce your issue here ``` validations: required: false - type: textarea attributes: label: Additional description: Anything else you would like to share? - type: checkboxes attributes: label: Are you willing to submit a PR? description: > (Optional) We encourage you to submit a [Pull Request](https://github.com/ultralytics/ultralytics/pulls) (PR) to help improve YOLOv8 for everyone, especially if you have a good understanding of how to implement a fix or feature. See the YOLOv8 [Contributing Guide](https://docs.ultralytics.com/help/contributing) to get started. options: - label: Yes I'd like to help by submitting a PR!
10-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值