ASCII Addition in Ruby


/ Published in: Ruby
Save to your folder(s)

This was an absolute failure. But here it is, if anyone would like to see horrible programming.


Copy this code and paste it in your HTML
  1. class Asciiarray
  2. attr_accessor :a, :b
  3. def initialize(a = "Hello", b = "World")
  4. @a = a
  5. @b = b
  6. end
  7.  
  8. def create
  9. isa = false
  10. equal = false
  11.  
  12. if @a.length > @b.length
  13. c = @a
  14. d = @b.rjust(@a.length)
  15. isa = true
  16. displacement = @a.length - @b.length
  17. elsif @b.length > @a.length
  18. c = @a.rjust(@b.length)
  19. d = @b
  20. displacement = @b.length - @a.length
  21. elsif @a.length == @b.length
  22. c = @a.rjust(@b.length)
  23. d = @b
  24. equal = true
  25. else
  26. puts "Error. Please try again."
  27. end
  28.  
  29. @a_array = c.split(//)
  30. @b_array = d.split(//)
  31.  
  32. @a_array.length.times do |iterate|
  33. q = @a_array[iterate-1]
  34. @a_array[iterate-1] = ?q
  35. end
  36.  
  37. @b_array.length.times do |iterate|
  38. x = @b_array[iterate-1]
  39. @b_array[iterate-1] = ?x
  40. end
  41.  
  42. if equal == false
  43. if isa == true
  44. displacement.times do |takeout|
  45. @a_array[takeout-1] = 0
  46. end
  47. else
  48. displacement.times do |takeout|
  49. @b_array[takeout-1] = 0
  50. end
  51. end
  52. end
  53. end
  54.  
  55. def operations
  56. @c_array = Array.new
  57. @a_array.length.times do |iterate|
  58. @c_array[iterate] = @a_array[iterate-1] + @b_array[iterate-1]
  59. end
  60. end
  61.  
  62. def reverseconvert
  63. @c_array.length.times do |iterate|
  64. if @c_array[iterate-1] > 127
  65. @c_array[iterate-1] = @c_array[iterate-1] % 127
  66. @c_array[iterate-1] = @c_array[iterate-1].chr
  67. elsif @c_array[iterate-1] < 32
  68. @c_array[iterate-1] = 32.chr
  69. else
  70. @c_array[iterate-1] = @c_array[iterate-1].chr
  71. end
  72. end
  73. end
  74.  
  75. def printout
  76. lol = @c_array.join
  77. puts lol
  78. end
  79. end
  80.  
  81. i = Asciiarray.new("HI I LIKE PIE", "ANDREW")
  82. i.create
  83. i.operations
  84. i.reverseconvert
  85. i.printout

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.