140529 Ruby - codecademy

the Zen of Ruby

||= - conditional asignment, 只在variable为nil时赋值

defind method 可省略return,it will return the result of the last evaluated expression.

.upto() / .downto() - 又一种遍历方法

.next - integer会返回下一个integer

#{ } - string interpolation, 在处理non-string时用


Control flow

case -> when -> when -> else -> end

Array

.push() - (srings也可以用) add an element to the end of an array #可以写成<<


Hash

set default value: Hash.new("default value")

.each_key / .each_value

.delete

Symbol

there's only one copy of any particular symbol at a given time.

用途: hash keys

referencing method names - e.g. [1,2,3].respond_to?(:push)

symbols can't be changed once created

和string转化:.to_sym ( .intern ) , .to_s

*ruby1.9变化:创建hash时写成{one: 1},不用=>

.select - filter a hash for values that meet certain criteria

Block

blocks are not objects

.collect - returns a copy, dosen't mutate the origin value

yield - 此处运行block中的代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值