“Prefer composition over inheritance” is a popular saying amongst programmers. In this article, I want to demonstrate what that looks like. We’re going to refactor some Rails classes from inheritance to composition. Then we’ll refactor again from composition to something even better: data. Inheritance (The Original Code) In an attempt to show “real” code, I’ve taken this example from the Rails cod
def register_new_user(params) new_user = User.new(params) authorize! :create, new_user new_user.save! send_welcome_email(new_user) end Even after reading the implementation, it’s still difficult to know for sure. Some of the possible exceptions would be obvious to a Rails developer, and you can probably guess which methods could throw other exceptions, but exceptions could be raised by any method
Continuing the from the previous post, here is a cheat sheet for RSpec’s assertion library: RSpec::Expectations. RSpec is a mature, feature-packed testing framework, but the documentation can be difficult to navigate. As an alternative to the official documentation, this cheat sheet contains short example code to demonstrate all the built-in expectation matchers. These examples were created with R
template = RXT::Template.from_file('weather.rxt') puts template.render( time: Time.now, description: 'Bright & sunny.', temp: 18.3, wind_vel: 14, wind_direction: 'SSE', ) I’ve given this Ruby XML templating language a spicy, exotic name: Ruby XML Template (RXT). I know, XML doesn’t make for the most exciting DSL, and the usefulness of this particular DSL is questionable. But it will do nicely for
After creating cheat sheets for RSpec::Core and RSpec::Expectations, I think it is only fair to have a cheat sheet for Minitest too. Minitest also provides an optional RSpec-like DSL, which is not covered here. Minitest is a small (some might say “mini”) testing framework, where tests are written in “plain Ruby,” as opposed to using a custom DSL. It has a gentler learning curve than RSpec, and is
Come with me on a journey through the internals of Rails, as we trace a request from the web server to the controller action method, and follow the response back again. This will give you a glimpse of how Rails works under the hood, hopefully exposing some of the “magic” of the framework. We will also get to see some of the design/architectural decisions at the core of Rails. This analysis was con
In Ruby, all functions are methods, so the terms “function” and “method” are interchangeable in this article. In this article, I want to demonstrate a nice way to write functional-style code in Ruby. It is a way to write non-trivial pure functions, without a bunch of weird non-idiomatic code. I’ll start by defining what pure functions are, explaining their benefits, and then walk through an exampl
RSpec is a mature, feature-packed testing framework, but the documentation can be difficult to navigate. The official documentation is written as a set of Cucumber tests within Relish. It’s very comprehensive, but also quite sparse, and Cucumber tests are kind of unnatural to read. When I forget how to do something in RSpec, I usually just look for an example in a test I’ve written previously, so
This article is about incorporating functional programming concepts into Ruby code, in a pragmatic way – something I call “functional style.” And when I say pragmatic, what I mean is that the code should still mostly look and feel like idiomatic Ruby. Ruby is not Haskell, and nor should it be. The idea is to work with the grain of the language, not against it. The code should be easy for any Rubyi
Shared mutable state is the source of a lot of bugs. When two or more objects use the same piece of mutable data, they all have the ability to break each other in ways that can be hard to debug. If the shared data is immutable, however, these objects can not affect each other, and are effectively decoupled. This article is a review of the options available to Rubyists regarding immutability. We’ll
Almost every web app requires a sign in form. In Ruby, the popular choice is the Devise gem for Rails. Devise gives you a lot of functionality out of the box, but it can also be frustrating. It has a lot of “magic,” and is heavily coupled to various parts of Rails. If you’re requirements don’t exactly fit the gem – for example, anonymous users – you will be wading through documentation, how-to’s,
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く