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
29 changes: 21 additions & 8 deletions Day1/assignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,44 @@
# --------------------------------------------------------------------------

# 1. Write a method to swap two variables.
def method(a, b)
my_cde

def swap(a, b)
a, b = [b, a]
[a, b]
end
Copy link
Collaborator

Choose a reason for hiding this comment

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

Indent the code properly like below. 2 spaces. It looks like editor settings. Ask your friends 👍




# 2. Write any one use case of === operator.
# Your answer here...

def find(a)
(1..10) === a
end



# 3. Print array of alphabates using Range operator.
# Your answer here...


alph = ('a'..'z').to_a
print alph

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


puts "#{'Ho! ' *3} Merry Christmas! "

# 5. Write a ruby program that perform following operations:
# a. Ask user his/her name
# b. Ask user his/her age
# c. Finally, print result in the form
# "Your name is <user's name>"
# "Your age is <user's age>"
# Your answer here...

#---------- using method----------------
def getAndShow
puts "Enter your name"
name = gets()
puts "Enter your Age"
age = gets()
puts "Your name is #{name}"
puts "Your Age is #{age}"
end
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>.

6 changes: 6 additions & 0 deletions Day1/displayUser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
puts "What is your name"
name = gets()
puts "What is your Age"
age = gets()
puts "Hey! your name is #{name}"
puts "And your age is #{age}"
20 changes: 17 additions & 3 deletions Day2/assignments/arrays_qs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
# 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 += i
end
sum
end

# Question2
Expand All @@ -13,15 +17,19 @@ def sum_of_arr(arr)
# 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

# 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.. :-)
while(0 < arr.length())
puts arr.pop()
end
end

# Question4
Expand All @@ -35,3 +43,9 @@ def pop_from_array(arr)
# Question5
# str = 'Hello Ruby!!!'
# Write code to store str into array as ['Hello fun', 'Ruby!!! fun']
str = 'Hello Ruby!!!'
str = str.split(" ")
str[0] += ' Fun'
str[1] += 'Fun'
str

Choose a reason for hiding this comment

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

👍

Copy link
Collaborator

Choose a reason for hiding this comment

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

👍 Best for this small assignment. But ideal answer is using map function. In real world, the length of array is unknown and this solution will not be work for large array. 😄

8 changes: 7 additions & 1 deletion Day2/assignments/control_statements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
# Return true if any of the elements in the random_keys array
# start with the letter "a" and false otherwise.
random_keys = ["all", "alone", "children"]
random_keys.each do |i|
puts i.include? ("a")
end

# Question2
# Return true if the string "stimpy" contains
# the letter "s" and false otherwise.
# the letter "s" and false otherwise.
str = "stimpy"
str.include? ("s")