yea that's exactly what i was thinking.. thanks @Mirv
though technically the question is broader than album/images it can be any other system logic that requires a hard cap but can be vulnerable to concurrency
example: spinning up a number of user accounts etc (which might be a lengthy process)
@JörgWMittag Given your comment here, would you write balanced_loop?here using loop do; break unless ... ; end, use something else I can't think of, or would you say it is a good use of while as an exception outside those 99.999%? (Not trolling, really curious what you think.)
... i'm not really to that level of mass use, but having a user try to upload, assign a caption etc & then telling them it fails, delete something & start over from scratch is a sure fire way to ensure your business fails.
Either before the client side UI allows an update - call a check to the server for their limit or go with drafting type solutions rather than risk the user never returns to your site.
@Amadan ... you might have a long wait ... last message: 474d ago, last seen: 65d ago
Weirdness of the day ...
@holders = ["timeGreeting", "firstName"]
@holders.map{ |k| puts k.to_sym } # fails string still
@holders.map{ |k| puts "#{k} - to_sym: #{k.to_sym}" } # strings for both
@holders.map{ |k| puts "#{k} - to_sym: #{k.to_s.to_sym}" } # strings for both
@holders.map(&:to_sym) # works => [:timeGreeting, :firstName]
Per S/O answer all of them should work - stackoverflow.com/a/14442463/4289112 - I was looking into it & at first I thought it had something to do with Array versus String #to_sym methods - but there…
Design question - I've thus far been passing around some massive hashes - should I start using delete to pull values from `args` & remove them as I go or just copy out what I need & let `args` die after initialization? ... there are times when I need to pass a majority of the hash that's left over after removing a few things ... like this ...
def initialize(holders, args)
@holders = holders
@deliminator = args.delete(:deliminator)
@raw = args.delete(:raw_template)
@parameters = args
@Mirv I vote to start creating objects rather than throwing around massive hashes.
But snark aside, I'd set discrete instance variables as you have done. I'd probably start with args = args.dup, to avoid modifying the passed-in argument.
Oh it's totally valid - just I'm pushing 50 classes for a template greeting system already & the need to test each of them as requirement is pretty heavy lifting versus just happily slinging hashes
I kind of went with another option ... think this is too verbose?
def fill_out_template_placeholders
start = @deliminator.start
stop = @deliminator.stop
temp_data = match_placeholder_to_data
# byebug
data.each do |x,y|
replacement = y.to_s
placeholder = start+x.to_s+stop
@raw.gsub!(placeholder, replacement)}
end
I almost want to write a block to format the deliminators with the x value
Also, I really like the discrete instance variables - as they made factoring out 3 or 4 classes take less than 2 or 3 minutes each (not counting test writing, but just actually changing the calling class code)
the only advantage in the assignment is it lets the ruby engine optimize variables better - i found that out when I was doing that multi-thread test thing earlier this year with robert or whoever
my version ran many times faster than his when I had a single line assignment & doing more than 2k entries
yea, i just wrapped the calls with a block & i even tested with the baked in time & also used timestamp diff'ing then ran the same calls - the compiler or the server naturally is optimized for the 2nd run to be at least twice as fast ... I'm not saying that I had every assignment saved per say...i'll see if i can find that example
I'm not sure this is up to date - but this was the original and spun off two others one for a self naming file logging system & another so i wouldn't pollute his project ... github.com/Mirv/asynchronize/branches
The other one that I did way more with take some digging
Project link - github.com/Mirv/AutoSeed
This one is twice to 4 times faster ...
models.each do | model_name |
model = model_name.classify.constantize # setup model to call
attribs = model.column_names - reject_attribs # drop extra db columns
attribs = attribs.map {|x| [x, insert_string]}.to_h # setup hash of columns
result = model.find_or_create_by!(attribs) # save error msgs
puts result # output results
This was the original - it runs about 20-30% faster 2nd time - but half to four times less than optimized version above
# Print the model creation method
puts "[{" + model_name.classify.constantize.all.map { |model|
model.attributes.reject{ |key| key == "id" }.map { |key, attribute|
key == "id" ? nil : "#{key}: '#{attribute}'"
}.join(", ")
}.join("},\n{")
+ "}].each { |attributes| \n"
+ model_name + ".find_or_create_by(attributes) }"
shrug - i noticed the one autoseed is rails probably 4 or 5 & my rubies range from 1.9.3 to 2.5.4 - there's 120 log files in one of my working directories & i'd say half of them are from 1.9.3
(my counts when i'm estimating are that I created over 200million models to test)
Here's the latest - nonworking branch when i abandoned the project to focus on something more bloglike - ... the file I'm linking here is the time testing part - link
I know for sure the 1st version is horribly slower when compared to the next 2, I was in the process of making the file logging better so it would change names and allow me to harvest the data in more meaningful ways before i looked into how much it cost to create an instance variable versus to call a bunch of methods
I do not understand your emphasis on optimization. When writing web apps, or most other apps, the CPU is not the bottleneck, even with a slow language like Ruby. Most of my Ruby programs are I/O bound.
Curiosity - those are just tooling projects for motivation to learn new stuff like one was multi-threading. My order of preference is readability - which you can clearly see the first one is easier - it also happens to be 4 times faster ... for IO when interaction with db. Speed is really a 3rd tier concern for me, but it doesn't mean I don't ask myself "what's up yo?"
So my greeting module is complete. 20 files, 4 data files. 20 test files with 4 helper files. I need to write some more tests for edge cases on final results, then scrub todo's and notes along with put all the highlights into the readme.
I have a general idea of what my app does ... but I also don't feel like it's super well constructed ... from a finished product end ... I expect my tests to be, "insert a, b, c, x, y" & get "z" ... where as instead with bdd - I have this chain of data in/out checks + error behavior that lets me refactor really well, but i'm not sure my tests have proper coverage etc
Also, because this loads from files - I have very little control beyond mocking of inserting information to the processing portion of the app
...like I have all these unit tests, but not even sure I can integration check the ruby only project...do people do integration checks of non-rails ruby projects?
There are some ways of testing code that uses files. Would you be interested in them?
I rarely do full-on testing of any code. It's very expensive and can be fragile.
I did do integration tests for my Basic interpreter, however. Those were pretty easy, since the interpreter reads a file and writes a bunch of stuff to stdout. That's very easy to test.
Yea - I pulled some file testing off S/O already & wrote custom location captures via rescue - I don't have a logger injected yet, but am doing puts (error_msg) or exit(msg) currently - I'm using that rescue Errno::ENOENT ... S/O seems to agree there is no way to test json#parse you just call it & see if you can pull keys out of it
Alright wayne - you're going to be proud ... one of the hashes I was passing around .. two variables only ... made into an object & my testing is so much easier now .... deliminator: Deliminators.new('{','}')
So I figure anywhere I have two tests checking the presence of the same data - is a new class i need to write & it will allow me to reduce my code - then in the future anytime I write a copycat test - I know to just make it an object...?
Should I be including a map off all my classes and how they chain somewhere?
Documentation, you mean? Code docs have to be maintained, and typically aren't, but sometimes a broad-strokes outline of how the project is organized can be useful.
Do you guys think time & timezone belong in an object together?
Also ... what do you think of doing this? ...
class TimeToGreeting
def initialize(args)
@parameters = args
default_greetings = args[:default_greetings]
@hours = retrieve_hours(args)
@greetings = default_greetings || greetings_file_load
end
def retrieve_hours(args)
hours_args = Hash.new
hours_args.merge!(timezone: args.delete(:timezone))
hours_args.merge!(startTimeStamp: args.delete(:startTimeStamp))
TimeInHours.new(hours_args).time_in_hours
end
Or part of me thinks somehow - at the top level above this class dependency inject TimeInHours? I have trouble envisioning that - as should the top level command line interface call every single class that will be made into an object? Should it be a midlevel thing where that particular task spits out like what I have above?