A hopefully simple question

Discussion in 'Ruby' started by Chuft Captain, Apr 9, 2009.

  1. Hello,

    I downloaded an irc bot written in Ruby. I haven't programmed in many
    years but couldn't resist the urge to tinker with the code so I peeked
    inside.

    Basically I am trying to add additional string recognition statements to
    this bot, so it says things in response to things people say in the
    channel.

    For example the code contains this statement:

    elsif msg.text == "lol"
    reply(msg, " LOLOLOLOL")

    This works fine. However I would like to have it respond if the
    statement being evaluated has "lol" anywhere in it, not just if it is
    exactly "lol". In other words, I want to search their string, which
    appears to be in the variable msg.text, to see if it contains "lol". In
    the past, I have found this to be a simple matter in other languages.

    I have been unable to find out how to do this in Ruby. If I try this:

    elsif msg.text.include? "lol"
    reply(msg, " LOLOLOLOL")

    the bot either does not run at all, or else quits immediately. Similar
    instant failure occurs when I try

    elsif msg.text.include?("lol")
    reply(msg, " LOLOLOLOL")

    or

    elsif msg.text =~ "lol"
    reply(msg, " LOLOLOLOL")


    Is there a simple way to accomplish this task by replacing the ==
    comparison operator with something else that searches a string variable
    for a substring?

    Thanks
    --
    Posted via http://www.ruby-forum.com/.
    Chuft Captain, Apr 9, 2009
    #1
    1. Advertising

  2. Chuft Captain wrote:
    > Hello,
    >
    > I downloaded an irc bot written in Ruby. I haven't programmed in many
    > years but couldn't resist the urge to tinker with the code so I peeked
    > inside.
    >
    > Basically I am trying to add additional string recognition statements to
    > this bot, so it says things in response to things people say in the
    > channel.
    >
    > Is there a simple way to accomplish this task by replacing the ==
    > comparison operator with something else that searches a string variable
    > for a substring?
    >
    > Thanks
    >

    /Everybody stand back/
    I know regular expressions

    if msg.text =~ %r{lol}i
    reply(msg, " LOLOLOLOL")

    =======================================================================
    This email, including any attachments, is only for the intended
    addressee. It is subject to copyright, is confidential and may be
    the subject of legal or other privilege, none of which is waived or
    lost by reason of this transmission.
    If the receiver is not the intended addressee, please accept our
    apologies, notify us by return, delete all copies and perform no
    other act on the email.
    Unfortunately, we cannot warrant that the email has not been
    altered or corrupted during transmission.
    =======================================================================
    Michael Malone, Apr 9, 2009
    #2
    1. Advertising

  3. Chuft Captain

    Phlip Guest

    Michael Malone wrote:

    > /Everybody stand back/
    > I know regular expressions


    Our hero!

    > if msg.text =~ %r{lol}i
    > reply(msg, " LOLOLOLOL")


    bla> echololia is a serious aphasia

    bot> LOLOLOLOL

    bla> uh... k
    Phlip, Apr 9, 2009
    #3
  4. Chuft Captain

    John Sikora Guest

    Phlip wrote:
    > Michael Malone wrote:
    >
    >> /Everybody stand back/
    >> I know regular expressions

    >
    > Our hero!
    >
    >> if msg.text =~ %r{lol}i
    >> reply(msg, " LOLOLOLOL")

    >
    > bla> echololia is a serious aphasia
    >
    > bot> LOLOLOLOL
    >
    > bla> uh... k



    It seems that if msg.text was a String object, then the include? method
    should work.
    The first thing I would do would be to try...

    puts msg.text.class

    ...to know exactly what I am dealing with.


    John S.
    --
    Posted via http://www.ruby-forum.com/.
    John Sikora, Apr 9, 2009
    #4
  5. Phlip wrote:
    > Michael Malone wrote:
    >
    >> /Everybody stand back/
    >> I know regular expressions

    >
    > Our hero!
    >
    >> if msg.text =~ %r{lol}i
    >> reply(msg, " LOLOLOLOL")

    >
    > bla> echololia is a serious aphasia
    >
    > bot> LOLOLOLOL
    >
    > bla> uh... k
    >

    %r{\Wlol\W}i

    da di duh da...
    *indiana jones theme music*

    =======================================================================
    This email, including any attachments, is only for the intended
    addressee. It is subject to copyright, is confidential and may be
    the subject of legal or other privilege, none of which is waived or
    lost by reason of this transmission.
    If the receiver is not the intended addressee, please accept our
    apologies, notify us by return, delete all copies and perform no
    other act on the email.
    Unfortunately, we cannot warrant that the email has not been
    altered or corrupted during transmission.
    =======================================================================
    Michael Malone, Apr 9, 2009
    #5
  6. Michael Malone wrote:
    > Chuft Captain wrote:
    >> Is there a simple way to accomplish this task by replacing the ==
    >> comparison operator with something else that searches a string variable
    >> for a substring?
    >>
    >> Thanks
    >>

    > /Everybody stand back/
    > I know regular expressions
    >
    > if msg.text =~ %r{lol}i
    > reply(msg, " LOLOLOLOL")
    >



    You are a god among men. You have my thanks!
    --
    Posted via http://www.ruby-forum.com/.
    Chuft Captain, Apr 9, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Gardner
    Replies:
    0
    Views:
    361
    Chris Gardner
    Oct 29, 2003
  2. Jon Thackray

    Hopefully simple XPath question

    Jon Thackray, Nov 10, 2004, in forum: XML
    Replies:
    9
    Views:
    417
    Joris Gillis
    Nov 13, 2004
  3. Replies:
    4
    Views:
    269
  4. Replies:
    1
    Views:
    250
  5. Harold Pritchett

    A hopefully simple question

    Harold Pritchett, Feb 16, 2005, in forum: Perl Misc
    Replies:
    6
    Views:
    131
    Chris Mattern
    Feb 16, 2005
Loading...

Share This Page