Ruby 支持两种对象化了的方法:Bound 和 Unbound。 Bound方法是指和一个特定对象绑定起来的方法对象。 Unbound方法自然就是没有被绑定的嘛。╮( ̄▽ ̄)╭ Unbound方法可以通过 instance_method 来创建,或者令bound方法 unbind 。 Unbound方法只能在绑定后才能被调用,而且必须绑定在一个 和这个方法被解绑下来的类(对象)的实例 上(只要这个实例是 kind_of? 这个类就行)。
class Square
def area
@side * @side
end
def initialize(side)
@side = side
end
end
area_un = Square.instance_method(:area)
s = Square.new(12)
area_un.bind(s).call #=> 144
Sinatra里面大概就是这样用的,当路径不同时,绑定不同的方法在get,post上面。 class « self这段代码在Sinatra源码里面有,高端又神秘。其实就是用来为 self 写一些singleton方法。我们首先不要管self,当作一个不同的object好了,看用法:
a = 'foo'
class << a
def inspect
'"bar"'
end
end
a.inspect # => "bar"
a = 'foo' # new object, new singleton class
a.inspect # => "foo"
所以 class << self 也就不难理解了:
class String
class << self
def value_of obj
obj.to_s
end
end
end
String.value_of 42 # => "42"
和下面两种方式是一样的:
class String
def self.value_of obj
obj.to_s
end
end
或者: def String.value_of obj obj.to_s end RackRack在Ruby的世界里面是一种构建服务端的最基本,但也是非常有意思的方式。 而且理解Rack也是理解Sinatra的基础,毕竟Sinatra是在Rack的基础上写出来的。 我们可以只用Rack搭建一个简单的后端程序。 直接上实例,代码保存到 config.ru ,然后在该文件夹下执行 rackup ,默认会在 http://localhost:9292 下显示。 非常建议好好研究一下这段代码,对于理解Sinatra和整个Ruby环境都非常有帮助。
class Application
def call(env)
handle_request(env['REQUEST_METHOD'], env['REQUEST_PATH'])
end
private
def handle_request(method, path)
if method == "GET"
get(path)
else
method_not_allowed(method)
end
end
def get(path)
[200, { "Content-Type" => "text/html" }, ["You have requested the path #{path}, using GET"]]
end
def method_not_allowed(method)
[405, {}, ["Method not allowed: #{method}"]]
end
end
run Application.new
(责任编辑:好模板) |


ecshop仿京东2012最新模板
人气:4646
ecshop仿Vjia商城2014网店模板
人气:948
ecshop也买酒2012最新模板
人气:2991
玉器商城|ecshop玉器模板
人气:641
淘宝客|返利网|易购51模板
人气:13549
PrestaShop外贸牛奶蛋糕甜点
人气:264