woodstok woodstok - 1 year ago 45
Ruby Question

What exactly is the keyword "should" in RSpec Ruby

I am a beginner in Ruby and had this question nagging me for a long time.

In an RSpec file , if we write

Book.should <do something>
, what is the
keyword? Is it a member of the Book object? How did it come to be the member of the Book object? Or is it some construct of Ruby? Is it a function? Where can i find the definition of this if it is a function or member?

Answer Source

Upon loading, RSpec includes a module into the Kernel module which is included into all objects known to Ruby. Thus, it can make the should method available to all objects. As such, should is not a keyword (like if, class, or end) but an ordinary method.

Note that that mixin is only available in RSpec contexts as it is "patched in" during loading or RSpec.