diff --git a/Day1/assignment.rb b/Day1/assignment.rb index f5e05fe..f7d2832 100644 --- a/Day1/assignment.rb +++ b/Day1/assignment.rb @@ -2,26 +2,30 @@ # -------------------------------------------------------------------------- # 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 # 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 @@ -29,4 +33,13 @@ def method(a, b) # c. Finally, print result in the form # "Your name is " # "Your age is " -# 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 diff --git a/Day1/displayUser.rb b/Day1/displayUser.rb new file mode 100644 index 0000000..4f50c88 --- /dev/null +++ b/Day1/displayUser.rb @@ -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}" diff --git a/Day2/assignments/arrays_qs.rb b/Day2/assignments/arrays_qs.rb index a4d5731..0fb7802 100644 --- a/Day2/assignments/arrays_qs.rb +++ b/Day2/assignments/arrays_qs.rb @@ -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 @@ -13,7 +17,9 @@ 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 @@ -21,7 +27,9 @@ def push_elements_into_array(arr, ele) # 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 @@ -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 + diff --git a/Day2/assignments/control_statements.rb b/Day2/assignments/control_statements.rb index 50593cc..05c645f 100644 --- a/Day2/assignments/control_statements.rb +++ b/Day2/assignments/control_statements.rb @@ -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. \ No newline at end of file +# the letter "s" and false otherwise. +str = "stimpy" +str.include? ("s") +