Tag: Ruby

Monty Hall Problem in Ruby

Just for fun, the monty hall problem in ruby. Runs simulation one million times. def monty_hall(switch) results = {switching: switch, wins: 0, losses: 0, count: 0, win_percentage: 0} (10**6).times.each do |n| prizes = [0,0,1].shuffle initial_selection = (0..2).to_a.sample other_doors = (0..2).to_a – [initial_selection] goat = other_doors.select{|n| prizes[n]==0}.sample available_selections = [initial_selection, (0..2).to_a – [initial_selection, goat]].flatten if switch […]

Rails select method with default value

When using the select form builder method in rails, for instance: f.select In conjunction with the options_from_collection_for_select method, like f.select :field, options_from_collection_for_select(@fields, ‘id’, ‘name’) Remember, there is a fourth argument in the above method, which defines the selected value, like so f.select :field, options_from_collection_for_select(@fields, ‘id’, ‘name’, @object.field)

Keep heroku from falling asleep / idling (even with PHP)

Ruby on Rails Scenario The easiest way to keep a Rails App from idling is to use New Relic Standard (free), in conjunction with their availability monitoring. You can do this in a couple of ways: Issue the following command with the Heroku Toolkit heroku addons:add newrelic:stark Or, login to your heroku account and provision […]