P06 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.coveralls.yml +1 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Guardfile +82 -0
- data/LICENSE.txt +21 -0
- data/P06.gemspec +42 -0
- data/README.md +5 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/docs/Alimento.html +871 -0
- data/docs/Alimento_concreto.html +459 -0
- data/docs/Array.html +316 -0
- data/docs/List.html +1135 -0
- data/docs/Nodo.html +410 -0
- data/docs/P06.html +117 -0
- data/docs/_index.html +156 -0
- data/docs/class_list.html +51 -0
- data/docs/css/common.css +1 -0
- data/docs/css/full_list.css +58 -0
- data/docs/css/style.css +492 -0
- data/docs/file.README.html +79 -0
- data/docs/file_list.html +56 -0
- data/docs/frames.html +17 -0
- data/docs/index.html +79 -0
- data/docs/js/app.js +248 -0
- data/docs/js/full_list.js +216 -0
- data/docs/js/jquery.js +4 -0
- data/docs/method_list.html +299 -0
- data/docs/top-level-namespace.html +112 -0
- data/lib/P06.rb +23 -0
- data/lib/P06/DSL.rb +168 -0
- data/lib/P06/alimento.rb +53 -0
- data/lib/P06/alimento_concreto.rb +64 -0
- data/lib/P06/list.rb +162 -0
- data/lib/P06/version.rb +3 -0
- metadata +183 -0
| @@ -0,0 +1,112 @@ | |
| 1 | 
            +
            <!DOCTYPE html>
         | 
| 2 | 
            +
            <html>
         | 
| 3 | 
            +
              <head>
         | 
| 4 | 
            +
                <meta charset="utf-8">
         | 
| 5 | 
            +
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
         | 
| 6 | 
            +
            <title>
         | 
| 7 | 
            +
              Top Level Namespace
         | 
| 8 | 
            +
              
         | 
| 9 | 
            +
                — Documentation by YARD 0.9.9
         | 
| 10 | 
            +
              
         | 
| 11 | 
            +
            </title>
         | 
| 12 | 
            +
             | 
| 13 | 
            +
              <link rel="stylesheet" href="css/style.css" type="text/css" charset="utf-8" />
         | 
| 14 | 
            +
             | 
| 15 | 
            +
              <link rel="stylesheet" href="css/common.css" type="text/css" charset="utf-8" />
         | 
| 16 | 
            +
             | 
| 17 | 
            +
            <script type="text/javascript" charset="utf-8">
         | 
| 18 | 
            +
              pathId = "";
         | 
| 19 | 
            +
              relpath = '';
         | 
| 20 | 
            +
            </script>
         | 
| 21 | 
            +
             | 
| 22 | 
            +
             | 
| 23 | 
            +
              <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
         | 
| 24 | 
            +
             | 
| 25 | 
            +
              <script type="text/javascript" charset="utf-8" src="js/app.js"></script>
         | 
| 26 | 
            +
             | 
| 27 | 
            +
             | 
| 28 | 
            +
              </head>
         | 
| 29 | 
            +
              <body>
         | 
| 30 | 
            +
                <div class="nav_wrap">
         | 
| 31 | 
            +
                  <iframe id="nav" src="class_list.html?1"></iframe>
         | 
| 32 | 
            +
                  <div id="resizer"></div>
         | 
| 33 | 
            +
                </div>
         | 
| 34 | 
            +
             | 
| 35 | 
            +
                <div id="main" tabindex="-1">
         | 
| 36 | 
            +
                  <div id="header">
         | 
| 37 | 
            +
                    <div id="menu">
         | 
| 38 | 
            +
              
         | 
| 39 | 
            +
                <a href="_index.html">Index</a> »
         | 
| 40 | 
            +
                
         | 
| 41 | 
            +
                
         | 
| 42 | 
            +
                <span class="title">Top Level Namespace</span>
         | 
| 43 | 
            +
              
         | 
| 44 | 
            +
            </div>
         | 
| 45 | 
            +
             | 
| 46 | 
            +
                    <div id="search">
         | 
| 47 | 
            +
              
         | 
| 48 | 
            +
                <a class="full_list_link" id="class_list_link"
         | 
| 49 | 
            +
                    href="class_list.html">
         | 
| 50 | 
            +
             | 
| 51 | 
            +
                    <svg width="24" height="24">
         | 
| 52 | 
            +
                      <rect x="0" y="4" width="24" height="4" rx="1" ry="1"></rect>
         | 
| 53 | 
            +
                      <rect x="0" y="12" width="24" height="4" rx="1" ry="1"></rect>
         | 
| 54 | 
            +
                      <rect x="0" y="20" width="24" height="4" rx="1" ry="1"></rect>
         | 
| 55 | 
            +
                    </svg>
         | 
| 56 | 
            +
                </a>
         | 
| 57 | 
            +
              
         | 
| 58 | 
            +
            </div>
         | 
| 59 | 
            +
                    <div class="clear"></div>
         | 
| 60 | 
            +
                  </div>
         | 
| 61 | 
            +
             | 
| 62 | 
            +
                  <div id="content"><h1>Top Level Namespace
         | 
| 63 | 
            +
              
         | 
| 64 | 
            +
              
         | 
| 65 | 
            +
              
         | 
| 66 | 
            +
            </h1>
         | 
| 67 | 
            +
            <div class="box_info">
         | 
| 68 | 
            +
              
         | 
| 69 | 
            +
             | 
| 70 | 
            +
              
         | 
| 71 | 
            +
              
         | 
| 72 | 
            +
              
         | 
| 73 | 
            +
              
         | 
| 74 | 
            +
              
         | 
| 75 | 
            +
             | 
| 76 | 
            +
              
         | 
| 77 | 
            +
             | 
| 78 | 
            +
              
         | 
| 79 | 
            +
            </div>
         | 
| 80 | 
            +
             | 
| 81 | 
            +
            <h2>Defined Under Namespace</h2>
         | 
| 82 | 
            +
            <p class="children">
         | 
| 83 | 
            +
              
         | 
| 84 | 
            +
                
         | 
| 85 | 
            +
                  <strong class="modules">Modules:</strong> <span class='object_link'><a href="P06.html" title="P06 (module)">P06</a></span>
         | 
| 86 | 
            +
                
         | 
| 87 | 
            +
              
         | 
| 88 | 
            +
                
         | 
| 89 | 
            +
                  <strong class="classes">Classes:</strong> <span class='object_link'><a href="Alimento.html" title="Alimento (class)">Alimento</a></span>, <span class='object_link'><a href="Alimento_concreto.html" title="Alimento_concreto (class)">Alimento_concreto</a></span>, <span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span>, <span class='object_link'><a href="List.html" title="List (class)">List</a></span>, <span class='object_link'><a href="Nodo.html" title="Nodo (class)">Nodo</a></span>
         | 
| 90 | 
            +
                
         | 
| 91 | 
            +
              
         | 
| 92 | 
            +
            </p>
         | 
| 93 | 
            +
             | 
| 94 | 
            +
             | 
| 95 | 
            +
             | 
| 96 | 
            +
             | 
| 97 | 
            +
             | 
| 98 | 
            +
             | 
| 99 | 
            +
             | 
| 100 | 
            +
             | 
| 101 | 
            +
             | 
| 102 | 
            +
            </div>
         | 
| 103 | 
            +
             | 
| 104 | 
            +
                  <div id="footer">
         | 
| 105 | 
            +
              Generated on Tue Nov 28 18:13:07 2017 by
         | 
| 106 | 
            +
              <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
         | 
| 107 | 
            +
              0.9.9 (ruby-2.3.0).
         | 
| 108 | 
            +
            </div>
         | 
| 109 | 
            +
             | 
| 110 | 
            +
                </div>
         | 
| 111 | 
            +
              </body>
         | 
| 112 | 
            +
            </html>
         | 
    
        data/lib/P06.rb
    ADDED
    
    | @@ -0,0 +1,23 @@ | |
| 1 | 
            +
            require "P06/version"
         | 
| 2 | 
            +
            require "P06/alimento"
         | 
| 3 | 
            +
            require "P06/list"
         | 
| 4 | 
            +
            require "P06/alimento_concreto"
         | 
| 5 | 
            +
            require "P06/DSL"
         | 
| 6 | 
            +
            require 'benchmark'
         | 
| 7 | 
            +
             | 
| 8 | 
            +
             | 
| 9 | 
            +
            # encoding: utf-8
         | 
| 10 | 
            +
            # Este modulo se ha creado para poder
         | 
| 11 | 
            +
            # poner el código que necesitemos
         | 
| 12 | 
            +
            # poner para poder realizar los
         | 
| 13 | 
            +
            # TDD del spec.
         | 
| 14 | 
            +
            # Este código va a pertenecer a las
         | 
| 15 | 
            +
            # distintas clases que hemos ido creando:
         | 
| 16 | 
            +
            # Alimento, Alimento_concreto y List.
         | 
| 17 | 
            +
            #
         | 
| 18 | 
            +
            #Author: Juan Pablo Claros Romero (alu0100972603@ull.edu.es)
         | 
| 19 | 
            +
             | 
| 20 | 
            +
             | 
| 21 | 
            +
            module P06
         | 
| 22 | 
            +
              # Your code goes here...
         | 
| 23 | 
            +
            end
         | 
    
        data/lib/P06/DSL.rb
    ADDED
    
    | @@ -0,0 +1,168 @@ | |
| 1 | 
            +
            class DSL
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            	attr_accessor :name, :datos, :total
         | 
| 4 | 
            +
            	
         | 
| 5 | 
            +
            	def initialize(nombre, alimentos, &block)
         | 
| 6 | 
            +
            		@nombre = nombre
         | 
| 7 | 
            +
            		@datos = []
         | 
| 8 | 
            +
            		@alimentos = alimentos
         | 
| 9 | 
            +
            		@total = 0
         | 
| 10 | 
            +
            		
         | 
| 11 | 
            +
            		if block_given?  
         | 
| 12 | 
            +
            			if block.arity == 1
         | 
| 13 | 
            +
            				yield self
         | 
| 14 | 
            +
            			else
         | 
| 15 | 
            +
            				instance_eval(&block) 
         | 
| 16 | 
            +
            			end
         | 
| 17 | 
            +
            		end
         | 
| 18 | 
            +
            	end
         | 
| 19 | 
            +
             | 
| 20 | 
            +
            	def to_s
         | 
| 21 | 
            +
            		
         | 
| 22 | 
            +
            		output = "\n#{@nombre}"
         | 
| 23 | 
            +
            		output << "\n#{'=' * 80}\n\n"
         | 
| 24 | 
            +
            		output << "Composicion nutricional:\n"
         | 
| 25 | 
            +
            		output << "\t\t|Proteínas\tGlúcidos\tLípidos\t\tValor Energético\n"
         | 
| 26 | 
            +
            		for i in (0..@alimentos.size-1)
         | 
| 27 | 
            +
            			if @alimentos[i].nombre.size > 7
         | 
| 28 | 
            +
            				output << "#{@alimentos[i].nombre}\t|#{@alimentos[i].proteinas}\t\t#{@alimentos[i].glucidos}\t\t#{@alimentos[i].grasas}\t\t#{@datos[i].round(2)}"
         | 
| 29 | 
            +
            			else
         | 
| 30 | 
            +
            				output << "#{@alimentos[i].nombre}\t\t|#{@alimentos[i].proteinas}\t\t#{@alimentos[i].glucidos}\t\t#{@alimentos[i].grasas}\t\t#{@datos[i].round(2)}"
         | 
| 31 | 
            +
            			end
         | 
| 32 | 
            +
            			@total = @total + datos[i]
         | 
| 33 | 
            +
            			output << "\n"
         | 
| 34 | 
            +
            		end
         | 
| 35 | 
            +
            		output << "#{'=' * 80}\n\n"
         | 
| 36 | 
            +
            		output << "Valor energético total\t\t\t\t\t\t#{@total.round(2)}\n\n"
         | 
| 37 | 
            +
            		
         | 
| 38 | 
            +
            		output
         | 
| 39 | 
            +
            	end
         | 
| 40 | 
            +
             | 
| 41 | 
            +
            	def vegetal(nombre, opciones = {})
         | 
| 42 | 
            +
            		
         | 
| 43 | 
            +
            		gramos = conversor(opciones)
         | 
| 44 | 
            +
            		calorias = 0
         | 
| 45 | 
            +
            		
         | 
| 46 | 
            +
            		for i in (0..@alimentos.size-1)
         | 
| 47 | 
            +
            			
         | 
| 48 | 
            +
            			if @alimentos[i].nombre == nombre
         | 
| 49 | 
            +
            				calorias = @alimentos[i].calorias*gramos
         | 
| 50 | 
            +
            			end
         | 
| 51 | 
            +
            			
         | 
| 52 | 
            +
            		end
         | 
| 53 | 
            +
            		
         | 
| 54 | 
            +
            		@datos << calorias
         | 
| 55 | 
            +
            	end
         | 
| 56 | 
            +
             | 
| 57 | 
            +
            	def fruta(nombre, opciones = {})
         | 
| 58 | 
            +
            		
         | 
| 59 | 
            +
            		gramos = conversor(opciones)
         | 
| 60 | 
            +
            		calorias = 0
         | 
| 61 | 
            +
            		
         | 
| 62 | 
            +
            		for i in (0..@alimentos.size-1)
         | 
| 63 | 
            +
            			
         | 
| 64 | 
            +
            			if @alimentos[i].nombre == nombre
         | 
| 65 | 
            +
            				calorias = @alimentos[i].calorias*gramos
         | 
| 66 | 
            +
            			end
         | 
| 67 | 
            +
            			
         | 
| 68 | 
            +
            		end
         | 
| 69 | 
            +
            		
         | 
| 70 | 
            +
            		@datos << calorias
         | 
| 71 | 
            +
            	end
         | 
| 72 | 
            +
            	
         | 
| 73 | 
            +
            	def cereal(nombre, opciones = {})
         | 
| 74 | 
            +
            		
         | 
| 75 | 
            +
            		gramos = conversor(opciones)
         | 
| 76 | 
            +
            		calorias = 0
         | 
| 77 | 
            +
            		
         | 
| 78 | 
            +
            		for i in (0..@alimentos.size-1)
         | 
| 79 | 
            +
            			
         | 
| 80 | 
            +
            			if @alimentos[i].nombre == nombre
         | 
| 81 | 
            +
            				calorias = @alimentos[i].calorias*gramos
         | 
| 82 | 
            +
            			end
         | 
| 83 | 
            +
            			
         | 
| 84 | 
            +
            		end
         | 
| 85 | 
            +
            		
         | 
| 86 | 
            +
            		@datos << calorias
         | 
| 87 | 
            +
            	end
         | 
| 88 | 
            +
            	
         | 
| 89 | 
            +
            	def proteina(nombre, opciones = {})
         | 
| 90 | 
            +
            		
         | 
| 91 | 
            +
            		gramos = conversor(opciones)
         | 
| 92 | 
            +
            		calorias = 0
         | 
| 93 | 
            +
            		
         | 
| 94 | 
            +
            		for i in (0..@alimentos.size-1)
         | 
| 95 | 
            +
            			
         | 
| 96 | 
            +
            			if @alimentos[i].nombre == nombre
         | 
| 97 | 
            +
            				calorias = @alimentos[i].calorias*gramos
         | 
| 98 | 
            +
            			end
         | 
| 99 | 
            +
            			
         | 
| 100 | 
            +
            		end
         | 
| 101 | 
            +
            		
         | 
| 102 | 
            +
            		@datos << calorias
         | 
| 103 | 
            +
            	end
         | 
| 104 | 
            +
            	
         | 
| 105 | 
            +
            	def aceite(nombre, opciones = {})
         | 
| 106 | 
            +
            		
         | 
| 107 | 
            +
            		gramos = conversor(opciones)
         | 
| 108 | 
            +
            		calorias = 0
         | 
| 109 | 
            +
            		
         | 
| 110 | 
            +
            		for i in (0..@alimentos.size-1)
         | 
| 111 | 
            +
            			
         | 
| 112 | 
            +
            			if @alimentos[i].nombre == nombre
         | 
| 113 | 
            +
            				calorias = @alimentos[i].calorias*gramos
         | 
| 114 | 
            +
            			end
         | 
| 115 | 
            +
            			
         | 
| 116 | 
            +
            		end
         | 
| 117 | 
            +
            		
         | 
| 118 | 
            +
            		@datos << calorias
         | 
| 119 | 
            +
            	end
         | 
| 120 | 
            +
             | 
| 121 | 
            +
            	def conversor(opciones)
         | 
| 122 | 
            +
            		gramos=0.0
         | 
| 123 | 
            +
            		
         | 
| 124 | 
            +
            		if opciones[:amount] =~ /(?i)pieza[s]?\s{0,4}/
         | 
| 125 | 
            +
            			gramos=gramos+1.18
         | 
| 126 | 
            +
            			opciones[:amount] =opciones[:amount].gsub(/(?i)pieza[s]?\s{0,4}/,"")
         | 
| 127 | 
            +
            		end
         | 
| 128 | 
            +
            		
         | 
| 129 | 
            +
            		if opciones[:amount] =~ /(?i)taza[s]?\s{0,4}/
         | 
| 130 | 
            +
            			gramos=gramos+2.974
         | 
| 131 | 
            +
            			opciones[:amount] =opciones[:amount].gsub(/(?i)taza[s]?\s{0,4}/,"")
         | 
| 132 | 
            +
            		end
         | 
| 133 | 
            +
            		
         | 
| 134 | 
            +
            		if opciones[:amount] =~ /(?i)cucharada[s]?\s{0,4}/
         | 
| 135 | 
            +
            			gramos=gramos+0.089
         | 
| 136 | 
            +
            			opciones[:amount] =opciones[:amount].gsub(/(?i)cucharada[s]?\s{0,4}/,"")
         | 
| 137 | 
            +
            		end
         | 
| 138 | 
            +
            		
         | 
| 139 | 
            +
            		if opciones[:amount] =~ /(?i)cucharon(es)?\s{0,4}/
         | 
| 140 | 
            +
            			gramos=gramos+9.778
         | 
| 141 | 
            +
            			opciones[:amount] =opciones[:amount].gsub(/(?i)cucharon(es)?\s{0,4}/,"")
         | 
| 142 | 
            +
            		end
         | 
| 143 | 
            +
            		
         | 
| 144 | 
            +
            		if opciones[:amount] =~ /(?i)pequeña[s]?\s{0,1}/
         | 
| 145 | 
            +
            			gramos=gramos-0.2
         | 
| 146 | 
            +
            			opciones[:amount] = opciones[:amount].gsub(/(?i)pequeña[s]?\s{0,1}/,"")
         | 
| 147 | 
            +
            		end
         | 
| 148 | 
            +
            		
         | 
| 149 | 
            +
            		if opciones[:amount] =~ /\d\/\d/
         | 
| 150 | 
            +
            			valor1 = /\d\//.match(opciones[:amount]).to_s.gsub(/\//,"")
         | 
| 151 | 
            +
            			valor2 = /\/\d/.match(opciones[:amount]).to_s.gsub(/\//,"")
         | 
| 152 | 
            +
            			resultado = valor1.to_f/valor2.to_f
         | 
| 153 | 
            +
            			gramos=gramos*resultado
         | 
| 154 | 
            +
            		end
         | 
| 155 | 
            +
            		
         | 
| 156 | 
            +
            		if opciones[:amount] =~ /\d/
         | 
| 157 | 
            +
            			if gramos > 0
         | 
| 158 | 
            +
            				gramos=gramos*opciones[:amount].to_f
         | 
| 159 | 
            +
            				opciones[:amount] = opciones[:amount].gsub(/\d/,"")
         | 
| 160 | 
            +
            			end
         | 
| 161 | 
            +
            		else
         | 
| 162 | 
            +
            			gramos=gramos+(opciones[:amount]/10)
         | 
| 163 | 
            +
            		end
         | 
| 164 | 
            +
            		
         | 
| 165 | 
            +
            		gramos
         | 
| 166 | 
            +
            		
         | 
| 167 | 
            +
            	end
         | 
| 168 | 
            +
            end
         | 
    
        data/lib/P06/alimento.rb
    ADDED
    
    | @@ -0,0 +1,53 @@ | |
| 1 | 
            +
            # Esta clase permite representar a un alimento
         | 
| 2 | 
            +
            # con sus porteinas, glucidos y grasas correspondientes. 
         | 
| 3 | 
            +
            # Tambien se puede sacar las calorias de los alimentos
         | 
| 4 | 
            +
            # con las proteinas, glucidos y grasas.
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            class Alimento
         | 
| 7 | 
            +
             | 
| 8 | 
            +
             | 
| 9 | 
            +
            	attr_reader :nombre, :proteinas, :glucidos, :grasas
         | 
| 10 | 
            +
             | 
| 11 | 
            +
            	# Se asigna el nombre del alimento, las proteinas, 
         | 
| 12 | 
            +
            	# los glucidos y las grasas.
         | 
| 13 | 
            +
            	def initialize(nombre,proteinas,glucidos,grasas,individuos)
         | 
| 14 | 
            +
            		@nombre = nombre
         | 
| 15 | 
            +
            		@proteinas = proteinas
         | 
| 16 | 
            +
            		@glucidos = glucidos
         | 
| 17 | 
            +
            		@grasas = grasas
         | 
| 18 | 
            +
            		@cal
         | 
| 19 | 
            +
            		@individuos = individuos
         | 
| 20 | 
            +
            	end
         | 
| 21 | 
            +
             | 
| 22 | 
            +
            	# Este es el metodo to_s que muestra al alimento por pantalla.
         | 
| 23 | 
            +
            	def to_s
         | 
| 24 | 
            +
            		"El alimento #{@nombre} tiene #{@proteinas} gramos de proteínas, #{@glucidos} gramos de glúcidos, #{@grasas} gramos de lípidos y #{calorias} calorías"
         | 
| 25 | 
            +
            	end
         | 
| 26 | 
            +
             | 
| 27 | 
            +
            	# Este metodo muestra al alimento formateado.
         | 
| 28 | 
            +
            	def formateado
         | 
| 29 | 
            +
            		print "Nombre: ",@nombre,"\t","Proteínas: ",@proteinas,"\t","Glúcidos: ",@glucidos,"\t","Grasas: ",@grasas
         | 
| 30 | 
            +
            		puts
         | 
| 31 | 
            +
            	end
         | 
| 32 | 
            +
             | 
| 33 | 
            +
            	# Este metodo calcula las calorias del alimento
         | 
| 34 | 
            +
            	# según las proteinas, los glucidos y las grasas
         | 
| 35 | 
            +
            	# del alimento.
         | 
| 36 | 
            +
            	def calorias
         | 
| 37 | 
            +
            		@cal = (4*@proteinas)+(4*@glucidos)+(9*@grasas)
         | 
| 38 | 
            +
            	end
         | 
| 39 | 
            +
             | 
| 40 | 
            +
            	# Este metodo lo que hace es devolverme el valor correspondiente
         | 
| 41 | 
            +
            	# dependiendo de la posicion en la que me encuentre en el vector,
         | 
| 42 | 
            +
            	# esto lo hago para que no me acceda a la posicion -1.
         | 
| 43 | 
            +
            	def neg(pos,gluc)
         | 
| 44 | 
            +
            		pos < 0 ? gluc[0] : gluc[pos]
         | 
| 45 | 
            +
            	end
         | 
| 46 | 
            +
             | 
| 47 | 
            +
            	# Este metodo me calcula el AIBC. Se ha aplicado la programacion
         | 
| 48 | 
            +
            	# funcional.
         | 
| 49 | 
            +
            	def AIBC
         | 
| 50 | 
            +
            		@individuos.map{|i| g=i.first; pos=-1; i.map{|j| prev=neg(pos,i); pos=pos+1; (((j-g)+(prev-g))*2.5)}.reduce(:+).round(2)}
         | 
| 51 | 
            +
            	end
         | 
| 52 | 
            +
             | 
| 53 | 
            +
            end
         | 
| @@ -0,0 +1,64 @@ | |
| 1 | 
            +
            # Esta clase es una clase heredada de alimento que contiene
         | 
| 2 | 
            +
            # también el grupo del alimento.
         | 
| 3 | 
            +
            # Se ha incluido el módulo Comparable
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            class Alimento_concreto < Alimento
         | 
| 6 | 
            +
             | 
| 7 | 
            +
                include Comparable
         | 
| 8 | 
            +
             | 
| 9 | 
            +
                attr_accessor :grupo
         | 
| 10 | 
            +
                
         | 
| 11 | 
            +
                # Se asigna el grupo del alimento y los otros datos del
         | 
| 12 | 
            +
                # alimento se le pasa al initialize de Alimento
         | 
| 13 | 
            +
                def initialize (grupo,nombre,proteinas,glucidos,grasas)
         | 
| 14 | 
            +
                    super(nombre,proteinas,glucidos,grasas,[])
         | 
| 15 | 
            +
                    @grupo = grupo
         | 
| 16 | 
            +
                end
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                # Este metodo se encarga de realizar las
         | 
| 19 | 
            +
                # comparaciones.
         | 
| 20 | 
            +
                def <=>(another)
         | 
| 21 | 
            +
            	calorias <=> another.calorias
         | 
| 22 | 
            +
                end
         | 
| 23 | 
            +
             | 
| 24 | 
            +
                # Este metodo se encarga de mostrar al alimento
         | 
| 25 | 
            +
                # por pantalla.
         | 
| 26 | 
            +
                def to_s
         | 
| 27 | 
            +
                    "Grupo: #{@grupo}\t" + "Nombre: #{@nombre}\t" + "Proteínas: #{@proteinas}\t" + "Glúcidos: #{@glucidos}\t" + "Grasas: #{@grasas}\t" + "Calorias: #{calorias}"
         | 
| 28 | 
            +
                end
         | 
| 29 | 
            +
                
         | 
| 30 | 
            +
            end
         | 
| 31 | 
            +
             | 
| 32 | 
            +
            # Aqui abro la clase array y creo los metodos de ordenacion
         | 
| 33 | 
            +
            # for y each. El sort ya lo tiene la clase
         | 
| 34 | 
            +
            class Array
         | 
| 35 | 
            +
                
         | 
| 36 | 
            +
                # Este es el metodo de ordenacio for. Utilizo el
         | 
| 37 | 
            +
                # metodo burbuja.
         | 
| 38 | 
            +
                def ordenar_for
         | 
| 39 | 
            +
                    
         | 
| 40 | 
            +
                    @vector=self
         | 
| 41 | 
            +
                    
         | 
| 42 | 
            +
                    for i in (0..@vector.size-1)
         | 
| 43 | 
            +
                        for j in (0..@vector.size-1)
         | 
| 44 | 
            +
                            if j+1 != @vector.size
         | 
| 45 | 
            +
                                if @vector[j+1] < @vector[j]
         | 
| 46 | 
            +
                                    @vector[j], @vector[j+1] = @vector[j+1], @vector[j]
         | 
| 47 | 
            +
                                end
         | 
| 48 | 
            +
                            end
         | 
| 49 | 
            +
                        end
         | 
| 50 | 
            +
                    end
         | 
| 51 | 
            +
                    @vector
         | 
| 52 | 
            +
                end
         | 
| 53 | 
            +
             | 
| 54 | 
            +
                #Este es el metodo de ordenacion each.
         | 
| 55 | 
            +
                def ordenar_each
         | 
| 56 | 
            +
                    
         | 
| 57 | 
            +
                    @ordenado= []
         | 
| 58 | 
            +
                    aux_self=self.clone
         | 
| 59 | 
            +
                    
         | 
| 60 | 
            +
                    (0..self.size-1).each{|i| min=aux_self.min; @ordenado << min; aux_self.delete(min)}
         | 
| 61 | 
            +
                    
         | 
| 62 | 
            +
                    @ordenado
         | 
| 63 | 
            +
                end
         | 
| 64 | 
            +
            end
         | 
    
        data/lib/P06/list.rb
    ADDED
    
    | @@ -0,0 +1,162 @@ | |
| 1 | 
            +
            # Aqui estamos declarando la clase Nodo,
         | 
| 2 | 
            +
            # pero lo hacemos mediante un Struct,
         | 
| 3 | 
            +
            # que nos incluye ya los mixin Comparable y Enumerable.
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            Nodo = Struct.new(:value, :next, :prev)
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            # Esta clase permite representar una lista
         | 
| 8 | 
            +
            # doblemente enlazada.
         | 
| 9 | 
            +
            # Se ha incluido el módulo Enumerable.
         | 
| 10 | 
            +
             | 
| 11 | 
            +
            class List
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                include Enumerable
         | 
| 14 | 
            +
                attr_accessor :head, :tail
         | 
| 15 | 
            +
               
         | 
| 16 | 
            +
                # Se asignan el valor del head y el tail de
         | 
| 17 | 
            +
                # la lista, que inicialmente es nil.
         | 
| 18 | 
            +
                def initialize
         | 
| 19 | 
            +
                   @head = nil
         | 
| 20 | 
            +
                   @tail = nil
         | 
| 21 | 
            +
                end
         | 
| 22 | 
            +
                
         | 
| 23 | 
            +
                # Inseta un valor por la cabeza de la lista.
         | 
| 24 | 
            +
                def push_head (nodo)
         | 
| 25 | 
            +
                    if @head == nil
         | 
| 26 | 
            +
                        @head = Nodo.new(nodo,nil,nil)
         | 
| 27 | 
            +
                        @tail = @head
         | 
| 28 | 
            +
                    else
         | 
| 29 | 
            +
                        @head.next = Nodo.new(nodo,nil,@head)
         | 
| 30 | 
            +
                        @head = @head.next
         | 
| 31 | 
            +
                    end
         | 
| 32 | 
            +
                end
         | 
| 33 | 
            +
             | 
| 34 | 
            +
                # Devuelve el valor de un nodo que le
         | 
| 35 | 
            +
                # pasamos por parametros.
         | 
| 36 | 
            +
                def get_value(value)
         | 
| 37 | 
            +
                    if @head == nil   #Comprobamos si la lista no esta vacía
         | 
| 38 | 
            +
                        raise RuntimeError, "Lista vacía, no se puede extraer nodo"
         | 
| 39 | 
            +
                    else
         | 
| 40 | 
            +
            		    i = @tail
         | 
| 41 | 
            +
            		    @valor=nil
         | 
| 42 | 
            +
            		    while i != nil
         | 
| 43 | 
            +
            		        if i.value == value
         | 
| 44 | 
            +
            		            @valor = i.value
         | 
| 45 | 
            +
            		        end
         | 
| 46 | 
            +
            		        i = i.next
         | 
| 47 | 
            +
            		    end
         | 
| 48 | 
            +
                    end
         | 
| 49 | 
            +
                    @valor
         | 
| 50 | 
            +
                end
         | 
| 51 | 
            +
                
         | 
| 52 | 
            +
                # Nos devuelve el nodo siguiente de un
         | 
| 53 | 
            +
                # nodo que le pasamos por parámetros.
         | 
| 54 | 
            +
                def get_next(value)
         | 
| 55 | 
            +
                    if @head == nil   #Comprobamos si la lista no esta vacía
         | 
| 56 | 
            +
                        raise RuntimeError, "Lista vacía, no se puede extraer nodo"
         | 
| 57 | 
            +
                    else
         | 
| 58 | 
            +
            		    i = @tail
         | 
| 59 | 
            +
            		    @valor = nil
         | 
| 60 | 
            +
            		    while i != nil
         | 
| 61 | 
            +
            		        if i.value == value
         | 
| 62 | 
            +
            		            @valor = i.next
         | 
| 63 | 
            +
            		        end
         | 
| 64 | 
            +
            		        i = i.next
         | 
| 65 | 
            +
            		    end
         | 
| 66 | 
            +
                    end
         | 
| 67 | 
            +
                    @valor
         | 
| 68 | 
            +
                end
         | 
| 69 | 
            +
                
         | 
| 70 | 
            +
                # Devuelve el nodo previo al nodo
         | 
| 71 | 
            +
                # que pasamos por parámetros.
         | 
| 72 | 
            +
                def get_prev(value)
         | 
| 73 | 
            +
                    if @head == nil   #Comprobamos si la lista no esta vacía
         | 
| 74 | 
            +
                        raise RuntimeError, "Lista vacía, no se puede extraer nodo"
         | 
| 75 | 
            +
                    else
         | 
| 76 | 
            +
            		    i = @tail
         | 
| 77 | 
            +
            		    @valor = nil
         | 
| 78 | 
            +
            		    while i != nil
         | 
| 79 | 
            +
            		        if i.value == value
         | 
| 80 | 
            +
            		            @valor = i.prev
         | 
| 81 | 
            +
            		        end
         | 
| 82 | 
            +
            		        i = i.next
         | 
| 83 | 
            +
            		    end
         | 
| 84 | 
            +
                    end
         | 
| 85 | 
            +
                    @valor
         | 
| 86 | 
            +
                end
         | 
| 87 | 
            +
             | 
| 88 | 
            +
                # Este metodo nos devuelve el tamaño
         | 
| 89 | 
            +
                # de la lista.
         | 
| 90 | 
            +
                def size
         | 
| 91 | 
            +
            	size = 0
         | 
| 92 | 
            +
            	i = @tail
         | 
| 93 | 
            +
            	while i != nil
         | 
| 94 | 
            +
            		i = i.next
         | 
| 95 | 
            +
            		size+=1
         | 
| 96 | 
            +
            	end
         | 
| 97 | 
            +
            	return size
         | 
| 98 | 
            +
                end
         | 
| 99 | 
            +
             | 
| 100 | 
            +
                # Este metodo nos permite insertar varios
         | 
| 101 | 
            +
                # valores a la lista.
         | 
| 102 | 
            +
                # Estos valores se insertan por el head.
         | 
| 103 | 
            +
                def push_others(values)
         | 
| 104 | 
            +
                    for i in (0..values.size-1)
         | 
| 105 | 
            +
            	        push_head(values[i])
         | 
| 106 | 
            +
                    end
         | 
| 107 | 
            +
                end
         | 
| 108 | 
            +
             | 
| 109 | 
            +
                # Este metodo nos permite extraer el
         | 
| 110 | 
            +
                # tail de la lista.
         | 
| 111 | 
            +
                def pop_tail
         | 
| 112 | 
            +
                    
         | 
| 113 | 
            +
                    extraido = false
         | 
| 114 | 
            +
                    
         | 
| 115 | 
            +
                    if @head == nil   #Comprobamos si la lista no esta vacía
         | 
| 116 | 
            +
            			raise RuntimeError, "Lista vacía, no se puede extraer nodo"
         | 
| 117 | 
            +
            		else
         | 
| 118 | 
            +
            		    extraido = true
         | 
| 119 | 
            +
            			if @head == @tail
         | 
| 120 | 
            +
            				@head = nil
         | 
| 121 | 
            +
            				@tail = nil
         | 
| 122 | 
            +
            			else
         | 
| 123 | 
            +
            				@tail = @tail.next
         | 
| 124 | 
            +
            				@tail.prev = nil
         | 
| 125 | 
            +
            			end
         | 
| 126 | 
            +
                    end
         | 
| 127 | 
            +
                    return extraido
         | 
| 128 | 
            +
                end
         | 
| 129 | 
            +
             | 
| 130 | 
            +
                # Este metodo nos permite extraer
         | 
| 131 | 
            +
                # el head de la lista.
         | 
| 132 | 
            +
                def pop_head
         | 
| 133 | 
            +
                    extraido = false
         | 
| 134 | 
            +
                    if @head == nil   #Comprobamos si la lista no esta vacía
         | 
| 135 | 
            +
            			raise RuntimeError, "Lista vacía, no se puede extraer nodo"
         | 
| 136 | 
            +
            		else
         | 
| 137 | 
            +
            		    extraido = true
         | 
| 138 | 
            +
            			if @head == @tail
         | 
| 139 | 
            +
            				@head = nil
         | 
| 140 | 
            +
            				@tail = nil
         | 
| 141 | 
            +
            			else
         | 
| 142 | 
            +
            				@head = @head.prev
         | 
| 143 | 
            +
                            @head.next = nil
         | 
| 144 | 
            +
            			end
         | 
| 145 | 
            +
                    end
         | 
| 146 | 
            +
                    
         | 
| 147 | 
            +
                    return extraido
         | 
| 148 | 
            +
                end
         | 
| 149 | 
            +
             | 
| 150 | 
            +
                # Este metodo es el each que van a
         | 
| 151 | 
            +
                # utilizar los metodos del módulo
         | 
| 152 | 
            +
                # Enumerable, pero esta acomodado
         | 
| 153 | 
            +
                # a nuestra clase.
         | 
| 154 | 
            +
                def each
         | 
| 155 | 
            +
            	i = @tail
         | 
| 156 | 
            +
            	while i != nil
         | 
| 157 | 
            +
            	    yield i.value
         | 
| 158 | 
            +
            	    i = i.next
         | 
| 159 | 
            +
            	end
         | 
| 160 | 
            +
                end
         | 
| 161 | 
            +
             | 
| 162 | 
            +
            end
         |