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.
发表评论
-
12.3.3 scaling issue of the status feed
2011-10-30 17:54 814the problem of the implementati ... -
12.3 the status feed
2011-10-30 15:34 8521. we need to get all the micro ... -
12.2 a working follow button with Ajax
2011-10-29 18:10 9091. in the last chapter, in the ... -
12.2 a web interface for following and followers.
2011-10-28 22:14 8751.before we do the UI, we need ... -
12. following user, 12.1 relationship model
2011-10-18 14:29 7441. we need to use a relationshi ... -
11.3 manipulating microposts.
2011-10-17 15:31 8931. since all micropost actions ... -
11.2 show microposts.
2011-10-17 12:01 6961. add test to test the new use ... -
11.1 user micropost -- a micropost model.
2011-10-17 10:43 11031. we will first generate a mic ... -
10.4 destroying users.
2011-10-16 15:47 732in this chapter, we will add de ... -
10.3 showing users list
2011-10-15 20:41 768in this chapter, we will do use ... -
10.2 protect pages.
2011-10-15 15:11 655again, we will start from TD ... -
10.1 updating users.
2011-10-14 18:30 7041. git checkout -b updating-use ... -
9.4 sign out
2011-10-13 15:21 730whew!!!, last chapter is a long ... -
9.3 sign in success.
2011-10-12 15:39 7411. we will first finish the cre ... -
9.1 about flash.now[:error] vs flash[:error]
2011-10-12 15:37 722There’s a subtle difference ... -
9.2 sign in failure
2011-10-12 12:19 655start from TDD!!! 1. requir ... -
9.1 sessions
2011-10-12 10:00 642a session is a semi-permanent c ... -
what test framework should you use?
2011-10-11 16:56 0for integration test, i have no ... -
what test framework should you use?
2011-10-11 16:56 0<p>for integration test, ... -
8.4 rspec integration tests
2011-10-11 16:53 714in integration test, you can te ...
相关推荐
### 重构在 Ruby 中的应用 #### 重构的意义与目的 重构是软件开发过程中不可或缺的一部分,其目的是为了改善代码结构而不改变其外部行为。通过重构,开发者可以提高代码的可读性、可维护性和扩展性,使得未来的修改...
在电力系统设计中,章节4.1到4.4主要关注了配电网络的设计以及新网络用户对网络的影响。本文将详细阐述这些知识点。 首先,我们来看配电网络的分类(4.1节)。根据表4.1,我们可以了解到不同级别和类别定义的概述。...
"inheritance---derived-class.rar_inheritance"这个压缩包文件显然包含了关于C++继承和派生类的详细教程。 继承的概念在于,子类可以自动获取基类的所有公共成员(包括数据成员和成员函数),并且可以添加新的成员...
在IT领域,尤其是在面向对象编程(Object-Oriented Programming,OOP)中,继承(Inheritance)是一项核心概念,它允许一个类(称为子类或派生类)继承另一个类(称为父类或基类)的特性与行为。C#作为一种广泛使用...
### Refactoring in Ruby #### 知识点概览 本文将深入探讨《Refactoring In Ruby》一书中提及的各种代码重构模式及其应用场景。该书由Lee Bogdanoff编撰,是Ruby开发人员进行代码优化和改进的重要参考。我们将逐一...
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中,方法和类是构建程序的基本元素,它们是实现代码重用和组织的关键概念。 ### 方法(Methods) 方法在Ruby中是可重复使用的代码...
### Eloquent Ruby:深入探索Ruby语言的魅力 #### 引言 《Eloquent Ruby》是一本深受Ruby开发者喜爱的书籍,作者Russ Olsen通过本书为读者提供了一条清晰的学习路径,不仅适用于初学者,也适合那些已经有一定经验...
理解类(Class)、对象(Object)、继承(Inheritance)、模块(Module)和封装(Encapsulation)是核心内容。 5. **块、Proc和Lambda**:Ruby中的块是代码片段,可以用do..end或者花括号{}包裹。Proc和Lambda是可...
最后,Ruby有强大的模块(module)和继承(inheritance)机制,允许代码重用和扩展。模块可以包含常量、方法和类,而类可以通过`操作符继承另一个类的特性。 总之,二十分钟的Ruby入门教程将带你初步了解这个优雅的...
本文将深入探讨“View Class Inheritance Hierarchy”这一功能,它专门用于查看 MATLAB 中的类继承层次结构,这对于理解和调试复杂的 OOP 项目至关重要。 MATLAB 的 OOP 模型支持类的定义、继承、封装和多态性,...
5. 面向对象特性:包括继承(Inheritance)、多态(Polymorphism)、封装(Encapsulation)。 6. 常量(Constant):在程序运行期间其值不能改变的变量。 7. 变量(Variable):分为局部变量、实例变量、类变量和...
这个"qt-all-class-inheritance-diagrams.rar"压缩包包含了QT框架中的所有类的继承关系图,对于理解和学习QT库的架构至关重要。下面将详细阐述QT的类继承体系和相关知识点。 首先,QT的核心在于其面向对象的设计,...
- 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"指的是在编程领域中的继承机制,特别是在面向对象编程(OOP)的概念中。继承是面向对象编程的一个核心特性,允许一个类(子类或派生类)从另一个类(父类或基类)继承属性和方法。这种设计模式...
- 类(Class):Ruby是纯面向对象的语言,一切皆对象,类用于创建对象的蓝图。 - 对象(Object):每个实例都有其特定的属性(实例变量)和行为(方法)。 - 继承(Inheritance):子类可以继承父类的属性和方法...
- `store_full_sti_class` 为 `true`,表示在单表继承(Single Table Inheritance, STI)模式下存储完整的类名(包括模块命名空间)。 2. `generate_best_match` 被设置为 `false`,这改变了路由生成的最佳匹配算法...
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 ...