On 5/19/06, Chris Klaiber <cklaiber / gmail.com> wrote:
> viswesh, how about something like this? i'm a bit of a ruby newbie
> myself, so the syntax here is probably terrible. the general idea may
> fit what you're looking for? it'll extend to whichever cases you
> require easily
>
> Chris
>
> def respond(input)
>         var cases = [
>                 [["good", "wonderful", "ok", "fantastic"],
>                  "Good old buddy"],
>                 [["bad", "so-so"],
>                  "That's rough"]
>         ];
>
>         for (l in cases)
>                 if input.toLower in l[0]
>                         return l[1]
>
>         return "no match"
> end
>
> puts "How are you? "
> puts respond(gets.chomp)

Converted to a case statement (because I needed a break from work).

def respond(input)
  case input
  when "good", "wonderful", "ok", "fantastic" then "Good old buddy"
  when "bad", "so-so" then "That's rough"
  else "no match"
  end
end

puts "How are you? "
puts respond(gets.chomp)


> On 5/18/06, Tim Becker <a2800276 / gmail.com> wrote:
> > On 5/18/06, Patrick Hurley <phurley / gmail.com> wrote:
> > > On 5/18/06, viswesh <visweshwar_03 / rediffmail.com> wrote:
> > > > Hi Mike,
> > > >
> > > > Thanks for ur time.
> > > > my intent of doing working on this code is :
> > > > as u can see the name prints if u give the name then after that if the
> > > > user types "good" or "ok" or "fine" or "wonderfull" he should be
> > > > prompted with saying Good buddy. else with "oh iam sorry"..
> > > > ***irrespective of case(i.e either uppercase or lowercase).
> > > >
> > > > i heard abt the casecmp() but usage of it 's not clear to me.. it would
> > > > be great if u could use that method in this code.
> >
> > Hi Viswesh,
> >
> > you can find documentation, including usage for `casecmp` here:
> > http://www.ruby-doc.org/core/classes/String.html#M001839
> >
> > Have a quick look at this documentation, it will answer a lot of your
> > questions. It's available for the entire core classes here:
> > http://www.ruby-doc.org/core/
> >
> > and for the standard library included with ruby here:
> > http://www.ruby-doc.org/stdlib/
> >
> > Cheers,
> >    -tim
> >
> >
> > > >
> > > > thanx in advance..
> > > >
> > > >
> > > > Mike Nelson wrote:
> > > > > viswesh wrote:
> > > > >> can u suggest me how to write a method where both cases are accepted to
> > > > >> a given string and that method i want to refer in the below program...
> > > > >
> > > > > I'm not too sure what you are asking for. Something like this?
> > > > >
> > > > > puts " what is your name"
> > > > > name = gets.chomp
> > > > >
> > > > > def checkIfGood(string)
> > > > >   if string == "good"
> > > > >     " good buddy"
> > > > >   else
> > > > >     "oh iam sorry"
> > > > >   end
> > > > > end
> > > > >
> > > > > puts " hello #{name} how are you"
> > > > > puts checkIfGood(gets.chomp)
> > > >
> > > >
> > > > --
> > > > Posted via http://www.ruby-forum.com/.
> > > >
> > > >
> > >
> > > You can use regular expressions for case insensitivity or downcase the
> > > string - regular expressions have the benefit of being far more
> > > flexible...
> > >
> > > # note with the regexp, unless we test for its absence we can forgo the chomp
> > > happy = case gets
> > >   when /not.*good/i : false
> > >   when /good/i : true
> > >   when /ok/i : true
> > >   when /fine/i : true
> > >   when /wonderful/i : true
> > >   else false  # am I the only one that feels that the : should be
> > > allowed here for symmetry
> > > end
> > > puts happy ? "Good buddy" : "oh i am sorry"
> > >
> > > Hope that helps
> > > pth
> > >
> > >
> >
> >
>
>


-- 
Bill Guindon (aka aGorilla)
The best answer to most questions is "it depends".