-
Notifications
You must be signed in to change notification settings - Fork 41
Please review my Day1 assignment #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
d4eb61f
d76ca8c
7f15ccc
2a51c79
cb0ebae
0191bb9
b2c2275
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
||
|
|
||
|
|
||
| # 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 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. What is your name: <your name here>
What is your age : <your age>
Your name is <your name>.
Your name is <your age>. |
||
| 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}" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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,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 | ||
|
|
@@ -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 | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 Best for this small assignment. But ideal answer is using |
||
There was a problem hiding this comment.
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 👍