“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
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
I know. FizzBuzz has been done to death. But I want to use it as a familiar base upon which we can explore some of the common tradeoffs involved in writing and maintaining software. In this article, I’ll show multiple implementations of FizzBuzz, all designed to achieve different goals, and discuss the implications of each. What is FizzBuzz? Hey guys, have you heard about this new FizzBuzz thing?
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ページを開く