Tuesday, November 1, 2011

RUBY

RUBY




REVISED: Sunday, June 7, 2015






You will learn how to download Ruby and write your first Ruby script.

I.  RUBY INTRODUCTION

Ruby is an object oriented,  cross-platform, interpreted, scripting language created in Japan in 1995 by Yukihiro Matsumoto.

II.  DOWNLOADING RUBY

Use the following link to download and install Ruby:

Make sure you write down the hard drive file path used to download Ruby.  When you create Ruby files save those files to the same path.

For example I downloaded Ruby using the following path:

c:\Ruby193


-->


III.  RUBY INSTALLATION VERIFICATION

If you are using Windows, after you run the "Start Command Prompt with Ruby," you should see a Ruby response similar to the following:

C:\Documents and Settings\HP_Administrator>

After the > type:

ruby -v

and press Enter.

If Ruby was installed properly, Ruby will respond with its version.  You should see a Ruby response similar to the following:

ruby 1.9.3p0 (2011-10-30) [i386-mingw32]

IV.  RUBY SCRIPT EXAMPLE

Copy and Paste the following Ruby script into your text editor:

def fact(n)
  if n == 0
    1
  else
    n * fact(n-1)
  end
end
puts fact(ARGV[0].to_i)

From your text editor do a "File Save As" and save the file with the name "fact.rb" to the same file folder you used when you downloaded Ruby; e.g.:

c:\Ruby193\fact.rb

From the "Start Command Prompt with Ruby," after the > type:

ruby c:\Ruby193\fact.rb 40

and press Enter.

The response from Ruby will be:

815915283247897734345611269596115894272000000000

which is 40 factorial.

The # character starts a Ruby comment.  A Ruby comment extends from the # character to the end of the line.  Ruby comments are completely ignored by the Ruby interpreter.  The comments are for your own use, to help you document Ruby scripts and help you remember why you wrote the script the way you did.  The following # comments are used to help explain the example Ruby script.

def fact(n)                # def defines a method, fact( ), which takes a   single argument, n.
  if n == 0                 # if is a condition. If true n = 1; if false else is evaluated.
    1                                # The value of n is 1 if the condition is true.
  else                            # Code from else to end is evaluated if condition is false.
    n * fact(n-1)        # n times fact(n-1).
  end                             # First end closes the if statement.
end                                # Second end closes the def statement.
puts fact(ARGV[0].to_i)    # Invokes fact( ) method, argument from command line, prints result.
# ARGV is an array containing command line arguments.
# Members of ARGV are strings which to_i converts to an integral number. 

You have learned how to download Ruby and write your first Ruby script, have fun!

Elcric Otto Circle



-->

-->





How to Link to My Home Page

It will appear on your website as:

"Link to: ELCRIC OTTO CIRCLE's Home Page"




No comments:

Post a Comment

RUBY Headline Animator