Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions Day1/assignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,26 @@
# --------------------------------------------------------------------------

# 1. Write a method to swap two variables.
# def method(a, b)
# Your code here....
# end
def swap(a,b)
a,b = b,a
end
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 That is the expected code. Good




# 2. Write any one use case of === operator.
# Your answer here...
Used to test equality within a when clause of a case statement.
ex: (1...10) === 5
=> it returns true.



# 3. Print array of alphabates using Range operator.
# Your answer here...
puts ('a'..'z').to_a



# 4. Print 'Ho! Ho! Ho! Merry Christmas!' using string interpolation and * operator.
# Your answer here...
print "Ho! "*3 + "Merry Christmas!"



Expand All @@ -30,3 +32,9 @@
# "Your name is <user's name>"
# "Your age is <user's age>"
# Your answer here...

print "Enter Your Name :"
name = gets
print "Enter Your age :"
age = gets
print "Your Name is : " + name + "Your Age is : " + age
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to create another ruby file like test.rb and copy paste this code and check how it works.
It should be print:

What is your name: <your name here>
What is your age   : <your age>

Your name is <your name>.
Your name is <your age>.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @budhrg

Thank you for comments; I've tried above code using .rb file and execute the same from command prompt.
It gives below result

E:\RubyProg>ruby r1.rb
Enter Your Name :mahesh
Enter Your age :25
Your Name is : mahesh
Your Age is : 25

39 changes: 36 additions & 3 deletions Day2/assignments/arrays_qs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,67 @@
# Declare one array arr1 and method with array as parameter
# return sum of elements in arr1 using each loop
def sum_of_arr(arr)
# do whatever you want...
sum = 0
arr.each do |i|
sum = sum + i
end
sum
end

puts sum_of_arr(array)
=>55


# Question2
# Following method 'push_elements_into_array' accepts two parameters
# 1. array & 2. new_element which user want to insert into array
# Method will return array with new element.
# Add new_element into array using 'push' method of Array
def push_elements_into_array(arr, ele)
# you can do it.. :-)
arr.push(ele)
arr
end

puts push_elements_into array(arr,6)
=>[1, 2, 3, 4, 5, 6]

# Question3
# Pass array to below method
# Iterate on array and puts each element from array
# using 'pop' method of Array
def pop_from_array(arr)
# you can do it.. :-)
l=arr.length
l.each do
num=arr.pop
puts "#{num} is removed!"
end
end

pop_from_array(arr)



# Question4
# a. Declare one array b. sort the array using 'sort' method
# c. reverse the array d. puts element having index = 4
# You can get index directly by using 'each_with_index' method
# eg. a = [1, 2, 3]
# a.each_with_index { |n, index| .... }
# Here, n = element of array & index = index of element in array
a=[9,2,4,6,8,1,3,7,5,10]
=> [9, 2, 4, 6, 8, 1, 3, 7, 5, 10]
a.sort
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a.reverse
=> [10, 5, 7, 3, 1, 8, 6, 4, 2, 9]
puts a[4]
=> 8




# Question5
# str = 'Hello Ruby!!!'
# Write code to store str into array as ['Hello fun', 'Ruby!!! fun']
arr = str.split(/ /)
arr.map {|i| i +" fun"
20 changes: 16 additions & 4 deletions Day2/assignments/file_qs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,27 @@
# with the contents -
# "This is a test.
# Lets learn more about Ruby."
f = File.new("test.txt", "w")
f.write("This is a test.\nLets learn more about ruby.")
f.close


# Question2
# Questiont1.txt", "w")
# Puts each line from file
# Using method 'readlines' to read the above .txt file into an array

File.readlines("test.txt").each do |line|
puts line
end
This is a test.
Lets learn more about ruby.
=> ["This is a test. \n", " Lets learn more about ruby.\n"]

# Question3
# Check if the file new.txt exists.
File.exists?("test.txt")
=>true


# Question4
# Rename the test.txt file powers.txt.
# Rename the test.txt file powers.txt.
File.rename("test.txt","power.txt")
=>0
16 changes: 15 additions & 1 deletion Day2/assignments/string_qs.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
# Question1
# Get the first letter from the string "Hello, Ruby!!".
str[0]
=> "H"

# Question2
# Get the first through 5th elements from the "Hello, Ruby!!" string.
str[4]
=>"0"

# Question3
# Check the 'sprintf' documentation and the % documentation in the String class
# and write one line code to print exact following line
# "1$ = Rs60.48"
sprintf("1$ = Rs %#.2f",60.48)
=> "1$ = Rs 60.48"


# Question4
# What is the problem with the following code? Correct it and puts.
# my variable = 'Mr. White'
my_variable='Mr. White'
puts my_variable
=>Mr. White



# Question5
# Print the result - Concatenate the following strings
first = 'Beautiful '
second = 'face tattoo'
second = 'face tattoo'
puts first+second
=> Beautiful face Tattoo