SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Sep 2006
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    What am I doing wrong?

    I am trying to create a program that will execute one of several commands, depending on which one I enter, but it's not working. Here is an extremely simple print example that I am using for illustration purposes only. I know that I can print another way, I am just using this as an example. I don't want to use hashs or arrays because some of the commands may be quite lengthy.

    #***************************************
    def dog
    puts 'I am a dog.'
    end

    def cat
    puts 'I am a cat.'
    end

    puts 'Enter command: '
    command_in = gets.chomp.downcase
    command_in
    puts 'Command ' + command_in + " executed.'
    #***************************************

    Here is what my output looks like...

    Enter command:
    (I enter "dog" as the response)
    Command dog executed.

    Here is what I want my output to look like...

    Enter command:
    (I enter "dog" as the response)
    I am a dog.
    Command dog executed.



    How can I get the program to actually do command_in instead of treating it as a literal? I think I may have to create a proc or something, but I don't know how to do it.

  2. #2
    SitePoint Zealot
    Join Date
    Jul 2005
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There has to be a better way to do it, but you could use eval so long as you validate the input:

    http://pastie.caboo.se/12374

  3. #3
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please try and use code blocks when posting code.

    Easiest way would be the following:

    Code:
    module Commands
      def self.dog
        puts 'I am a dog.'
      end
    
      def self.cat
        puts 'I am a cat.'
      end
    end
    
    puts 'Enter command: '
    command_in = gets.chomp.downcase
    Commands.send(command_in)
    puts 'Command ' + command_in + " executed.'
    It has to be said though, allowing users to arbitrarily call methods is somewhat of a security hole. Always remember to validate input.

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2005
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Luke beat me to it, I just remembered send and that all methods are a part of a class, similarly:

    Code:
    def cat; puts "Cat!"; end
    
    def dog; puts "Dog!"; end
    
    puts "Enter a method to execute:"
    desired_method = gets.downcase.chomp
    Object.send(desired_method)
    puts "Executed #{desired_method}."

  5. #5
    SitePoint Member
    Join Date
    Sep 2006
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys. This is all still very confusing to me, as I am an old school COBOL programmer, and have no experience at all in these types of languages. I think it's going to be twice as hard for me, because not only do I need to learn a lot of new habits and conventions, but I need to unlearn old habits and conventions as well. I can see that this stuff requires a different type of thinking. I hope you can put up with me as I continue to ask questions.

  6. #6
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by IAIHMB
    Luke beat me to it, I just remembered send and that all methods are a part of a class, similarly:

    Code:
    def cat; puts "Cat!"; end
    
    def dog; puts "Dog!"; end
    
    puts "Enter a method to execute:"
    desired_method = gets.downcase.chomp
    Object.send(desired_method)
    puts "Executed #{desired_method}."
    Its worth mentioning that this is even more insecure than my method


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •