`
peryt
  • 浏览: 54352 次
  • 来自: ...
最近访客 更多访客>>
社区版块
存档分类
最新评论
  • waiting: 既然都指定了dataType为'script'那就不必特别在b ...
    jQuery

4.4 play with class inheritance in ruby

阅读更多

 

1. 
class Word
    def palindrome?(string)
        string == string.reverse
    end
end

 in this class, it doesn't indicate superclass explicitly, so the default is inherit from Object class.

 

since it doesn't define initialize method, it will use the initialize method of Object class.

 

how to use it?

 

w = Word.new

w.palindrome?("foobar")   => false

w.palindrome?("level")    => true

 

2. a better definition will be inherit from String class:

 

class Word < String

def palindrome?

self == self.reverse

end

end

 

how to use it?

 

s = Word.new("level")

s.palindrome?   => true

s.length   =>  5

 

inside the Word class, self is the Object itself.

 

3. and next, we find it will be more natural to define the palindrome? method in the String class itself, so that we can call it on a string directly.

 

Ruby will let you do this, Ruby classes can be opened and modified, allowing developer to add methods:

 

 

class String
    def palindrome?
        self == self.reverse
    end
end

 although this feature is very powerful, you need to be careful, 

don't do it unless you have a really good reason.

 

Rails add many methods to ruby for good reasons, for example, 

Rails add "blank" method to Object class:

 

 

"".blank?
=> true
"           ".empty?
=> false
"           ".blank?
=> true

nil.blank?
=> true

 because in web dev, we often want to prevent var from being blank, like space or other whitespace.

 

 

4. let define a User class this time:

 

 

class User
  attr_accessor :name, :email
  
  def initialize(attributes = {})
    @name = attributes[:name]
    @email = attributes[:email]
  end
  
  def formated_email
    "#{@name} <#{@email}>"
  end

end
 

attr_accessor  defines the get and set method for @name and @email.

initialize method is special in Ruby, it is called when exec User.new

 

save this part of code into a file called example_user.rb

 

then in the console:

 

 

require './example_user'
example = User.new
example.name = "Example User"
example.email = "user@example.com"
example.formatted_email
 

 

 

Remember that we can omit the {} in the final hash param when calling a method.

so we can create another user by this way:

 

 

user = User.new(:name => "abcd", :email => "abcd@abcd.com")

 

It is very common to use hash argument in Rails.

 

0
3
分享到:
评论

相关推荐

    Refactoring.in.Ruby.pdf

    ### 重构在 Ruby 中的应用 #### 重构的意义与目的 重构是软件开发过程中不可或缺的一部分,其目的是为了改善代码结构而不改变其外部行为。通过重构,开发者可以提高代码的可读性、可维护性和扩展性,使得未来的修改...

    chapter4.1-4.4

    在电力系统设计中,章节4.1到4.4主要关注了配电网络的设计以及新网络用户对网络的影响。本文将详细阐述这些知识点。 首先,我们来看配电网络的分类(4.1节)。根据表4.1,我们可以了解到不同级别和类别定义的概述。...

    inheritance---derived-class.rar_inheritance

    "inheritance---derived-class.rar_inheritance"这个压缩包文件显然包含了关于C++继承和派生类的详细教程。 继承的概念在于,子类可以自动获取基类的所有公共成员(包括数据成员和成员函数),并且可以添加新的成员...

    CSharp - Module 10_Inheritance in C#

    在IT领域,尤其是在面向对象编程(Object-Oriented Programming,OOP)中,继承(Inheritance)是一项核心概念,它允许一个类(称为子类或派生类)继承另一个类(称为父类或基类)的特性与行为。C#作为一种广泛使用...

    Refactoring In Ruby

    ### Refactoring in Ruby #### 知识点概览 本文将深入探讨《Refactoring In Ruby》一书中提及的各种代码重构模式及其应用场景。该书由Lee Bogdanoff编撰,是Ruby开发人员进行代码优化和改进的重要参考。我们将逐一...

    Head First Ruby.pdf

    With Early Release ebooks, you get books in their earliest form—the author's raw and unedited content as he or she writes—so you can take advantage of these technologies long before the official ...

    Ruby 方法、类

    Ruby 是一种面向对象的编程语言,以其优雅的语法和强大的面向对象特性著称。在Ruby中,方法和类是构建程序的基本元素,它们是实现代码重用和组织的关键概念。 ### 方法(Methods) 方法在Ruby中是可重复使用的代码...

    eloquent ruby

    ### Eloquent Ruby:深入探索Ruby语言的魅力 #### 引言 《Eloquent Ruby》是一本深受Ruby开发者喜爱的书籍,作者Russ Olsen通过本书为读者提供了一条清晰的学习路径,不仅适用于初学者,也适合那些已经有一定经验...

    learning-ruby.

    理解类(Class)、对象(Object)、继承(Inheritance)、模块(Module)和封装(Encapsulation)是核心内容。 5. **块、Proc和Lambda**:Ruby中的块是代码片段,可以用do..end或者花括号{}包裹。Proc和Lambda是可...

    二十分钟Ruby入门教程

    最后,Ruby有强大的模块(module)和继承(inheritance)机制,允许代码重用和扩展。模块可以包含常量、方法和类,而类可以通过`操作符继承另一个类的特性。 总之,二十分钟的Ruby入门教程将带你初步了解这个优雅的...

    View Class Inheritance Hierarchy:查看一个Matlab OO项目的继承层次-matlab开发

    本文将深入探讨“View Class Inheritance Hierarchy”这一功能,它专门用于查看 MATLAB 中的类继承层次结构,这对于理解和调试复杂的 OOP 项目至关重要。 MATLAB 的 OOP 模型支持类的定义、继承、封装和多态性,...

    ruby api html

    5. 面向对象特性:包括继承(Inheritance)、多态(Polymorphism)、封装(Encapsulation)。 6. 常量(Constant):在程序运行期间其值不能改变的变量。 7. 变量(Variable):分为局部变量、实例变量、类变量和...

    qt-all-class-inheritance-diagrams.rar_QT树状图_inheritance_qt 树状图_q

    这个"qt-all-class-inheritance-diagrams.rar"压缩包包含了QT框架中的所有类的继承关系图,对于理解和学习QT库的架构至关重要。下面将详细阐述QT的类继承体系和相关知识点。 首先,QT的核心在于其面向对象的设计,...

    FastReport.v4.15 for.Delphi.BCB.Full.Source企业版含ClientServer中文修正版支持D4-XE5

    - fixed bug with Unicode in TfrxMemoView appeared in previous release - improved MAPI interface in TfrxExportMail export - fixed some problems with allpication styles XE2/XE3 - improved compatibility ...

    inheritance

    【标题】"inheritance"指的是在编程领域中的继承机制,特别是在面向对象编程(OOP)的概念中。继承是面向对象编程的一个核心特性,允许一个类(子类或派生类)从另一个类(父类或基类)继承属性和方法。这种设计模式...

    Ruby语言中文教程

    - 类(Class):Ruby是纯面向对象的语言,一切皆对象,类用于创建对象的蓝图。 - 对象(Object):每个实例都有其特定的属性(实例变量)和行为(方法)。 - 继承(Inheritance):子类可以继承父类的属性和方法...

    Ruby的25个编程细节(技巧、实用代码段)

    - `store_full_sti_class` 为 `true`,表示在单表继承(Single Table Inheritance, STI)模式下存储完整的类名(包括模块命名空间)。 2. `generate_best_match` 被设置为 `false`,这改变了路由生成的最佳匹配算法...

    Java.Persistence.with.Hibernate.2nd.Edition

    We show you how to deal with inheritance, collections, and complex class associations. Finally, we discuss integration with legacy database schemas and some mapping strategies that are especially ...

Global site tag (gtag.js) - Google Analytics