In Sinatra application I have a code
gef '/123' do
@var1 = # some calculations
@var2 = # some calculations
#..... many of them
haml(:"view123")
end
and all of the instance variables are used in view123. I wonder, will there be any benefit of using the hash instead of many variables like this:
gef '/123' do
hash_var = {}
hash_var[:var1] = # some calculations
hash_var[:var2] = # some calculations
#..... many of them
haml(:"view123")
end
I think you are on the right track, and it’s hard to advise in this very abstract sense, but in general that
# some calculationslooks like business logic. And business logic belongs in models.How about a model instead? Make a new class that performs these calculations, then instantiate it and pass the instance to your view. Your controller/route handler remains lean and clean, and many many lines of calculations are all tucked into a file dedicated to just that thing. Neat and tidy.
view123.erb: