-2

Hey is anyone aware of how to take user input using a gets.chomp method and then defining that unique user input as a class object? Or any method of course. But sticking to ruby, not rails.

obviously this wont work:

puts "What would you like to save the contact under?"
ncontact = gets.chomp
ncontact = Contacts.new

Since it's just redefining ncontact. Is there a way i can do this? or even if i can maybe rename the object using a self command in a new method. I basically want a user to be able to create a contact on their own and have that contact saved under a name the user wants, and be able to display the attributes(or info) of that contact using another method. the second aprt of that is easy enough.

What i am unable to figure out is how to have a method that will create a new object every time it runs, under a name the user wants it to be.

2
  • Are you wanting to take the contact info and store its contents in an object like so? Example: input is Bill Johnson and this would be stored in contacts.name. Or are you wanting to dynamically create a new class based on this information (ie with reflection)? Nov 1, 2015 at 3:34
  • This seems like a really bad idea. I'd be careful allowing user input to define anything in running code. Storing that input as data is one thing, but allowing a user to define variable or class names seems like a recipe for disaster. And keeping track of it will increase your code overhead needlessly. Nov 1, 2015 at 17:08

1 Answer 1

0

As the Tin Man pointed out - what you are asking about is both risky and strongly discouraged. Or, in his words:

I can't see any reason that doing this would be a good thing. It's akin to the old Perl practice of using a variable's content to act as the variable's name, which is a practice that is strongly discouraged for maintenance and readability reasons by the Perl community.

I agree with his assessment. Having said that, here is one way to do what you asked:


Try to read through RubyMonk's metaprogramming guide, at least through part of it...

You'll come across eval which will get you closer to what you want.

Try, in your example:

puts "What would you like to save the contact under?"
ncontact = gets.chomp
Kernel.eval "#{ncontact} = Contacts.new"

Other, and also safer, solutions include the use of a Hash as a map. i.e.

contacts_map = {}
puts "What would you like to save the contact under?"
ncontact = gets.chomp
contacts_map[ncontact] = Contacts.new

Although I'm not sure why you would either of those... the variable names are only relevant for your own code. The client only sees whatever you show them. Might as well have a property in the Contacts class, such as a show_as field that the client uses for access.

Also, consider the following user input:

exit; name

This might demonstrate some risks inherent in the approach...

The user just forced your application to quit(!). If your code were to run within a server or a public application, this would be a very serious security flaw.

Good luck!

6
  • I can't see any reason that doing this would be a good thing. It's akin to the old Perl practice of using a variable's content to act as the variable's name, which is a practice that is strongly discouraged for maintenance and readability reasons by the Perl community. Nov 1, 2015 at 17:15
  • @theTinMan I agree, not to mention how big of a security risk we are taking by allowing a user's input to run as code, unchecked... (I added this to my answer)... But I'm not sure we should avoid answering the 'how' just because the use-case in the question seems less than a good practice.
    – Myst
    Nov 1, 2015 at 17:33
  • 1
    Answering how is fine, but strongly worded cautions are in order too. Most often people ask such questions because they are dragging bad practices from other languages, or have a bad idea based on a XY Question so educating them is good for everyone. Providing code showing the better way is especially good then. Nov 1, 2015 at 17:47
  • I've been told running eval is even more dangerous as someone with coding knowledge can feed it a code that can destroy your code, reboot your computer, etc
    – Akit Jain
    Nov 1, 2015 at 22:16
  • 2
    eval by itself is neither bad or good. Used correctly it's incredibly powerful, but the problem is it's a loaded gun, and used improperly, or carelessly, it can be dangerous. And unfortunately it is used carelessly too often by people who pass unfiltered user-entered information into it. Nov 2, 2015 at 3:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.