Date & Time in Ruby

Running the Ruby on Rails in my Command with the following:

puts time.year    # => Year of the date 
puts time.month   # => Month of the date (1 to 12)
puts time.day     # => Day of the date (1 to 31 )
puts time.wday    # => 0: Day of week: 0 is Sunday
puts time.yday    # => 365: Day of year
puts time.hour    # => 23: 24-hour clock
puts time.min     # => 59
puts time.sec     # => 59
puts time.usec    # => 999999: microseconds
puts time.zone    # => "UTC": timezone name

Turning time into seconds:

# Returns number of seconds since epoch
time = Time.now.to_i  

# Convert number of seconds into Time object.
Time.at(time)

# Returns second since epoch which includes microseconds
time = Time.now.to_f

Some different ways to display the date and time:

time = Time.new

puts time.to_s
puts time.ctime
puts time.localtime
puts time.strftime("%Y-%m-%d %H:%M:%S")

Time arithmetic:

now = Time.now           # Current time
puts now

past = now - 10          # 10 seconds ago. Time - number => Time
puts past

future = now + 10        # 10 seconds from now Time + number => Time
puts future

diff = future - now      # => 10  Time - Time => number of seconds
puts diff

Source: http://www.tutorialspoint.com/ruby/ruby_date_time.htm

Advertisements

Numbers, Operators, and Expressions

Numbers, Operators and Expressions:

  1. What’s the difference between an Integer and a Float?
    Integers are whole numbers. Floats are decimals
    More info here: http://docs.railsbridge.org/ruby/numbers_and_arithmetic
  2. Why should you be careful when converting back and forth between integers and floats?
  3. What’s the difference between =, ==, and ===?
    = is assignment operator (ie. sets a = b)
    == is equal comparison operator (checks if two variables have equal values)
    === is identical comparison operator (checks if two variables are equal values and are the same type)
  4. How do you do exponents in Ruby?
    Use two * together. I.e.: a**2 is a to the second power
    http://www.tutorialspoint.com/ruby/ruby_operators.htm
  5. What is a range?
    A range is a set of values with a start and an end.
  6. How do you create a range?
  7. What’s the difference between (1..3) and (1...3)?
    (1..3) includes the numbers 1-3. (1…3) does not include the last number, so just numbers 1-2.
  8. What are three ways to create a range?
    Range as a sequence, range as an interval, range as a condition.

Resources:
http://docs.railsbridge.org/ruby/numbers_and_arithmetic
http://www.tutorialspoint.com/ruby/ruby_operators.htm
http://ruby-doc.org/core-1.9.3/Range.html

Codermanual – Part V finished

I’ve just finished Part V of Codermanual, the JavaScript & jQuery section. It was an oversimplification of the language, I feel, but the way it was explained, really helped me wrap my head around how programming languages work. The amount of information in this part was overwhelming, but I feel confident now that I can handle JavaScript.

I had tried to learn JavaScript on my own back in April but I was using Codecademy at that time and I think their JavaScript tutorial was way too advanced. Everything went over my head. It was my first time learning an object-oriented programming language. Around the same time, I kept hearing a lot about Ruby and Ruby on Rails so I tried Codecademy’s Ruby course instead and found it much easier to grasp so I stuck with it and am now working on Michael Hartl’s Ruby on Rails tutorial.

I’m halfway through and realizing that there are still many things about Ruby that I have questions about, so I’m going to pause Hartl’s tutorial as well as Codermanual and do The Odin Project’s Ruby Programming course (which is also free!).

The more I learn, the more I realize how little I know.