The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Hidden Ruby Language Features

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Chad Fowler

Posts: 408
Nickname: chadfowler
Registered: Apr, 2003

A programmer, musician, and language addict.
Hidden Ruby Language Features Posted: Apr 13, 2004 4:51 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Chad Fowler.
Original Post: Hidden Ruby Language Features
Feed Title: ChadFowler.com
Feed URL: http://feeds2.feedburner.com/Chadfowlercom
Feed Description: Best practices, worst practices, and some generally obvious stuff about programming.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Chad Fowler
Latest Posts From ChadFowler.com

Advertisement
Brian Marick writes about a little-known feature of the Ruby language: the ability to automatically mock classes for testing at runtime. There is a keyword called "mock" that you can use to do it. For example, if you want to temporarily replace the behavior of a class called MyDatabaseConnector for a test, you would use the following code:
  mock MyDatabaseConnector
  class ClassToMockWith
    ..code...
  end

Whatever class definition directly succeeds the invocation of "mock" will be temporarily substituted for the class specified as an argument to "mock". Specifically, in this case, any references to MyDatabaseConnector will be replaced with references to ClassToMockWith, so instantiating MyDatabaseConnector actually instantiates ClassToMockWith, and so on.

Rubyists reading this are probably thinking I’ve lost my mind, because this isn’t really a feature of the Ruby language. It’s just a simple (albeit powerful) extension that Brian has written. But, to someone coming from a history of work exclusively done in in one or more of the more static languages (Java, C#, etc.), it looks like a first-class part of the language.

This is what Guy Steele was talking about in his Growing a Language OOPSLA talk.

Read: Hidden Ruby Language Features

Topic: Pragmatic Starter Kit Jolt Award Previous Topic   Next Topic Topic: In C

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use