Question

IN RUBY LANGUAGE. Write a single Ruby demo program that illustrates the use of all main...

IN RUBY LANGUAGE.

Write a single Ruby demo program that illustrates the use of all main Ruby iterators (loop, while, until, for, upto, downto, times, each, map, step, collect, select, reject). The program should have a few lines illustrating loop, followed by a few lines illustrating while, and so on).

PLEASE HAVE COPYABLE CODE. NOT A PICTURE. THANK YOU.

Homework Answers

Answer #1

Loop

i = 0
loop do
  i += 1
  puts i
  break         # this will cause execution to exit the loop
end

While:

x = 4

# using while loop

# here conditional is x i.e. 4

while x >= 1

# statements to be executed

  puts "Hi"

  x = x - 1

# while loop ends here

end

FOR:

i = "YO"

# using for loop with the range

for a in 1..5 do    

puts i

end

Until:

var = 7

# using until loop

# here do is optional

until var == 11 do

  # code to be executed

  puts var * 10

  var = var + 1

# here loop ends

end

EACH:

# using each iterator

# here collection is range

# variable name is i

(0..9).each do |i|     

    # statement to be executed

    puts i    

end

Collect:

a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

  

# using collect iterator

# printing table of 5

b = a.collect{ |y| (5 * y) }

puts b

TIMES:

# using times iterator by providing

# 7 as the iterate value

7.times do |i|

    puts i

end

UPTO:

# using upto iterator

# here top value is 4

# bottom value is 7

4.upto(7) do |n|   

  puts n   

end  

  

# here top > bottom

# so no ouput

7.upto(4) do |n|   

  puts n   

end  

DOWNTO:

# using downto iterator

# here top value is 7

# bottom value is 4

7.downto(4) do |n|   

  puts n   

end  

  

# here top < bottom

# so no output

4.downto(7) do |n|   

  puts n   

end  

STEP:

# using step iterator

# skipping value is 10

# (0..60 ) is the range

(0..60).step(10) do|i|

    puts i

end

EACH_LINE:

# using each_line iterator

"Welcome\nto\nRailway\nPortal".each_line do|i|

puts i

end

MAP:

array = [1,2,3]

array.map { |n| n * 2 }

# [2, 4, 6] is output

STEP:

# Increment from 0 to 10, by 2 each time.

# ... Name the iteration variable "v".

0.step(10, 2) do |v|

   puts v

end

Collect:

# Ruby code for collect() method

  

# declaring array

a = [1, 2, 3, 4]

  

# invoking block for each element

puts "collect a : #{a.collect {|x| x + 1 }}\n\n"

Select:

  

# declaring array

c = [18, 22, 3, 3, 50, 6]

  

# select

puts "select method : #{a.select {|num| num > 10 }}\n\n"

Reject:

# declaring array

c = [18, 22, 3, 3, 50, 6]

  

# reject

puts "reject method : #{a.reject {|num| num > 10 }}\n\n"

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Convert this C++ program exactly as you see it into x86 assembly language: // Use the...
Convert this C++ program exactly as you see it into x86 assembly language: // Use the Irvine library for the print function #include <iostream> // The string that needs to be printed char word[] = "Golf\0"; // Pointer to a specific character in the string char * character = word; //NOTE: This main() function is not portable outside of Visual Studio void main() { // Set up a LOOP - See the while loop's conditional expression below int ecx =...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how the firms resources incompetencies support the given pressures regarding costs and local responsiveness. Describe entry modes have they usually used, and whether they are appropriate for the given strategy. Any key issues in their global strategy? casestudy: Atlanta, June 17, 2014. Sea of Delta employees and their families swarmed between food trucks, amusement park booths, and entertainment venues that were scattered throughout what would...
Mattel Responds to Ethical Challenges Business Ethics This case was written by Debbie Thorne, John Fraedrich,...
Mattel Responds to Ethical Challenges Business Ethics This case was written by Debbie Thorne, John Fraedrich, O. C. Ferrell, and Jennifer Jackson, with the editorial assistance of Jennifer Sawayda. This case was developed for classroom discussion rather than to illustrate either effective or ineffective handling of an administrative, ethical, or legal discussion by management. All sources used for this case were obtained through publicly available material. Mattel, Inc. is a world leader in the design, manufacture, and marketing of family...
Sign In INNOVATION Deep Change: How Operational Innovation Can Transform Your Company by Michael Hammer From...
Sign In INNOVATION Deep Change: How Operational Innovation Can Transform Your Company by Michael Hammer From the April 2004 Issue Save Share 8.95 In 1991, Progressive Insurance, an automobile insurer based in Mayfield Village, Ohio, had approximately $1.3 billion in sales. By 2002, that figure had grown to $9.5 billion. What fashionable strategies did Progressive employ to achieve sevenfold growth in just over a decade? Was it positioned in a high-growth industry? Hardly. Auto insurance is a mature, 100-year-old industry...
What tools could AA leaders have used to increase their awareness of internal and external issues?...
What tools could AA leaders have used to increase their awareness of internal and external issues? ???ALASKA AIRLINES: NAVIGATING CHANGE In the autumn of 2007, Alaska Airlines executives adjourned at the end of a long and stressful day in the midst of a multi-day strategic planning session. Most headed outside to relax, unwind and enjoy a bonfire on the shore of Semiahmoo Spit, outside the meeting venue in Blaine, a seaport town in northwest Washington state. Meanwhile, several members of...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT