ruby programming language

Learn from examples

1. Program to output an integer, a string, and a floating point number, initially just one method. exc1.rb source code # exrb1.rb first example to declare and output a string, and integers # and floating point numbers. printf "ruby exrb1.rb running \n" msg = "my string" i = 9999 j = 7 x = 37.0 y = 1.273e+12 z = Complex(1.5,2.75) puts msg puts "any sring" printf "%s \n", msg puts "i= #{i} , j= #{j}" printf "i= %d , j= %d \n", i, j puts "x = #{x}" printf "x = %6.2f \n", x puts "y = #{y}" printf "y = %f \n", y printf "y = %e \n", y puts "z= #{z}" Output from execution: exc1_rb.out source output ruby exrb1.rb running my string any sring my string i= 9999 , j= 7 i= 9999 , j= 7 x = 37.0 x = 37.00 y = 1273000000000.0 y = 1273000000000.000000 y = 1.273000e+12 z = 1.5+2.75i 2. commands to execute the source code at a minimum, Windows, Linux, MacOSX. Windows: optional write output to file ruby exrb1.rb ruby exrb1.rb > exrb1_rb.out Linux: ruby exrb1.rb MacOSX ruby exrb1.rb 3. You must be able to declare variables and arrays and matrix of various types. exrb3.rb source code # exrb3.rb example to declare arrays and list and matrix # of various types printf "ruby exrb3.rb running\n" names = ['Matthew', 'Mark', 'John'] print names puts " " print names[1..2] puts " " printf "allocate and initialize array a1[4]\n" a1 = Array.new(4) for i in 0..3 a1[i] = i*i end for i in 0..3 puts "a1[#{i}]=#{a1[i]}" end puts " " imax = 3 jmax = 4 printf "allocate and initialize matrix m1[3][4]\n" m1 = Array.new(imax+1) m1.map!{Array.new(jmax+1)} # does second dimension for i in 0..imax for j in 0..jmax m1[i][j] = i+j end end for i in 0..imax for j in 0..jmax puts "m1[#{i}][#{j}] = #{m1[i][j]} " end end printf "exrb3.rb finished\n" Execution output: ruby exrb3.rb running ["Matthew", "Mark", "John"] ["Mark", "John"] allocate and initialize array a1[4] a1[0]=0 a1[1]=1 a1[2]=4 a1[3]=9 allocate and initialize matrix m1[3][4] m1[0][0] = 0 m1[0][1] = 1 m1[0][2] = 2 m1[0][3] = 3 m1[0][4] = 4 m1[1][0] = 1 m1[1][1] = 2 m1[1][2] = 3 m1[1][3] = 4 m1[1][4] = 5 m1[2][0] = 2 m1[2][1] = 3 m1[2][2] = 4 m1[2][3] = 5 m1[2][4] = 6 m1[3][0] = 3 m1[3][1] = 4 m1[3][2] = 5 m1[3][3] = 6 m1[3][4] = 7 exrb3.rb finished 4. You need to be able to have loops, iteration statements exrb4.rb source code # exrb4.rb example iteration, loop, while, case printf "ruby exrb4.rb running \n" puts "simple forms of ruby for, while, case, statements" puts "for i in 0..3 # .. <=3" for i in 0..3 print i print "\n" end print "\n" puts "for i in 0...3 # ... <3" for i in 0...3 print i print "\n" end print "\n" puts "for i in 0...4 # ... < next 2" for i in 0...4 if i==2 next end # if print i print "\n" end print "\n" puts "for i in 0...4 # ... < break 2" for i in 0...4 if i==2 break end # if print i print "\n" end print "\n" puts "for i in 3.downto(0) # like for .. >=0 " for i in 3.downto(0) print i print "\n" end # i print "\n" j=2 i=0 print "j = " print j print " i = " print i print "\n" print "while i<4 && j==2 || j>5 \n" while i<4 && j==2 || j>5 print i print "\n" i=i+1 end # while print "\n" j=2 i=0 puts "j = #{j} i = #{i}" puts "while i<4 and j==2 or j>5 " while i<4 and j==2 or j>5 puts "#{i}" i=i+1 end # while puts " " puts "for i in 0..9 " puts " case i" puts " when 1, 3..5 # i== any 1, 3, 4, 5" puts " do_something" puts " when 2, 6..8 # i== any 2, 6, 7, 8" puts " do_something" puts " end # case" puts "end # i" for i in 0..9 puts "i=#{i}" case i when 1, 3..5 puts "i in 1, 3..5" when 2, 6..8 puts "i in 2, 6..8" end # case end # i puts " " printf "exrb4.rb finished \n" Execution output: ruby exrb4.rb running simple forms of ruby for, while, case, statements for i in 0..3 # .. <=3 0 1 2 3 for i in 0...3 # ... <3 0 1 2 for i in 0...4 # ... < next 2 0 1 3 for i in 0...4 # ... < break 2 0 1 for i in 3.downto(0) # like for .. >=0 3 2 1 0 j = 2 i = 0 while i<4 && j==2 || j>5 0 1 2 3 j = 2 i = 0 while i<4 and j==2 or j>5 0 1 2 3 for i in 0..9 case i when 1, 3..5 # i== any 1, 3, 4, 5 do_something when 2, 6..8 # i== any 2, 6, 7, 8 do_something end # case end # i i=0 i=1 i in 1, 3..5 i=2 i in 2, 6..8 i=3 i in 1, 3..5 i=4 i in 1, 3..5 i=5 i in 1, 3..5 i=6 i in 2, 6..8 i=7 i in 2, 6..8 i=8 i in 2, 6..8 i=9 exrb4.rb finished 5. You need if then else conditional statements exrb5.rb source code # exrb5.rb example of if then else printf "ruby exrb5.rb running \n" Pi = 3.14159265358979323846 # radian ang = 210.0 # degree puts "if (ang/180.0)*Pi > Pi" if (ang/180.0)*Pi > Pi printf "Pi= %25.20f \n", Pi printf "210 degree= %25.20f radian \n", ((210.0/180.0)*Pi) end j = 2 puts "if j==2" if j==2 puts "j is 2" elsif j==3 puts "j is 3" else puts "j not 2 or 3" end puts "if j!=1" if j!=1 puts " j is not 1" elsif j==2 puts "j is 2" else puts "j not 1 or 2" end i = 1 puts "if j != i || j > i && j <= i+2" if j != i || j > i && j <= i+2 puts "C syntax for compare, and, or" end printf "exrb5.rb finished \n" Execution output: ruby exrb5.rb running if (ang/180.0)*Pi > Pi Pi= 3.14159265358979311600 210 degree= 3.66519142918809226472 radian if j==2 j is 2 if j!=1 j is not 1 if j != i || j > i && j <= i+2 C syntax for compare, and, or exrb5.rb finished 6. You need to be able to create functions, procedures, subroutines. exrb6.rb source code # exrb6.rb example to create function and procedure and call printf "ruby exrb6.rb running \n" def f(p) # test function return p*p; end def fact(n) # computes return if n == 0 1 else n * fact(n-1) end end def makerand(y) # sets array n = y.length for i in 0..n y[i] = rand end end x = f(2.0) puts "x=f(2.0)= #{x}" nf = fact(9) puts "9! = #{nf}" a = Array.new(3) makerand(a) for i in 0..3 puts "a[#{i}]=#{a[i]}" end printf "exrb6.rb finished \n" Execution output: ruby exrb6.rb running x=f(2.0)= 4.0 9! = 362880 a[0]=0.9816177368740102 a[1]=0.21204745842106187 a[2]=0.01905480591634323 a[3]=0.05222179985963349 exrb6.rb finished 7. You need to be able to read and write files in various formats. exrb7.rb source code # exrb7.rb example to read and write files printf "ruby exrb7.rb running " puts "example read and print file readfile.dat" counter = 1 f = File.new("readfile.dat", "r") while (line = f.gets) puts "#{counter}: #{line}" counter = counter + 1 end f.close puts "example write a file writefile.dat" outp = File.open("writefile.dat", 'w+') # + allows read for i in 1..3 outp.print i outp.print " line " outp.puts(" print and puts") end outp.rewind for i in 1..3 puts(outp.gets) end outp.close puts "exrb7.rb finished" Execution output: ruby exrb7.rb running example read and print file readfile.dat 1: Just data for testing 2: second line 3: third and last line example write a file writefile.dat 1 line print and puts 2 line print and puts 3 line print and puts exrb7.rb finished 8. You need to be able to use a number of files combined to build a program. This typically uses class definitians in a file and class used in other programs. A very simple example, class in foo.rb, use in bar.rb. foo.rb source code bar.rb source code bar_rb.out output exrb8.rb source code Execution output: ruby exrb8.rb running file foo.rb in this directory: #file: foo.rb file name not upper case class Foo # class name starts with upper case letter def initialize puts 'foo' # any ruby code here end end file bar.rb in this directory: #file: bar.rb file name not upper case require_relative 'foo' # this gets Foo class Foo.new runs class initialize exrb8.rb finished A much larger example: test_deriv.rb uses classes from inverse.rb and deriv.rb inverse.rb source code deriv.rb source code test_deriv.rb source code test_deriv_rb.out output Now, you are ready for a more brief language summary Many ruby examples: source code and output: test_arith.rb source code test_arith_rb.out output math_funct.rb source code math_funct_rb.out output test_matrix.rb source code test_matrix_rb.out output test_solve.rb source code test_solve_rb.out output test_eigen.rb source code test_eigen_rb.out output Poly_area.rb source code class test_poly_area.rb source code test_poly_area_rb.out output Deriv.rb source code class test_deriv.rb source code test_deriv_rb.out output Inverse.rb source code class Deriv.rb source code class pde11_eq.rb source code pde11_eq_rb.out output pde44_eq.rb source code pde44_eq_rb.out output Simeq_newton5.rb source code class test_simeq_newton5.rb source code test_simeq_newton5_rb.out output Poly.rb source code class test_poly.rb source code test_poly_rb.out output Poly2.rb source code class test_poly2.rb source code test_poly2_rb.out output Poly3.rb source code class test_poly3.rb source code test_poly3_rb.out output Poly4.rb source code class test_poly4.rb source code test_poly4_rb.out output

Last updated 11/18/2020