Tips

The value returned by a Ruby
method is the value of the last expression evaluated, so we can get rid of
the temporary variable and the return statement altogether.

Prefixing a method name with self. (as we do on line 5) makes it a class
method: it can be called on the class generally.


Instance variables are not directly accessible outside the class. To make
them available, write methods that return their values.
class Greeter
def initialize(name)
@name = name
end
def name
@name
end
def name=(new_name)
@name = new_name
end
end

 

The private directive is the strictest; private methods can be called only
from within the same instance. Protected methods can be called in the
same instance and by other instances of the same class and its subclasses.

Modules are similar to classes in that they hold a collection of methods,
constants, and other module and class definitions. Unlike classes, you
cannot create objects based on modules.


In Ruby,
that’s not the case; nil is an object, just like any other, that happens to
represent nothing.

So an ORM layer maps tables to classes, rows to objects, and columns to
attributes of those objects. Class methods are used to perform table-level
operations, and instance methods perform operations on the individual
rows.
By relying on convention and starting with sensible defaults,
Active Record minimizes the amount of configuration that developers perform.


Ruby statement modifiers are a useful shortcut if the body of an if or while statement
statement is just a single expression. Simply write the expression, followed
by if or while and the condition.
puts "Danger, Will Robinson" if radiation > 3000


In Ruby, you typically create a regular expression by
writing /pattern/ or %r{pattern}.


Marshaling Objects

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值