Ruby 时间 year()函数 year() 是Ruby中一个内置的方法,用于返回时间的年份。 语法 :time.year() 参数 :该函数不接受任何参数 返回值 : 它返回时间的年份 例子 1 : # Ruby code for year() method # Include Time require 'time' # Declaring time a = Time.new(2000, 12, 23) # Prints the year for the time puts a.year()RubyCopy 输出: 2000RubyCopy 例2 : # Ruby code for year() method # Include Time require 'time' # Declaring time a = Time.now() # Prints the year for the time puts a.year()RubyCopy 输出: 2019RubyCopy