Created
December 5, 2014 00:46
-
-
Save pmint93/789bf09a8c03c92f1215 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
I got it 😄 Thanks you very much 👍
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
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: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.