Actually found the answer to the English version

require 'rubygems' require 'active_support/all' def random_hour(from, to) (Date.today + rand(from..to).hour + rand(0..60).minutes).to_datetime end puts random_hour(10, 15) 

The question is how to understand the principle of work require I know. The question is, why connect it, what is it? \

 require 'rubygems' require 'active_support/all' 

When compiling this code gives

 C:/Ruby23-x64/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- active_support/all (LoadError) from C:/Ruby23-x64/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require' from C:/Users/###/Desktop/RubyProg/OlympProb/2.6/reshenie.rb:6:in `<top (required)>' 
  • these require are classic gems. If you just need to show the time as a string, then you can simply ruby -e 'puts rand(0..24).to_s + ":" + rand(0..60).to_s' . And deal with gems if you want to cut. - KoVadim
  • In principle, as an option. But it’s still interesting what’s wrong with the above code. Is this all not part of the standard assembly (sdk)? - QWD666 2:32 pm
  • The concept of "standard assembly" is very vague. But I am afraid that just you have the second package just is not included. You can install it in different ways. Either make a gem file for yourself and then chop everything on your team (and this is the right way), or package manager to install gems. - KoVadim
  • Solved the problem by slightly upgrading your offer. `x = rand (0..24) .to_s.rjust (2, '0') +": "+ rand (0..60) .to_s.rjust (2, '0')` - QWD666
  • Another possibly stupid question is how to implement a regular time search as strings? If you do it in a pearl, it should look something like this /\d1616..19}:\d3030}/ for it doesn’t matter whether it is a string or a number. - QWD666

2 answers 2

To generate random time and, in general, to generate various information, it is better to use a specialized gem like faker

 require 'faker' puts Faker::Time.between( Time.new(2016, 7, 15, 0, 0, 0), Time.new(2016, 7, 15, 23, 59, 59)) 

To use the example above, you will need to install a gem faker

 gem install activesupport 

In the example you gave, active_support / all was most likely required for the to_datetime method. This is one of the components of the Rails framework, completely redundant for such a small example. However, since Rails is very popular and many developers know it well, its components / gems are often used to create some quick solutions. To install active_support, you need to run the command

 gem install activesupport 
     require 'active_support/all' 

    What is it? Bad example of loading Active Support gem .

    This is what the numbers suddenly have methods for hour / hours , minutes and the like. But the trouble is that far from it is loaded . This line generally loads all Active Support . And this is a rather big gem.

    These methods are missing from Ruby itself. Try irb expression in pure 5.hours . NoMethodError , NoMethodError . And with ActiveSupport it works, because standard classes are added (monkeypatched) in it. Hence, we are dealing with the section "Core Extensions" ( core_ext ) . Methods have arisen for numbers , so it seems that this is an extension of numeric . Further it is obvious - methods about time, so the file time.rb .

    Knowing this, you can make a much more “narrow” and fast require :

     require 'active_support/core_ext/numeric/time' 

    What for? We look Rails guides :

    In order to have a near-zero default footprint, it doesn’t load anything by default. You can just load it

    To have almost zero memory consumption, Active Support does not load anything by default. It is broken into small pieces so that you can load only what you need.