WT Assignment 6
1. Explain the control statements and arrays in Ruby.
Control Statements: Ruby has several control statements to manage the flow of execution in a
program, including if, unless, case, and loops like while, for, and each.
if-else: Executes code based on a condition.
age = 18
if age >= 18
puts "You are an adult."
else
puts "You are a minor."
End
unless: Executes code if a condition is false.
unless age < 18
puts "You are eligible to vote."
End
case-when: Similar to switch statements in other languages, executes code based on
matching cases.
day = "Tuesday"
case day
when "Monday"
puts "Start of the week"
when "Tuesday"
puts "Second day of the week"
else
puts "Other day"
end
Loops: Ruby supports loops like while, until, for, and each
# while loop
i=1
while i <= 5 do
puts i
i += 1
end
# each loop (works with arrays)
[1, 2, 3].each do |num|
puts num
end
Arrays: In Ruby, arrays can store elements of any type and are dynamic in size.
Creating Arrays: Use square brackets [] or the Array.new constructor.
numbers = [1, 2, 3, 4]
fruits = ["apple", "banana", "cherry"]
Accessing Elements: Arrays are zero-indexed in Ruby.
puts fruits[1] # Outputs "banana"
Array Methods: Ruby provides various methods for array manipulation.
fruits.push("orange") # Adds "orange" to the array
fruits.pop # Removes the last element
fruits.include?("apple") # Returns true if "apple" is in the array
fruits.each { |fruit| puts fruit } # Iterates over each element
2. Explain the concept of layouts & document requests in Rails.
Layouts: In Ruby on Rails, layouts are used to provide a consistent look and feel across multiple views
in an application. A layout file typically includes HTML structure, navigation menus, footers, and
other elements shared across pages. By default, layouts are located in the app/views/layouts
directory.
Example: The application.html.erb layout might look like this:
<!DOCTYPE html>
<html>
<head>
<title>MyApp</title>
</head>
<body>
<%= yield %> <!-- This renders the specific view content -->
</body>
</html>
Document Requests: In Rails, controllers handle document requests by responding with specific
views or performing actions, such as rendering JSON data or redirecting to another action. When a
request is made to the server, Rails maps it to a controller and action, processes the action, and then
returns a response (usually an HTML document or JSON).
3. Explain the classes in Ruby. Elaborate on pattern matching in Ruby.
Classes: Classes in Ruby define blueprints for creating objects. They encapsulate methods and
attributes to model real-world entities.
Example:
class Car
attr_accessor :make, :model
def initialize(make, model)
@make = make
@model = model
end
def details
"This car is a #{@make} #{@model}."
end
end
car = Car.new("Toyota", "Corolla")
puts car.details # Outputs "This car is a Toyota Corolla."
Pattern Matching: Introduced in Ruby 2.7, pattern matching allows destructuring and testing object
structures, useful for conditionally processing different data types or complex structures.
Example:
case {name: "Alice", age: 30}
in {name: "Alice", age: age}
puts "Alice is #{age} years old."
in {name: "Bob", age: age}
puts "Bob is #{age} years old."
else
puts "Unknown person"
end
4. What is EJB? Explain types of EJB
EJB Overview: Enterprise JavaBeans (EJB) is a server-side component architecture for modular
enterprise applications. EJBs encapsulate business logic, enabling secure, scalable, and transactional
application development.
Types of EJB:
1. Session Beans: Handle business logic for clients.
o Stateless Session Beans: Do not maintain state across multiple method calls. Ideal
for short, one-time operations.
o Stateful Session Beans: Maintain a conversational state with the client across
multiple method calls.
2. Entity Beans: Represent persistent data stored in a database. Now largely replaced by JPA
(Java Persistence API).
3. Message-Driven Beans (MDB): Enable asynchronous communication between systems by
acting as message consumers in a JMS (Java Messaging Service) environment.
5. Explain rails with request and response in rail application.
In a Rails application, the flow of a request and response works as follows:
1. Request: When a user accesses a URL, a request is sent to the Rails server. The router
processes the URL and maps it to a specific controller action.
2. Controller Action: The controller processes the request, interacting with models as needed
to retrieve or manipulate data.
3. View: The controller then renders a view (usually in ERB) or returns data (e.g., JSON for
APIs).
4. Response: Rails generates the response (HTML, JSON, or other formats) and sends it back to
the client.
Example:
# In routes.rb
get '/welcome', to: 'pages#welcome'
# In pages_controller.rb
class PagesController < ApplicationController
def welcome
@message = "Hello, Rails!"
end
end
# In welcome.html.erb
<h1><%= @message %></h1>
6. What are the string operation available in RUBY.
Ruby provides numerous methods for manipulating strings. Here are some commonly used ones:
Concatenation:
str1 = "Hello"
str2 = "World"
puts str1 + " " + str2 # "Hello World"
Interpolation:
name = "Alice"
puts "Hello, #{name}!" # "Hello, Alice!"
String Length:
str = "Hello"
puts str.length # 5
Substring:
str = "Hello"
puts str[1, 3] # "ell"
Replace:
str = "Hello"
puts str.gsub("e", "a") # "Hallo"
Case Conversion:
str = "Hello"
puts str.upcase # "HELLO"
puts str.downcase # "hello"
Split:
str = "apple,banana,cherry"
fruits = str.split(",") # ["apple", "banana", "cherry"]
Reverse:
str = "Ruby"
puts str.reverse # "ybuR"