How do you get an accidental time of the type hh:mm



  • I actually found an 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 do you understand the /principle of work require me to know. The question is, why do you have to connect what this is?

    require 'rubygems'
    require 'active_support/all'

    When compiling this code, it issues

    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)>'



  • To generate incidental time and, in general, to generate a variety of information, you better use a specialized hem like https://github.com/stympy/faker#fakertime

    require 'faker'
    

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

    To take advantage of the example above, you'll need to set up a hem faker.

    gem install activesupport

    In your example, active_support/all is most likely required for the method to_datetime♪ It's one of the components of the Rails Framworth, which is totally redundant for such a small example. However, as the Rails is very popular and many developers know it well, very often use its components/hems to make quick decisions. To establish active_support, you're gonna need a team.

    gem install activesupport


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2