views:

56

answers:

2

A gem/plugin that I'm using to support my test suite calls some GNU commands that cause the Windows shell to roll over and die. I can rewrite these functions with a bypass in this fashion:

def get_rake_output(task)
  if RUBY_PLATFORM.include? 'mingw'
    puts 'Skipped since OS is Windows. Run the test suite on a UNIX-like shell.'
    ''
  else
    `./rake --silent #{task}`
  end
end

Of course, this prints the message every time the function is called. How do I best make sure that it displays only once?

+3  A: 

It's method in ruby.</rant>. Use a class variable like @@warning_said and check for that one. Aka

puts "Ruby does not like Windows here" unless @@warning_said
@@warning_said = true

I'm not exaclty sure what scope you are operating in, but that should do it.

Tass
This stands to reason. Thanks! I was initially using global variables, but this makes more sense. Of course, the class variable needs to be initialized.
Steven Xu
+3  A: 

the idomatic ruby way to handle this is to memoize it

@@warning_said ||= "warning"
Jed Schneider
+1 for mentioning the word memoize.
Andrew Grimm