Question

In: Computer Science

In Ruby, methods are not what is called first-class citizens. However, procs (lambdas as well) and block logic in Ruby are.


In Ruby, methods are not what is called first-class citizens. However, procs (lambdas as well) and block logic in Ruby are. For each of the following descriptions below give an example of a proc in Ruby that satisfies the condition. There should be an example for EACH QUESTION.

QUESTION b)The first class object may be stored in variables (i.e. it may have a symbolic name). Show examples of defining and using named functions.

Solutions

Expert Solution

->You'll often have a piece of code that needs to be executed many times in a program. Instead of writing that piece of code over and over, there's a feature in most programming languages called a procedure, which allows you to extract the common code to one place. In Ruby, we call it a method.

def say

# method body goes here

end

There's a comment in the method body to show you where the logic for the method definition will go.

Suppose we had the following code in a file named say.rb. Create this file and type these examples along.

puts "hello"
puts "hi"
puts "how are you"
puts "I'm fine"

->Notice how we've duplicated the puts many times. We'd like to have one place where we can puts and send that one place the information we want to puts. Let's create a method definition to do that.

def say(words)
  puts words
end
 
say("hello")
say("hi")
say("how are you")
say("I'm fine")

-> We call (or invoke) the method by typing its name and passing in arguments. You'll notice that there's a (words) after say in the method definition. This is what's called a parameter. Parameters are used when you have data outside of a method definition's scope, but you need access to it within the method definition. If the method definition does not need access to any outside data, you do not need to define any parameters.

-> One of the benefits that methods give us is the ability to make changes in one place that affect many places in our program. Suppose we wanted to add a . at the end of every string we send to the say method. We only have to make that change in one place.

def say(words)
  puts words + '.'    ## <= We only make the change here!
end
 
say("hello")
say("hi")
say("how are you")
say("I'm fine")

Default Parameter:

When you're defining methods you may want to structure your method definition so that it always works, whether given parameters or not. Let's restructure our say method definition again so that we can assign a default parameter in case the caller doesn't send any arguments.

def say(words='hello')
  puts words + '.'
end
 
say()
say("hi")
say("how are you")
say("I'm fine")

You'll notice that say() prints hello. to the console. We have provided a default parameter that is used whenever our method is called without any arguments.

Method Definition and Local Variable Scope

Before moving on to the next topic on methods, let's take a moment to discuss the concept of local variable scope within a method definition. A method definition creates its own scope outside the regular flow of execution. This is why local variables within a method definition cannot be referenced from outside of the method definition.

a = 5
 
def some_method
  a = 3
end
 
puts a

What's the value of a? Still 5, because method definitions create their own scope that's entirely outside of the execution flow.


Related Solutions

Write a Java class called CityDistances in a class file called CityDistances.java.    1. Your methods...
Write a Java class called CityDistances in a class file called CityDistances.java.    1. Your methods will make use of two text files. a. The first text file contains the names of cities. However, the first line of the file is a number specifying how many city names are contained within the file. For example, 5 Dallas Houston Austin Nacogdoches El Paso b. The second text file contains the distances between the cities in the file described above. This file...
Question 63.33 pts What is the first block in a blockchain called? Group of answer choices...
Question 63.33 pts What is the first block in a blockchain called? Group of answer choices Genesis block First block Alpha block Initial block Question 53.33 pts Data is permanently recorded on the Bitcoin network through files called Group of answer choices Blocks Nounce Node Safety file
Hi, I want to implement the following methods with a driver class In the comment block...
Hi, I want to implement the following methods with a driver class In the comment block for add, give the best possible big-O of the worst-case running time for executing a single add operations and give the best possible big-O of the total worst-case running time of executing a sequence of N add operations. here is the Implement class: import java.util.Iterator; // Do not modify the given code. @SuppressWarnings("unchecked") // Given public class MyArrayList { private T[] data; // Given...
Overview For this assignment, implement and use the methods for a class called Seller that represents...
Overview For this assignment, implement and use the methods for a class called Seller that represents information about a salesperson. The Seller class Use the following class definition: class Seller { public: Seller(); Seller( const char [], const char[], const char [], double ); void print(); void setFirstName( const char [] ); void setLastName( const char [] ); void setID( const char [] ); void setSalesTotal( double ); double getSalesTotal(); private: char firstName[20]; char lastName[30]; char ID[7]; double salesTotal; };...
Overview For this assignment, implement and use the methods for a class called Seller that represents...
Overview For this assignment, implement and use the methods for a class called Seller that represents information about a salesperson. The Seller class Use the following class definition: class Seller { public: Seller(); Seller( const char [], const char[], const char [], double ); void print(); void setFirstName( const char [] ); void setLastName( const char [] ); void setID( const char [] ); void setSalesTotal( double ); double getSalesTotal(); private: char firstName[20]; char lastName[30]; char ID[7]; double salesTotal; };...
What is the range of address of the 100th block of Class A
What is the range of address of the 100th block of Class A
Write the following methods in java class ARM that represent state information as well as functional...
Write the following methods in java class ARM that represent state information as well as functional blocks of the ARM platform. [Go through the following 5 classes then write methods for the instructions: mov, str, ldr, add in class ARM, finally, write a print method for ARM in Main.java that can display the registers and the memory locations that have been used. (make sure to make a visualization of the print method instead of just a console dump)] --- Please...
For this assignment, implement and use the methods for a class called Seller that represents information about a salesperson.
For this assignment, implement and use the methods for a class called Seller that represents information about a salesperson.The Seller classUse the following class definition:class Seller { public:   Seller();   Seller( const char [], const char[], const char [], double );        void print();   void setFirstName( const char [] );   void setLastName( const char [] );   void setID( const char [] );   void setSalesTotal( double );   double getSalesTotal(); private:   char firstName[20];   char lastName[30];   char ID[7];   double salesTotal; };Data MembersThe data members for the class are:firstName holds the Seller's first namelastName holds the Seller's last nameID holds the Seller's id numbersalesTotal holds the Seller's sales totalConstructorsThis class has two constructors. The default constructor (the one that takes...
Write a class called VLPUtility with the following static methods: Java Language 1. concatStrings that will...
Write a class called VLPUtility with the following static methods: Java Language 1. concatStrings that will accept a variable length parameter list of Strings and concatenate them into one string with a space in between and return it. 2. Overload this method with two parameters, one is a boolean named upper and one is a variable length parameter list of Strings. If upper is true, return a combined string with spaces in upper case; otherwise, return the combined string as...
(a) What is a class? What is an object? What is the relationship? (b) What are the instance variables and methods of a class?
 (a) What is a class? What is an object? What is the relationship? (b) What are the instance variables and methods of a class? (c) What is the effect of declaring instance variables and methods public or private? (d) Why do we often declare the instance variables of classes private? (e) Could we declare methods private? Would we want to do so?  (f) What does the identifier this mean? Give an example of its use (g) What is a constructor? (h) What is inheritance? Why is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT