Skip to content

Instantly share code, notes, and snippets.

@pmint93
Created December 5, 2014 00:46
Show Gist options
  • Save pmint93/789bf09a8c03c92f1215 to your computer and use it in GitHub Desktop.
Save pmint93/789bf09a8c03c92f1215 to your computer and use it in GitHub Desktop.
# Author: pmint93
# Exercise: https://www.facebook.com/groups/718037468264021/permalink/765003373567430/
class Greeter
def self.hello(file = '')
yield if defined? yield
eval open(file).read if File.exist? file
end
end
@runlevel5
Copy link

Another good one, but I think load is a better way to read and evaluate the content of a file. However in other cases, for example, if the usecase is to lazy-evalulate, I see many people practice following pattern:

content = Proc.new { eval(open(file).read) }

Besides, I am a fan of fail fast, so I think in your code, if you plan to guard against the missing params, better to expicitly raise exception to warn use of ArgumentError or EOENT error.

@pmint93
Copy link
Author

pmint93 commented Dec 5, 2014

I got it 😄 Thanks you very much 👍

@thachck
Copy link

thachck commented Dec 5, 2014

Some minor changes you may have a look 😄

class Greeter
  def self.hello(file = '')
    yield if block_given?
    eval File.read(file)
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment