Write a program that recommends how to dress today.
The files are clothes in the format:
название шмотки тип шмотки диапазон температур
For example,
шапка-ушанка из зайца шапка (-20, -5)
or
шлепанцы ботинки (+20, +30)
The program asks the user what the temperature is now, and then generates a set of clothes, displays the name on the screen (in the console) one random item of each type (if found): шапка, шарф, кофта, куртка, штаны, ботинки, перчатки.
Clarification:
Under the range can fit 2 or 3 items of each type for example:
валенки ботинки (-35, -15) унты ботинки (-25, -15) утепленные кроссовки ботинки (-25, -10)
and if the user enters -18, then all 3 types will come out, and 1 random type is needed, you cannot go outside and put on felt boots on top of the warmed sneakers.
Какая сегодня температура? -18 Рекомендуется надеть: шапку вязанную, шарф, куртка с мехом, джинсы с начесом, валенки.
To solve the problem, I created the ClothesItem
class
class ClothesItem attr_accessor :title, :type, :line def initialize(path) File.open(path) do |file| arr = file.readlines @title = arr[0].chomp @type = arr[1].chomp @line = arr[2].chomp end end def in_interval?(t) int1, int2 = @line.gsub(/[() ]/, '').split(',') (int1.to_i..int2.to_i).include?(t) end end
And here is my unfinished main program (I put the files in the clothes folder)
require_relative 'clothes_item' puts 'Какая сегодня температура?' temperature = STDIN.gets.chomp.to_i all_clothes = Array.new Dir["#{File.dirname(__FILE__)}/data/*.txt"].each do |path| all_clothes << ClothesItem.new(path) end suit_clothes = Array.new types_of_clothes = Array.new all_clothes.each do |c| if c.in_interval?(temperature) types_of_clothes << c.type suit_clothes << [c.title, c.type] end end
I can not understand how to do so to display the name of clothes 1 random type of clothing.
I only learn ruby
I know how syntax I know a lot of methods, but I’m somehow lacking in logic.
Enumerable
exercise! Simple, but informative. - D-side