ruby语言注意事项

每一门语言都有其独特之处,今天我就来聊聊ruby的独特之处

1.ruby中除了nil为false,其他都为true

2.ruby中的string是可变的,string类中有一系列修改字符串的方法。例如[]= <<

   ruby中的数值对象是不可变的

3.ruby中的block

3.times { print "ruby }

#do end
1.upto(10) do |x|
  puts x
end

 

4.支持fluent APIs

5.ruby中方法的括号可以省略

--永远不要在方法名和圆括号直接加空格

--要么全部不加括号

--要么全加括号

6.先shebang在coding,shebang永远在第一行

7.字符串内插

8.ruby中array可变,且如果所有超出,返回nil而不是报异常

9.Hash中由于字符串是可变的,当字符串作为键时,被当做特殊对待,会建立私有拷贝。因为使用可变对象作为hash键有许多问题,入股以一个对象作为hash键,但是后来改变了该对象,那么hash表将被破坏。可考虑为可变对象生产私有拷贝或者调用freeze方法。修改之后使用hash类的rehash方法。建议使用symbol作为键。

10.Range对象,<=>操作符,返回-1 0 或者1

11.succ方法

"a".succ # return "b"

1.succ # return 2

 12.Range的成员关系

r = 0...100
r.include? 100 # false
r.member? 50 # true
r.include? 99.9 # true

 13.Symbols

14.内省

o = "123456"
o.class
o.superclass
o.instance_of? String
Object.superclass

x = 1
x.is_a? Fixnum # is_a equal ===
x.is_a? Integer
x.is_a? Numeric
x.is_a? Comparable
x.is_a? Object
x.is_a? BaseObject

o.respond_to? :"<<" # true if o has an << operator
o.respond_to? :"<<" and not o.is_a? Numeric
#关注类型而不是类

 15.相对判断

equal? # 是否同一个对象,永远不要重写,也可以通过比较object_id
a = "ruby"
b= "ruby"
a.equal? b # return false
a == b # return true

#hash值之间的相等性是通过==实现的,键的相等性是通过eql?,某些类不允许类型转化

# === case中的比较
# =~ 用于模式匹配 !~

# 类的对象之间除了定义相等性还可定义顺序性

 16.拷贝对象(clone and dup)

17.冻结对象

s = "ice"
s.freeze #无法自改对象,也无法添加方法
s.frizen?

s.upcase! # error

# 使用clone拷贝后的对象也是被冻结的,使用dup拷贝后未被冻结

 

18.污染对象

s = "abc"
s.taint
s.tainted? # true
s.upcase.tainted? # true
s[1,2].tainted? # true

s.untarint

# 一个tainted的对象clone或者dup后还是受污染的

 17.模块可以嵌套

18.kernel中定义的方法是全局函数,作为Object类的私有方法定义的

a[0] => a.[](0)
s + y => s.+(y)
# += ||= 比较常用

 

19.并行赋值情况

x, y, z = 1, 2, 3
x = 1, 2, 3 # x = [1,2,3]
x, = 1, 2, 3 # x = 1
x, y, z = [1, 2, 3] => x, y, z = 1, 2, 3

x, y, z = 1, *[2, 3] => x, y, z = 1, 2, 3

x, *y = 1, 2, 3 => x = 1, y = [2, 3]

x, (y, z) = a, b => x = a; y, z = b

 

20.求幂操作是右结合的

2**3**4 => 2**(3**4)
--20140707 P119

 

21.Enumerable模块

collect(同map,返回新的数组或其他),select(数组选取),reject(与select正好相反),inject(两个参数,累计值和迭代值)

data = [2,5,3,4]
sum = data.inject {|sum, x| sum+x}
floatprod = data.inject(1.0) {|p,x| p*x}
max = data.inject {|m,x| m>x ? m : x}

enum_for(:sym)

to_enum

外部迭代器(程序员控制迭代,例如next)与内部迭代器

 

22.yield

def sequence(n, m, c)
  i, s = 0, []
  while i<n
  	y = m * i + c
  	yield y if block_given?
  	s << y
  	i += 1
  end
  s
end

 

23.return,redo,next,break,retry,throw catch,raise,rescue,else,ensure

 

24.线程,纤程,连续体(后两者极少使用)

def readfiles filenames
  threads = filenames.map do |f|
  	Thread.new {File.read f}
  end

  threads.map {|t| t.value}
end
f = Fiber.new {
  puts "Fiber says Hello"
  Fiber.yield
  puts "Fiber says Goodbye"
}

puts "Caller says Hello"
f.resume
puts "Caller says Goodbye"
f.resume

25.方法、Proc、lambda、闭包(slosure)

&把一个普通的代码块转化为以Proc对象,如果在Proc对象前加&,那么该Proc将当做普通代码块使用。

 

26.专题:Proc和lambda

三种方法创建proc对象,Proc.new、proc、lambda(后两者都是kernel的类方法),lambda子面量 ->(好处是可以为参数设定默认值)

调用方法,call(参数列表)

=================================

Proc对象有一个arity方法,用于返回期望的参数个数

相等性,有相同的代码块,不意味着相等,应该是克隆或者复制品时,才相等

区别:

proc更像是代码块,而lambda更像是方法

lambda中的return仅从自身返回,而proc不是

lambda会进行参数校验

=================================

proc和lambda都是闭包

Binding对象

 

27.Method对象(很少使用),方法name、owner、receiver

调用,使用call或者[]

区别:Method对象不是闭包

 

第七章:类和模块 p228

 拷贝:dup、clone、initialize_copy

marshal_dump

marshal_load

 

28.单键------仅仅拥有一个实例的类,常用来存储全局程序状态,可以用来替代类方法和类变量

ruby中有点特别的是如果给某个对象加了一个方法,那么他也是单键方法

new和allocate必须私有,必须阻止dup和clone产生新的拷贝。

 

Singleton模块

所有的类都是模块

如果一个模块定义了若干实例方法,而不是类方法,这些方法可以混入类中------include

模块中混入模块Module.include Object.extend

 

29.加载路径

$LOAD_PATH or $:

方法查找:o.m为例

先在o的单键中查找--》实例方法--》在包含的所有模块中按照被引入的顺序逆序查找--》超类查找并重复步骤--》method_missing

类方法查找

先在o的单键中查找--》在包含的所有模块中按照被引入的顺序逆序查找--》超类查找并重复步骤--》method_missing

常量查找:

在包含语句中(即模块或类中)--》在包含的所有模块中按照被引入的顺序逆序查找--》超类查找并重复步骤--》const_missing

 

第八章:反射和元编程

30.反射--内省也

o.class
o.superclass
o.instance_of? c
o.is_a? c
o.kind_of? c
c === o
o.respond_to? name #是否有name这个公开或者保护的方法


module A; end
module B; include A; end;
class C; include B; end;
C<B
B<A
C<A
Fixnum < Integer
Integer < Comparable
# ...
A.ancestors
C.ancestors
B.ancestors
String.ancestors

C.include? B
B.include? A

A.included_modules
B.included_modules



module Greeter; def hi; "hello"; end; end
s="string object"
s.extend(Greeter)
s.hi
String.extend(Greeter)
String.hi

module M
  class C
    Module.nesting # => [M::C, M]
  end
end

eval方法:

x = 1
eval "x+1"

binding对象:

Proc对象的binding方法

ruby1.9中Binding对象的eval方法,无需作为eval的第二个参数传入。

class Object
  def bindings
    binding
  end
end

class Test
  def initialize(x); @x = x; end
end

t = Test.new 10
eval("@x", t.bindings)

31.instance_eval class_eval

instance_eval为对象创建了一个单键方法,(是类对象时,成为类方法)

class_eval定义了一个普通的实例方法

两者与eval的主要区别是可以对代码块求值

 

32.instance_exec class_exec(module_exec)

instance_eval class_eval不同的是他可以接收参数

 

33.常量和变量

global_variables

local_variables

class_variables

instance_variables

constants

 

instance_variable_set

instance_variable_get

instance_variable_defined?

 

class_variable_set

class_variable_get

class_variable_defined?

 

constant_set

constant_get

constant_defined? #如果把false作为第二个参数传入,那么只在本类或者模块中查找

constant_missing

 

34.方法

methods

public_mithods

public_mithods false

protected_methods

private_methods

private_methods false

singleton_methods

 

instance_methods

instance_methods false

public_instance_methods

protected_instance_methods

private_instance_methods false

 

public_method_defined?

protected_method_defined?

private_method_defined?

method_defined?

respond_to?

 

方法调用:send--想对象发送一个消息,可以调用一个对象的任意有名方法,包括私有和保护的,第一个参数是方法名,后面的是该方法的参数

"hello".send :upcase
Math.send :sin, Math::PI/2

ruby1.9出现了public_send方法,区别是只调用公开方法

 

35.定义方法,取消方法,和别名方法

define_method # 是一个私有方法

def add_method c, m, &b
  c.class_eval {
    define_method m, &b
  }
end

add_method(String, :greet) { "hello, " + self }
"world".greet

定义单键方法在ruby1.9中可以使用define_singleton_method

 

alias plus +

alias_method

remove_method

undef_method

freeze

 

36.处理未定义方法

method_missing 强悍无比 const_missing

class Hash
  def method_missing key, *args
    text = key.to_s
    if text[-1,1] == "="
      self[text.chop.to_sym] = args[0]
    else
      self[key]
    end
  end
end

h = {}
h.one = 1
puts h.one

 

37.钩子方法

inherited

included

extended

method_added

singleton_method_added

method_removed

method_undefined

singleton_method_removed

singleton_method_undefined

 

38.跟踪

__FILE__

__LINE__

__method__

__callee__

SCRIPT_LINES__

 

kernel.trace_var来跟踪全局变量

 

39.ObjectSpace and GC

ObjectSpace.each_object(Class) {|c| puts c}
ObjectSpace.id2ref

ObjectSpace.garbge_collect强制ruby进行垃圾回收,或者GC.start

GC.disable

GC.enable

 

40.用同步代码块实现线程安全

两个线程不能同时修改同一个对象

解决问题方案一:将线程安全的代码放在调用一个Mutex对象的synchronize方法的代码块中,二是自实现模拟java中的synchronized关键字

 

41.动态创建方法

用class_eval创建方法

用define_method创建方法

 

42.领域特定语言 DSL

 

 43.网络编程

require 'socket'

host, port = ARGV

s = TCPSocket.open(host, port) do |s|
  while line = s.gets
  	puts line.chop
  end
 end
require 'socket'

server = TCPServer.open(2000)
loop {
  client = server.accept
  client.puts Time.now.ctime
  client.close
}

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值