`
tim.teng
  • 浏览: 10051 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

Understanding 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
分享到:
评论

相关推荐

    Writing Linux Device Drivers: a guide with exercises

    Writing Linux Device Drivers is designed to show experienced programmers how to develop device drivers for Linux systems, and give them a basic understanding and familiarity with the Linux kernel....

    1Z0-811 Exam Guide to Have a Cakewalk in Oracle Java SE Certific

    Allocate extra time to challenging areas. - **Active Learning Techniques:** Engage in active learning techniques such as note-taking, summarizing, and teaching others to reinforce your understanding...

    09新东方在线考研英语强化班阅读讲义

    When investors believe a particular industry or company has strong growth potential, they are more likely to allocate their resources there. Private businesses, large or small, play a crucial role ...

    大数据-算法-探究教学对初中生数学解题与交流能力的影响.pdf

    Meanwhile, during the communication process, students can clarify their misconceptions, reinforcing their internal understanding of mathematical concepts, which in turn enhances their problem-solving...

    Direct Memory Access in Linux.doc

    By understanding the limitations of traditional methods like `/dev/mem` and leveraging kernel modules and DMA functions such as `ioremap()` and `remap_pfn_range()`, developers can allocate and access...

    c-programming-for-scientists-and-engineers-manufacturing-engineering-series

    The book explains how to define and use pointers to structs, and how to dynamically allocate memory for data structures. ### Chapter 2: Introduction to Executable Statements #### 2.1 Introduction ...

    操作系统课件

    Preemptive scheduling is another key concept, which allows the operating system to interrupt a currently executing process and allocate the CPU to a higher priority process. There are two types of ...

    用友自动化测试面试题

    - **Select Tools and Resources**: Choose appropriate tools and allocate necessary resources. - **Create a Schedule**: Develop a detailed schedule for testing activities. - **Creating Test Cases**: ...

    韩国崇实大学 Ad hoc and Sensor Networks 英文课件

    8. Schedule-Based ProtocolsSchedule-based protocols, on the other hand, allocate transmission slots to nodes in advance. They provide better channel utilization and reduced collision rates but require...

    Virtualizing SQL Server with VMware_English

    #### Understanding SQL Server Virtualization **Definition of SQL Server Virtualization**: SQL Server virtualization involves deploying SQL Server instances on virtual machines (VMs) rather than ...

    考研英语面试问答题目以及答案的整理.pdf,这是一份不错的文件

    My undergraduate studies laid a solid foundation for my postgraduate pursuit by providing a comprehensive understanding of the basics in my major. The lectures, laboratory sessions, and practical ...

    APICS DICTIONARY

    By focusing on the activities needed at each stage, organizations can better allocate resources, manage costs, and ensure that project milestones are met efficiently. #### 2. Activity-Based Cost ...

    Speex手册----编解码介绍

    In VBR mode, Speex will allocate more bits for complex parts of the speech signal and fewer bits for simpler parts, resulting in an average bit rate that is typically lower than a constant bit rate ...

    CUDA Reference from NVIDIA official site.

    This is useful for developers who need to know how many GPUs are present in the system before attempting to allocate resources or launch kernels on them. **1.1.2 cudaSetDevice** `cudaSetDevice` ...

    CUDA_C_Programming_Guide

    ### CUDA C Programming Guide ... By understanding and applying the principles outlined in this guide, developers can effectively harness the power of parallel computing for a wide range of applications.

    Debugging Malloc Lab: Detecting Memory-Related Errors

    The skills you will have learned upon the completion of this exercise are pointer arithmetic and a greater understanding of the consequences of subtle memory mistakes. Logistics Unzip debugging_...

    JAVA面试题目 JAVA面试题目一览表

    Suggestion: Share your understanding of a few key patterns and their real-world applications. "I have extensive experience implementing Singleton, Factory, and Observer patterns. For instance, using ...

    微软内部资料-SQL性能优化2

    Before we look at how SQL Server uses and manages its memory, we need to ensure a full understanding of the more common memory related terms. The following definitions will help you understand how SQL...

    Linux System Administrator Guide Version0.9

    A significant portion of the guide is dedicated to understanding the Linux directory tree, which is crucial for system administration tasks. The following directories are highlighted: - **Root ...

    二级减速器课程设计说明书reducer design specification.doc

    (4) enhanced our understanding and application of the functions of Word and AutoCAD in office software. Keywords: reducer, transmission device, design, calculation, CAD Chapter 1 Course Design Task ...

Global site tag (gtag.js) - Google Analytics