I want to be able to name methods dynamically (I would not leave it up to user input to do this, but as an example):
puts ""
foo = gets
def (whatever the user inputted for foo)
end
How can I do this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can do this using the
sendmethod to send a message to the class, using the parameter:define_methodto tell it you are going to define a new method for that class.For example, having a class
CarA call to
c.soundbrings about the errorBut after defining the name of the method and sending it to the class:
The call to
c.soundnow brings the output