The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Ruby Stub Variations: Struct

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
Jay Fields

Posts: 765
Nickname: jayfields
Registered: Sep, 2006

Jay Fields is a software developer for ThoughtWorks
Ruby Stub Variations: Struct Posted: Sep 14, 2006 2:22 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Jay Fields.
Original Post: Ruby Stub Variations: Struct
Feed Title: Jay Fields Thoughts
Feed URL: http://blog.jayfields.com/rss.xml
Feed Description: Thoughts on Software Development
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Jay Fields
Latest Posts From Jay Fields Thoughts

Advertisement
Ruby Stub Variations: Introduction
Ruby Stub Variations: OpenStruct
Ruby Stub Variations: TestStub
Ruby Stub Variations: Struct

In my last entry I described how TestStub provided a concise syntax for defining stubs on the fly. However, I always consider it a win when I can remove code from the codebase without removing functionality.

Ruby provides a Struct class that allows you to define classes on the fly. From the Ruby documentation:
A Struct is a convenient way to bundle a number of attributes together, using accessor methods, without having to write an explicit class.
Struct also provides a one line solution to the example test.
def test_values_are_appened_to_insert_statement
statement = Insert.into[:table_name].values do
Struct.new(:to_sql).new('select column1, column2 from table2')
end
assert_equal "insert into table_name select column1, column2 from table2", statement.to_sql
end
The syntax of Struct doesn't seem as natural since the proliferation of using hashes as parameters, but Struct does provide the flexability of creating a new class and initializing at a later time. Struct works well and does not suffer from any of the limitations of the other examples.

Read: Ruby Stub Variations: Struct

Topic: Optimizing method calls: Ruby and Strongtalk Previous Topic   Next Topic Topic: Ruby Stub Variations: TestStub

Sponsored Links



Google
  Web Artima.com   

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