1. Ruby / Говнокод #11684

    −157

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    class SomeModel < ActiveRecord::Base
      after_save :some_method
    
      def some_method
        self.save
      end
    
    end

    Рекурсивная рекурсия рекурсивна.

    mr.The, 31 Августа 2012

    Комментарии (2)
  2. Ruby / Говнокод #11643

    −160

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    <div class="comment-content">
      		<% if comment.body.include?('[/quote]') %>
      			<% @text = Array.new %>
      			<% summ = '' %>
      			<% @text = simple_format(comment.body).split(' ') %>
      			<% namecheck = false %>
      			<% contentcheck = false %>
      			<% quotestart = false %>
      			<% donothing = true %>
      			<% quotecount = 0 %>
      			<% count_quotes = 0 %>
      			<% @text.each do |t| %>
      				<% if t.include?('[/quote]') %>
      					<% count_quotes ||= 0 %>
      					<% count_quotes += 1 %>
      				<% end %>
      			<% end %>
      			<% @text.each do |t| %>
      				<% if t.include?('[/quote]') %>
      					<% contentcheck = false %>
      					<% namecheck = false %>
      					<% t = t.gsub('[/quote]', '</div></div>') %>
      				<% elsif t.include?('[quote=') %>
      					<% namecheck = true %>
      					<% donothing = false %>
      					<% t = t.gsub('[quote=', '') %>
      				<% elsif t.include?(']') %>
      					<% t = t.gsub(']', '') %>
      				<% end %>
      				<% if donothing == false %>
      					<% if contentcheck == true and namecheck == true and t != '' and t != '<p>' and t != '</p>' %>
      						<% t = '<div class=quotecontent>' + '<div class=quotestart>' + '<div class=quotename>' + 'Цитата - ' + t + '</div>' %>
      						<% contentcheck = true %>
      						<% namecheck = false %>
      					<% elsif contentcheck == true and t != '' and t != '<p>' and t != '</p>' %>
      						<% t = '<div class=quotecontent>' + t %>
      						<% contentcheck = false %>
      					<% elsif namecheck == true and t != '' and t != '<p>' and t != '</p>' %>
      						<% t = '<div class=quotestart>' + '<div class=quotename>' + 'Цитата - ' + t + '</div>' %>
      						<% namecheck = false %>
      						<% contentcheck = true %>
      					<% end %>
      				<% end %>
      				<% summ = summ + t + ' ' %>
      			<% end %>
      		<% else %>
      			<% summ = comment.body %>
      		<% end %>
        	<%=raw simple_format(summ) %>
        </div>

    Вьюха списка комментариев. Когда это попало к верстальщику - он был мягко говоря шокирован.

    ruby-code, 24 Августа 2012

    Комментарии (21)
  3. Ruby / Говнокод #11601

    −379

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    9. 9
    if city.present?
      if city.city.present?
        city = city.city
      else
        city = "NIL"
      end
    else
      city = "NIL"
    end

    Нечто

    bulka, 16 Августа 2012

    Комментарии (37)
  4. Ruby / Говнокод #10622

    −84

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    def self.allow_some_behaviour?
      # better safe than sorry.
      bool = !false ? !false : !!false
    
      # type checking
      if bool
    
        # short circuit just in case
        return !true;
      elsif !bool
        return !false
      end
    end

    найдено в реальном проекте, работало в течении года где то. зачем это сделано - непонятно. почему не выпилили, тоже неясно.

    malleus, 08 Июня 2012

    Комментарии (4)
  5. Ruby / Говнокод #10518

    −98

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    9. 9
    def confirmed
      if self.confirmation
        "Да"
      elsif !self.confirmation
          "Нет"
      else
          ""
      end
    end

    забавно:)

    emerald, 02 Июня 2012

    Комментарии (15)
  6. Ruby / Говнокод #9768

    −102

    1. 1
    Money.new(v[:amount].gsub('.','').to_i)

    а вы умеете так переводить баксы в центы?

    cancerogen, 26 Марта 2012

    Комментарии (5)
  7. Ruby / Говнокод #9493

    −100

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    # ищем позиции в гугле
       def search(request, keyword, p = nil)
          proxy = nil
       # инициализация прокси
          if p # обьект с прокси-серверами
             proxy = "http://#{p.proxy}/" # случайный прокси
             proxy_working = false 
             while not proxy_working
                begin
                   timeout(5) { open(request, :proxy => proxy) }
                rescue Timeout::Error
                   $log.put "bad broxy #{proxy}"
                   proxy = "http://#{p.proxy}/"
                   next
                rescue Errno::ECONNREFUSED
                   $log.put "bad broxy #{proxy}"
                   proxy = "http://#{p.proxy}/"
                   next
                rescue OpenURI::HTTPError
                   $log.put "bad broxy #{proxy}"
                   proxy = "http://#{p.proxy}/"
                   next
                else
                   $log.put "working #{proxy}"
                   proxy_working = true
                end
             end
          end
    # код

    baldrs, 20 Февраля 2012

    Комментарии (0)
  8. Ruby / Говнокод #9269

    −101

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    def actual?
        if self.from <= Date.today
          if self.to
            if self.to >= Date.today
              true
            else
              false
            end
          else
            true
          end
        else
          false
        end
      end

    метод в модели, проверяющий ее актуальность

    MikDiet, 31 Января 2012

    Комментарии (6)
  9. Ruby / Говнокод #9208

    −109

    1. 1
    monthes = ['Нулября', 'Января', 'Февраля', 'Марта', 'Апреля', 'Мая', 'Июня', 'Июля', 'Августа', 'Сентября', 'Октября', 'Ноября', 'Декабря']

    Вот, оказывается, как лечится, что индексы в массиве начинаются с нуля, а номера месяца с 1

    solenko, 24 Января 2012

    Комментарии (50)
  10. Ruby / Говнокод #9108

    −106

    1. 001
    2. 002
    3. 003
    4. 004
    5. 005
    6. 006
    7. 007
    8. 008
    9. 009
    10. 010
    11. 011
    12. 012
    13. 013
    14. 014
    15. 015
    16. 016
    17. 017
    18. 018
    19. 019
    20. 020
    21. 021
    22. 022
    23. 023
    24. 024
    25. 025
    26. 026
    27. 027
    28. 028
    29. 029
    30. 030
    31. 031
    32. 032
    33. 033
    34. 034
    35. 035
    36. 036
    37. 037
    38. 038
    39. 039
    40. 040
    41. 041
    42. 042
    43. 043
    44. 044
    45. 045
    46. 046
    47. 047
    48. 048
    49. 049
    50. 050
    51. 051
    52. 052
    53. 053
    54. 054
    55. 055
    56. 056
    57. 057
    58. 058
    59. 059
    60. 060
    61. 061
    62. 062
    63. 063
    64. 064
    65. 065
    66. 066
    67. 067
    68. 068
    69. 069
    70. 070
    71. 071
    72. 072
    73. 073
    74. 074
    75. 075
    76. 076
    77. 077
    78. 078
    79. 079
    80. 080
    81. 081
    82. 082
    83. 083
    84. 084
    85. 085
    86. 086
    87. 087
    88. 088
    89. 089
    90. 090
    91. 091
    92. 092
    93. 093
    94. 094
    95. 095
    96. 096
    97. 097
    98. 098
    99. 099
    100. 100
    def simpleEncrDecr( alphabet, alphabetKey, encrDecrWord, triger) #простой метод одну букву из алфавита заменяет другой
      alphabet = alphabet.chars.to_a
      alphabetKey = alphabetKey.chars.to_a
      encrDecrWord = encrDecrWord.chars.to_a
      if triger == 'encryption' then
        for i in 0..encrDecrWord.length-1
           encrDecrWord[i] = alphabetKey[alphabet.rindex(encrDecrWord[i])]
        end
      elsif triger == 'decryption'
        for i in 0..encrDecrWord.length-1
           encrDecrWord[i] = alphabet[alphabetKey.rindex(encrDecrWord[i])]
        end
      else
        p "You is looser! :-)"
      end
      encrDecrWord.join
    end
    
    def manyAlphEncrDecr( alphabet, alpKey, encrDecrWord, triger)  #для закодування нужное слово которое будет ключом
      alphabet = alphabet.chars.to_a
      encrDecrWord = encrDecrWord.chars.to_a
      if triger == 'encryption' then
        for i in 0..encrDecrWord.length-1
          sum = alphabet.rindex(encrDecrWord[i])+alphabet.rindex(alpKey[i])
          encrDecrWord[i] = alphabet[sum%alphabet.length]
        end
      elsif triger == 'decryption'
        for i in 0..encrDecrWord.length-1
           div = alphabet.rindex(encrDecrWord[i])-alphabet.rindex(alpKey[i])
           encrDecrWord[i] = alphabet[div]
        end
      else
        p "You is looser! :-)"
      end
      encrDecrWord.join
    end
    
    def permutationEncrDecr( permKeyNew, encrDecrWord, triger)  #меняет местами буквы в соответствии с ключом, 1234 <=> 3241
      encrDecrWord = encrDecrWord.chars.to_a
      encrDecrWordNew = Array.new
      if triger == 'encryption' then
        for i in 0..encrDecrWord.length-1
          encrDecrWordNew[i] = encrDecrWord[permKeyNew[i]]
        end
      elsif triger == 'decryption'
        for i in 0..encrDecrWord.length-1
           encrDecrWordNew[permKeyNew[i]] = encrDecrWord[i]
        end
      else
        p "You is looser! :-)"
      end
      encrDecrWordNew.join
    end
    
    def frequencyAnalysis(textForAnalis) #частотный анализ текста (подсчет букв)
      analisHash = Hash.new
      for i in 0..textForAnalis.length-1
        analisHash[textForAnalis[i]] = 0
      end
      for i in 0..textForAnalis.length-1
        analisHash[textForAnalis[i]] += 1
      end
      puts "Frequency Analysis - #{analisHash}"
    end
    
    p "Alphabet - #{alphabet = ('0'..'z').to_a.join}"
    print "Enter words for encryption/decryption:"
    wordForEncryped = gets.chomp
    print "Type method:\n1-simple\n2-many alphabetic\n3-permutation\n"
    method = gets.chomp
    case
      when method == '1' then
        puts "Key for alphabet - #{alphabetKey = alphabet.chars.to_a.shuffle.join}"
        puts "Simple Encryped - #{encrypedWord = simpleEncrDecr( alphabet, alphabetKey, wordForEncryped, 'encryption')}"
        frequencyAnalysis(encrypedWord)
        puts "Simple Decryped - #{simpleEncrDecr( alphabet, alphabetKey, encrypedWord, 'decryption')}"
      when method == '2' then
        print "Enter word for many alphabetical encryption/decryption:"
        wordForManyAlphKey = gets.chomp.chars.to_a
        manyAlphKey = Array.new
        for i in 0..wordForEncryped.length-1
          manyAlphKey[i] = wordForManyAlphKey[i%wordForManyAlphKey.length] 
        end
        puts "Many Alphabetic Encryped - #{encrWordForManyAlph = manyAlphEncrDecr( alphabet, manyAlphKey, wordForEncryped, 'encryption')}"
        frequencyAnalysis(encrWordForManyAlph)
        puts "Many Alphabetic Decryped - #{manyAlphEncrDecr( alphabet, manyAlphKey, encrWordForManyAlph, 'decryption')}"
      when method == '3' then
        print 'Please typed key(f.e. 23451):'
        permKey = gets.chomp.chars.to_a
        lengthForSum = permKey.length
        newPermKey = Array.new
        for i in 0..wordForEncryped.length-1
          lengthForSum += permKey.length if i==lengthForSum
          newPermKey[i] = permKey[i%permKey.length].to_i + lengthForSum - permKey.length-1
        end
        puts "Permutation Encryped - #{encrypedWordForPrem = permutationEncrDecr( newPermKey, wordForEncryped, 'encryption')}"
        frequencyAnalysis(encrypedWordForPrem)
        puts "Permutation Decryped - #{permutationEncrDecr( newPermKey, encrypedWordForPrem, 'decryption')}"
      else p 'Incorect typed'
    end

    Методьі кодирования
    1. простой метод одну букву из алфавита заменяет другой
    2. для закодування нужное слово которое будет ключом
    3. меняет местами буквы в соответствии с ключом, 1234 <=> 3241
    4. частотный анализ текста (подсчет букв)
    у каждого метода кроме 4 есть триггер - выбор в каком режиме метод будет работать (кодирование или декодирование)

    bartezic, 13 Января 2012

    Комментарии (7)