The place where your having the error is
if fine.String == "good"
fine is already a string, thats what gets returns so you don't need to
convert it.
String is a class not a method, so object.String will not work. If you want
to get a string from an object use the to_s method
eg object.to_s
but you could do this a bit differently
puts "What is your name?"
puts "Hello #{gets.chomp} how are you?"
fine = gets.chomp
fine == "good" ? puts( "Good buddy") : puts( "Oh I'm Sorry" )
This would do the same thing that your looking to do with the code above,
but would be more convienient if you wrapped it in a method definition and
then called it.
def check_it_out
puts "What is your name?"
puts "Hello #{gets.chomp} how are you?"
fine = gets.chomp
fine == "good" ? puts( "Good buddy") : puts( "Oh I'm Sorry" )
end
call with
check_it_out
···
On 5/17/06, viswesh <visweshwar_03@rediffmail.com> wrote:
can anybody execute it and tell me where i go wrong.please let me know
asap...
puts " what is your name"
name = gets.chomp
puts " hello " + name + " how are you"
fine = gets
if fine.String=="good"
puts " good buddy"
else
puts "oh iam sorry"
end
----- Original Message ----- From: "viswesh" <visweshwar_03@rediffmail.com>
Newsgroups: comp.lang.ruby
To: "ruby-talk ML" <ruby-talk@ruby-lang.org>
Sent: Wednesday, May 17, 2006 9:46 AM
Subject: Please answer to this code?
can anybody execute it and tell me where i go wrong.please let me know asap...
puts " what is your name"
name = gets.chomp
puts " hello " + name + " how are you"
fine = gets
if fine.String=="good"
puts " good buddy"
else
puts "oh iam sorry"
end
I assume you are getting an error that looks something like:
list.rb:8: private method `String' called for "David\n":String (NoMethodError)
That is telling you that there is no method named String for the string object that you named "fine". On the line where you do the comparison with "good" I assume you want to compare two strings. The variable fine is already a string so you don't have to do any sort of implicit converstion first.
if fine == "good"
That's all you need there. Now if you were comparing a variable that contained something other than a string, you could force a conversion (vaguely similar to a cast in other languages) by calling the to_s method. That would look like this:
if fine.to_s == "good"
however that is totally unnecessary here because both items are already strings.
David Koontz
···
viswesh wrote:
can anybody execute it and tell me where i go wrong.please let me know asap...
puts " what is your name"
name = gets.chomp
puts " hello " + name + " how are you"
fine = gets
if fine.String=="good"
puts " good buddy"
else
puts "oh iam sorry"
end
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.
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)
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
···
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.
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)
> 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.
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/
On 5/18/06, Patrick Hurley <phurley@gmail.com> wrote:
On 5/18/06, viswesh <visweshwar_03@rediffmail.com> wrote:
>
> 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"
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)
···
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.
Have a quick look at this documentation, it will answer a lot of your
questions. It's available for the entire core classes here: RDoc Documentation
and for the standard library included with ruby here: RDoc Documentation
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
>
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/19/06, Chris Klaiber <cklaiber@gmail.com> wrote:
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:
> RDoc Documentation
>
> and for the standard library included with ruby here:
> RDoc Documentation
>
> 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".