Holla Tech - Learn

Standard Classes
 

Ruby provides a number of standard built-in classes that make our life a lot easier by providing useful methods for manipulating data.
Some of the classes in previous lessons include Array, String and Struct.
Another useful class is the Math class, which provides methods to perform mathematical operations.
For example: 

# square root
puts Math.sqrt(9) # 3

# pi constant
puts Math::PI

# trigonometry (sin, cos, tan)
puts Math::cos(0) # 1 

 

NOTE!
Try it yourself using a Ruby code editor to view the outputs. 

Time
 

The Time class represents dates and times in Ruby.

# current time
t = Time.now
puts t

# year, month, day
puts t.year
puts t.month
puts t.day

# custom date
t = Time.new(1988, 6, 10)

# day of week: 0 is Sunday
puts t.wday

# day of year
puts t.yday 

 

NOTE!
Try it yourself using a Ruby code editor to view the outputs. 

BACK NEXT

CLICK ON THE BUTTON BELOW TO GO TO THE RUBY MAIN COURSE PAGE. 

RUBY MAIN COURSE PAGE

 


© License: All Rights Reserved 


CONTACT HOLLA TECH – LEARN SUPPORT