- 1
- 2
- 3
and is the same as && but with lower precedence. They both use short-circuit evaluation.
WARNING: and even has lower precedence than = so you'll want to avoid and always
Нашли или выдавили из себя код, который нельзя назвать нормальным, на который без улыбки не взглянешь? Не торопитесь его удалять или рефакторить, — запостите его на говнокод.ру, посмеёмся вместе!
0
and is the same as && but with lower precedence. They both use short-circuit evaluation.
WARNING: and even has lower precedence than = so you'll want to avoid and always
0
def with_batched_ids(count, ids)
if ids.is_a? Array
if ids.length > 1
ids.each_slice(count) do |ids|
yield(ids)
end
else
yield(ids)
end
else
yield(ids)
end
end
Заshitное программирование и просто красивая портянка из yield - end - else. Как на ровном месте придумать целый метод.
0
Руби говно на всей Земле
Руби говно другим во благо
Не за красивое "спасибо"
А за хорошее бабло
0
https://github.com/ruby/ruby/blob/trunk/parse.y
Как распарсить Руби код.
0
def gsubn text
text.gsub('\n','').gsub('\t','').gsub('\r','').gsub(/\"/,'').gsub('@@@@','. ').gsub(" "," ")
end
последний gsub меняет два пробела на один
0
def self.allowed_symbols
@allowed_symbols ||= (('A'..'Z').to_a + (0..9).to_a).shuffle
end
def self.forbidden_symbols_regexp
@allowed_symbols_regexp ||= Regexp.new("[^#{allowed_symbols.join}]")
end
Проверка формата промокода
0
Net::HTTPServerException: 404 "Not Found"
сервер виноват™
0
(params[:date_lte].present? && params[:date_gte].present?) ?
@events = CalendarEvent.where(:publication_date.gte => DateTime.parse(params[:date_gte]),
:publication_date.lte => DateTime.parse(params[:date_lte]) + 23.hours + 59.minutes) :
@events = CalendarEvent.order(publication_date: :desc).limit(6)
Божественное использование тернарного оператора
0
def check_web_property(file_folder_id)
folder_for_updated_files = FileFolder.find file_folder_id
# min_to_be_active = folder_for_updated_files.min_properties_to_be_active
process_uniq_id = SecureRandom.urlsafe_base64(30)
min_web_property = folder_for_updated_files.min_web_property_check
max_web_property = folder_for_updated_files.max_web_property_check
#puts "update ad_groups with given inventory limit to Previus Inventory = nil "
self.ad_groups.and( { "Inventory" => {'$gte' => folder_for_updated_files.min_web_property_check}}, {"Inventory" =>{'$lte' => folder_for_updated_files.max_web_property_check}}).update_all( "Previus Inventory" => nil, :update_complited => "no")
#PpcBuild.last.ad_groups.and( { "Previus Inventory" =>{'$gte' => 1}}, {"Previus Inventory" =>{'$lte' => 8 } }).count
puts "select ad_groups with given inventory limit"
selrcted_ad_groups = self.ad_groups.and( { "Inventory" =>{'$gte' => folder_for_updated_files.min_web_property_check}}, {"Inventory" =>{'$lte' => folder_for_updated_files.max_web_property_check } })
count_selected_ad_groups_for_processing = selrcted_ad_groups.count
selrcted_ad_groups.no_timeout.each do |ad|
if !self.uniq_urls.where("Landing Page"=> ad["Landing Page"], process_id: process_uniq_id).exists?
new_url = UniqUrl.new("Landing Page"=> ad["Landing Page"], process_id: process_uniq_id, ppc_build_id: self.id)
new_url.save
end
end
all_uniq_url_selected = self.uniq_urls.where(process_id: process_uniq_id)
elemetns_count = 0
all_uniq_url_selected.no_timeout.each do |uniq_url|
ad = AdGroup.find_by("Landing Page" => uniq_url["Landing Page"], ppc_build_id: self.id)
ad_parameters = { "Landing Page" => uniq_url["Landing Page"], ppc_build_id: self.id.to_s }
UpdateDataFromWebSite.perform_async(ad_parameters, process_uniq_id)
min_to_be_active = ad.min_for_ad_group_to_be_active
elemetns_count +=1
ad.web_update_inventory(ad.id.to_s, process_uniq_id, min_to_be_active)
if elemetns_count == 20
sleep 0
elemetns_count = 0
end
ad.update_attributes("Previus Inventory" => ad["Inventory"])
ad.push(process_id: process_id)
url = ad["Landing Page"]
uri = URI(url)
use_ssl = true
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = use_ssl
http.read_timeout = 20
http.open_timeout = 20
resp = http.start() { |http| http.get(uri.path) }
body = resp.body
page = Nokogiri::HTML(body)
if !page.css('.results-count').empty?
new_property = page.css('.results-count').first.text
property_number = new_property[/(?<=of\s)?\d{1,}\D?\d?{1,}/]
if property_number
ad.update_attributes("Inventory" => new_property.gsub(/\s+/, ' ')[/(?<=of\s)?\d{1,}\D?\d?{1,}/].strip)
else
ad.update_attributes("Inventory" => "0")
end
else
ad.update_attributes("Inventory" => "0")
end
ad.update_attributes("Status" => get_status(ad["Inventory"], min_to_be_active), update_complited: "yes")
end
Пример метода из ruby класса содержащего 890 строк
0
def rotate_array(arr, i)
i = i%arr.size
return arr if i == 0
left = arr[0...i].reverse
right = arr[i..arr.size].reverse
arr = left + right
arr.reverse
end
https://knaidu.gitbooks.io/problem-solving/content/arrays/rotate_array.html
вообще не понял зачем reverse
http://ideone.com/9fVLsq