Ruby on Rails over the prompt



Ruby on rails ha sthree important commands: 
erb, ruby, and rails.

1. erb command

The "erb" command reproduces html tags and 
executes what it is between <= ... >.
Example:

<% word = "Regards," %>
<html>
<head></head><body>
<p><%= word %></p>
</body></html>

Save this file with an rb extention, as regards.rb.
Run this program under the prompt command-line like this:
C:\ruby\>erb regards.rb
The output is:
<html> <head></head><body> <p>Regards,</p> </body></html>

1. ruby command

C:\>cd ruby
The current version:
C:\ruby>ruby -v ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]

Program directly on the command line using the -e option:
C:\ruby>ruby -e 'puts "Regards"' Regards

Programs can be stored in files:

1. A simple string

C:\ruby>echo print 'All the best of luck ... '> examples/best.rb C:\ruby>cd examples C:\ruby\examples>best.rb All the best of luck ... C:\ruby\examples>cd .. C:\ruby>

2. fibo.rb

# Program to find the fibobacci  of a positive number
# Save this as fibonacci.rb
def fibonacci(n)
  if (n == 0 || n == 1) 
  1
  else 
  fibonacci(n-1) + fibonacci(n-2)
  end
end

print fibonacci(ARGV[0].to_i) "\n"

Outputs:
C:\ruby\examples>fibonacci.rb 1 1 C:\ruby\examples>fibonacci.rb 12 233 C:\ruby\examples>fibonacci.rb 23 46368 C:\ruby\examples>

3.Invoking ruby with no arguments

C:\ruby>cd ..

C:\>ruby
puts "Regards,"
puts "All the best of luck .."
^D
Regards,
All the best of luck ..

C:\>
^D means Ctrl-D.

4. using evaluation eval.rb utility

The file eval.rb is stored in te directory:
C:\ruby\samples\RubySrc-1.8.6\sample

Issue the command:
C:\ruby>cd C:\ruby\samples\RubySrc-1.8.6\sample
Isuue the following:
C:\ruby\samples\RubySrc-1.8.6\sample>ruby eval.rb
We get ruby> prompt. tape: puts "Regards"
ruby> puts "Regards"
We get:
Regards
nil
quit this prompt:
ruby> exit

C:\ruby\samples\RubySrc-1.8.6\sample>
C:\ruby\samples\RubySrc-1.8.6\sample>ruby eval.rb

5. Strings

Some examples:

ruby> 1+2
3
ruby> "xyz"
"xyz"

ruby> 'xyz'
"xyz"

ruby> puts "x\ny\nz"
x
y
z
nil

ruby> puts 'x\ny\nz'
x\ny\nz
nil

ruby> '\n'
"\\n"

ruby> '\234'
"\\234"

ruby> "abcd #{2*22} xyz"
"abcd 44 xyz"

Concatenation:

ruby> "avail" + "able"
"available"

ruby> "avail" * 3
"availavailavail"

Boolean and equalities

ruby> "gold" == "gold"
true
ruby> "gold" == "diamond"
false
ruby>

Test the calsses:

ruby> @a=-8.7 -8.7 ruby> @a.class Float ruby> @a=123 123 ruby> @a.class Fixnum ruby> @a="gold mouth" "gold mouth" ruby> @a.class String ruby>