Ruby Introduction

Ruby Introduction

  • Ruby is a general-purpose, interpreted and pure object-oriented programming language.
  • It was created in 1993 by Yukihiro Matsumoto of Japan.
  • Features:
    • It is an open-source and is free.
    • Interpreted programming language.
    • True object-oriented programming language.
    • Server-side scripting language.
    • Common Gateway Interface (CGI) scripts.
    • Ruby code can be embedded into Hypertext Markup Language (HTML).
    • Very much scalable and easily maintainable.
    • Used to develop the Internet and intranet applications.
    • Support many GUI tools such as Tcl/Tk, GTK, and OpenGL.
    • Supports to connect databases like DB2, MySQL, Oracle, and Sybase.
  • First Program
    
    #!/usr/bin/ruby -w
    puts "First Program!";
    
  • Run program use the ruby command.
    $ruby test.rb
    
    prints the result: First Program!
    
  • Strings with multiple lines:
    #!/usr/bin/ruby -w
    
    print <<EOF
    line1
    line2
    EOF
    
    Output:
    line1
    line2
    
  • Same as above, in other way.
    #!/usr/bin/ruby -w
    print <<"EOF";
    line1
    line2
    EOF
    
    output:
    line1
    line2
    
  • To execute and print multiple commands.
    #!/usr/bin/ruby -w
    print <<`EOC` # execute commands
    echo 'line1'
    echo 'line2'
    EOC
    
    
    Output:
    line1
    line2
    
  • Connects Postgresql database in ruby code:

  • 		rows=Array.new
    		con = PG.connect :host => 'host_name', :dbname => 'database name', :user => 'db user name', :password => 'db user password' 
    		rs = con.exec "select emp.first_name || ' ' || emp.last_name as employee_name,"+
    				"emp.start_date, emp.end_date, dp.designation, emp.salary, emp.email "+
    				"from employees emp "+
    				"join department dp on (emp.employee_id = dp.employee_id) "+
    				"where dp.department_id = '2';"
    		rs.each do |row|
    			obj = { "name" => row['employee_name'],"start_date"=>row['start_date'],"end_date"=>row["end_date"],"designation"=>row['designation'],"salary"=>row['salary'],"email"=>row['email']};
    			rows.push(obj)
    		end
        
    		rescue PG::Error => e
    			#rows.push(e.message)
    			puts e.message   
    		ensure
    	    		rs.clear if rs
    	    		con.close if con
    
Ruby installation

Privacy Policy  |  Copyright@2017 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Free online programming tutorials

Email Facebook Google LinkedIn Twitter
^