Date post: | 18-Jul-2016 |
Category: |
Documents |
Upload: | sujathalavi |
View: | 17 times |
Download: | 1 times |
1) Write a program to explain the class and object concept in Ruby.
#!/usr/bin/ruby
class Customer @@no_of_customers=0 def initialize(id, name, addr) @cust_id=id @cust_name=name @cust_addr=addr end def display_details() puts "Customer id #@cust_id" puts "Customer name #@cust_name" puts "Customer address #@cust_addr" end def total_no_of_customers() @@no_of_customers += 1 puts "Total number of customers: #@@no_of_customers" endend
# Create Objectscust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya")cust2=Customer.new("2", "Poul", "New Empire road, Khandala")
# Call Methodscust1.display_details()cust1.total_no_of_customers()cust2.display_details()cust2.total_no_of_customers()
Output:
>ruby cust.rbCustomer id 1Customer name JohnCustomer address Wisdom Apartments, LudhiyaTotal number of customers: 1Customer id 2Customer name PoulCustomer address New Empire road, KhandalaTotal number of customers: 2
2) Write a program to read the content in the file using file concepts in ruby.
#!/usr/bin/ruby
full= File.open(“file1.txt”)phrase=["phrase1","phrase2","phrase3"]count=0full.each do |line|first=[]first=line.split(/\|/)first.each do |single|sub=single.strip!main = (sub).to_s + ” “+(phrase [count]).to_sputs maincount+=1endend
file1.txt
item1 | item2 | item3 | item4 |item11 | item21 | item31 | item41 |item12 | item23 | item34 | item45 |item13 | item23 | item33 | item43 |item14 | item24 | item34 | item44 |
Output:
>ruby file.rbitem1 phrase1item2 phrase2item3 phrase3item4
item11item21item31item41
item12item23
item34item45
item13item23item33item43
item14item24item34item44
3) Write a program to create a user defined function and how to call the function in ruby.
#!/usr/bin/ruby
def rectarea(l,b)area = (l*b)perimeter = 2*(l+b)return area, perimeterendArea,Perimeter = rectarea(15,20)puts Area, Perimeter
Output:
30070
4) Write a program to retrieve data from the database
#!/usr/bin/ruby -w
require "dbi"
begin # connect to the MySQL server dbh = DBI.connect("DBI:Mysql:TESTDB:localhost",
"testuser", "test123") dbh.do( "INSERT INTO EMPLOYEE(FIRST_NAME, LAST_NAME, AGE,
SEX, INCOME)
VALUES ('Mac', 'Mohan', 20, 'M', 2000)" ) puts "Record has been created" dbh.commitrescue DBI::DatabaseError => e puts "An error occurred" puts "Error code: #{e.err}" puts "Error message: #{e.errstr}" dbh.rollbackensure # disconnect from server dbh.disconnect if dbhend
Output:
Employee Table
Mac Mohan 20 M 2000
Record has been created
5) Write a program for create Rails Controllers and Rails Views
book_controller.rb
class BookController < ApplicationController def list @books = Book.find(:all) end def show @book = Book.find(params[:id]) end def new @book = Book.new @subjects = Subject.find(:all) end def create @book = Book.new(params[:book]) if @book.save redirect_to :action => 'list' else @subjects = Subject.find(:all) render :action => 'new' end end def edit @book = Book.find(params[:id]) @subjects = Subject.find(:all) end def update @book = Book.find(params[:id]) if @book.update_attributes(params[:book]) redirect_to :action => 'show', :id => @book else @subjects = Subject.find(:all) render :action => 'edit' end end def delete Book.find(params[:id]).destroy redirect_to :action => 'list' end def show_subjects @subject = Subject.find(params[:id]) endend
list.rhtml
<ul id="subjects"><% Subject.find(:all).each do |c| %><li><%= link_to c.name, :action => "show_subjects", :id => c.id %></li><% end %></ul><% if @books.blank? %><p>There are not any books currently in the system.</p><% else %><p>These are the current books in our system</p><ul id="books"><% @books.each do |c| %><li><%= link_to c.title, {:action => 'show', :id => c.id} -%><b> <%= link_to 'Edit', {:action => 'edit', :id => c.id} %></b><b> <%= link_to "Delete", {:action => 'delete', :id => c.id},:confirm => "Are you sure you want to delete this item?" %></b></li><% end %></ul><% end %><p><%= link_to "Add new Book", {:action => 'new' }%></p>
new.rhtml
<h1>Add new book</h1><%= start_form_tag :action => 'create' %><p><label for="book_title">Title</label>:<%= text_field 'book', 'title' %></p><p><label for="book_price">Price</label>:<%= text_field 'book', 'price' %></p><p><label for="book_subject">Subject</label>:<%= collection_select(:book,:subject_id,@subjects,:id,:name) %></p><p><label for="book_description">Description</label><br/><%= text_area 'book', 'description' %></p><%= submit_tag "Create" %><%= end_form_tag %><%= link_to 'Back', {:action => 'list'} %>
show.rhtml
<h1><%= @book.title %></h1><p><strong>Price: </strong> $<%= @book.price %><br />< <strong>Subject: </strong> <%= link_to @book.subject.name,:action => "show_subjects", :id => @book.subject.id %><br /> <strong>Created Date:</strong> <%= @book.created_at %><br /></p><p><%= @book.description %></p><hr /><%= link_to 'Back', {:action => 'list'} %>
edit.rhtml
<h1>Edit Book Detail</h1><%= start_form_tag :action => 'update', :id => @book %><p><label for="book_title">Title</label>:<%= text_field 'book', 'title' %></p><p><label for="book_price">Price</label>:<%= text_field 'book', 'price' %></p><p><label for="book_subject">Subject</label>:<%= collection_select(:book, :subject_id, @subjects, :id, :name) %></p><p><label for="book_description">Description</label><br/><%= text_area 'book', 'description' %></p><%= submit_tag "Save changes" %><%= end_form_tag %><%= link_to 'Back', {:action => 'list' } %>
show_subjects.rhtml
<h1><%= @subject.name -%></h1><ul><% @subject.books.each do |c| %><li><%= link_to c.title, :action => "show", :id => c.id -%></li><% end %></ul>
8) Design a webpage that should compute one’s age on a given date using PHP.
http://www.phpro.org/examples/Calculate-Age-With-PHP.html
http://www.populate.it/age_on_date_php.html
9) Design a webpage to generate multiplication table for a given number
http://if.invisionfree.com/topic/471433/1/
function multiplytable($number, $maximum = 15) { for ($i=1; $i <= $maximum; $i++ ) { echo $number . " times " . $i . "=" . ($i * $number) . "\n"; } }
then, you can do
multiplytable(34); // 34 * 1 to the default 34 * 15
or
multiplytable(34,23); // 34 * 1 all the way to 34 * 23
http://codepad.org/b6wN93Q1
10) Design a authentication web page in PHP with MySQL to check user name andpassword
login.html
<head><title>Login Form</title></head><body> <form method="post" action="validate_login.php" > <table border="1" > <tr> <td><label for="username">Username</label></td> <td><input type="text" name="username" id="username"></td> </tr> <tr> <td><label for="password">Password</label></td> <td><input name="password" type="password" id="password"></input></td> </tr> <tr> <td><input type="submit" value="Submit"/> <td><input type="reset" value="Reset"/> </tr> </table> </form></body></html>
validate_login.php
<?php
// Grab User submitted information$email = $_POST["users_email"];$pass = $_POST["users_pass"];
// Connect to the database$con = mysql_connect("localhost","root","");// Make sure we connected succesfullyif(! $con){ die('Connection Failed'.mysql_error());}
// Select the database to usemysql_select_db("my_dbname",$con);
$result = mysql_query("SELECT users_email, users_pass FROM users WHERE users_email = $email");
$row = mysql_fetch_array($result);
if($row["users_email"]==$email && $row["users_pass"]==$pass) echo"You are a validated user.";else echo"Sorry, your credentials are not valid, Please try again.";?>