5 Replies - 1960 Views - Last Post: 13 January 2012 - 07:34 PM Rate Topic: -----

#1 rjohn5854  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 172
  • Joined: 04-October 11

New to Ruby: Basic Question.

Posted 13 January 2012 - 12:27 PM

I was wondering how to run this ruby script.

The file name is check_payday.rb. Is there a specific folder location i should be saving this script to execute it?

#!/usr/bin/env ruby
# check_payday.rb

DAYS_IN_A_PAY_PERIOD = 14
SECONDS_IN_A_DAY     = 60 * 60 * 24

matching_date = Time.local(0, 0, 0, 22, 9, 2006, 5, 265, true, "EDT")
current_date = Time.new()

difference_in_seconds = (current_date - matching_date)
difference_in_days    = (difference_in_seconds / SECONDS_IN_A_DAY).to_i
days_to_wait          = (
  DAYS_IN_A_PAY_PERIOD - difference_in_days
) % DAYS_IN_A_PAY_PERIOD

if (days_to_wait.zero?)
  puts 'Payday today.' 
else
  print 'Payday in ' + days_to_wait.to_s + ' day'
  puts days_to_wait == 1 ? '.' : 's.' 
end



Is This A Good Question/Topic? 0
  • +

Replies To: New to Ruby: Basic Question.

#2 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 894
  • View blog
  • Posts: 3,153
  • Joined: 12-May 09

Re: New to Ruby: Basic Question.

Posted 13 January 2012 - 12:30 PM

As long as Ruby is able to run, it doesn't matter where you save it. Just open a command prompt, get to that directory, and run ruby script_name.rb

This post has been edited by xclite: 13 January 2012 - 12:30 PM

Was This Post Helpful? 0
  • +
  • -

#3 rjohn5854  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 172
  • Joined: 04-October 11

Re: New to Ruby: Basic Question.

Posted 13 January 2012 - 12:39 PM

Hmmm get to that directory? i think i missed that part.

I just open terminal and type check_payday.rb - it gave an error that file can't be found. How do i get to the directory? like can you show me an example ??

Very much appreciaited.
Was This Post Helpful? 0
  • +
  • -

#4 Crockeo  Icon User is offline

  • D.I.C Head

Reputation: 44
  • View blog
  • Posts: 247
  • Joined: 21-June 11

Re: New to Ruby: Basic Question.

Posted 13 January 2012 - 06:48 PM

Well, first off, it depends on what OS you're running, and where the file is saved of course.

Like if the file were location at:
/home/[username]/Development/Ruby/check_payday.rb



You'd get there by typing
cd /home/[username]/Development/Ruby/

#Then type this to run it:
ruby check_payday.rb



Of course that's only on Linux and Mac OSX.
Was This Post Helpful? 0
  • +
  • -

#5 rjohn5854  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 172
  • Joined: 04-October 11

Re: New to Ruby: Basic Question.

Posted 13 January 2012 - 06:56 PM

Ohh cool.

I just figured it out an hour ago. I saved the file on the desktop (Mac)

I typed ruby and the line below in my terminal.

/users/(myusername)/desktop/check_payday.rb



Why is CD used? im new to the terminal too.

Thank You
Was This Post Helpful? 0
  • +
  • -

#6 Crockeo  Icon User is offline

  • D.I.C Head

Reputation: 44
  • View blog
  • Posts: 247
  • Joined: 21-June 11

Re: New to Ruby: Basic Question.

Posted 13 January 2012 - 07:34 PM

Cd is a command to navigate to a directory in the terminal.

Like:
cd /



Would make it so your terminal is operating in the root directory.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1