Ruby 函数式对象编程

chenge · 2017年01月24日 · 1517 次阅读

今天看到这个视频Next-generation Ruby web apps with dry-rb, rom-rb, and Roda: RedDotRubyConf 2016和一些博客,作者介绍了如何引入一些函数思路,值得了解一下吧。

比较有趣的是用函数的方式来使用对象,一个对象只有一个方法,数据是分离的等等,很有新意吧。

class CreateArticle
  attr_reader :params

  def initialize(params = {})
    @params = params
  end

  def call
    result = ValidateArticle.call(params)

    if result.success?
      PersistArticle.call(params)
    end
  end
end

create_article = CreateArticle.new("title" => "Hello World")
create_article.call

Discover how concepts like functional programming, immutability, strict typing, dependency injection and object composition can actually be easy and natural in Ruby (yes, really!), and how they will make your web app a pleasure to build, test and extend.

暂无回复。
需要 登录 后方可回复, 如果你还没有账号请 注册新账号