G
gmalicki
These two snippits of code both do the same thing. One of them uses
reflection and one does not. The one that uses reflection is a line
shorter so to me it feels like a better choice. In the #ruby-lang IRC
channel people told me to "use reflection only when you have to". Is
that a sane general rule to stick with for Ruby programming ? Is there
anything wrong with using reflection in this case ? Are there any
upsides or downsides to either approach ?
# NIL LOCAL VAR TECHNIQUE
query = nil
query = "foo" if config[:scope] == age
query = "bar" if config[:scope] == :domain
raise BrokenConfig if query.nil?
# REFLECTION TECHNIQUE
query = "foo" if config[:scope] == age
query = "bar" if config[:scope] == :domain
raise BrokenConfig unless Kernel.local_variables.member?( "query" )
thanks
reflection and one does not. The one that uses reflection is a line
shorter so to me it feels like a better choice. In the #ruby-lang IRC
channel people told me to "use reflection only when you have to". Is
that a sane general rule to stick with for Ruby programming ? Is there
anything wrong with using reflection in this case ? Are there any
upsides or downsides to either approach ?
# NIL LOCAL VAR TECHNIQUE
query = nil
query = "foo" if config[:scope] == age
query = "bar" if config[:scope] == :domain
raise BrokenConfig if query.nil?
# REFLECTION TECHNIQUE
query = "foo" if config[:scope] == age
query = "bar" if config[:scope] == :domain
raise BrokenConfig unless Kernel.local_variables.member?( "query" )
thanks