Forum: Ruby Please answer to this code?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3d4c2229e352a9d0b41ecead595560ca?d=identicon&s=25 viswesh (Guest)
on 2006-05-17 15:46
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
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2006-05-17 16:03
(Received via mailing list)
viswesh,

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
Ad4caa46eebe9347305177fc33468563?d=identicon&s=25 Mike Nelson (miken700)
on 2006-05-17 16:03
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

I think you forgot the .chomp on the second gets. This is probably your
main issue. Some style suggestions,


puts " what is your name"
name = gets.chomp

puts " hello #{name} how are you"
fine = gets.chomp

if fine == "good"
  puts " good buddy"
else
  puts "oh iam sorry"
end
31867bf7d269297b4f757f6dd1014ba2?d=identicon&s=25 Xiao-Feng Li (Guest)
on 2006-05-17 16:06
(Received via mailing list)
You'd get gets.chomp for fine and use fine=="good" for direct
comparison.

-xiaofeng
3d4c2229e352a9d0b41ecead595560ca?d=identicon&s=25 viswesh (Guest)
on 2006-05-17 16:10
Hi Mike and Daniel,

That's wonderfull quick time replies and iam thankfull for both.
Yeah Mr.Mike ur right.
thanks again..
bye


Mike Nelson wrote:
> 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
>
> I think you forgot the .chomp on the second gets. This is probably your
> main issue. Some style suggestions,
>
>
> puts " what is your name"
> name = gets.chomp
>
> puts " hello #{name} how are you"
> fine = gets.chomp
>
> if fine == "good"
>   puts " good buddy"
> else
>   puts "oh iam sorry"
> end
Ced5fff44ff8929fc974012ea108b284?d=identicon&s=25 Sergey Volkov (rf-vsv)
on 2006-05-17 16:16
(Received via mailing list)
< Your code
> fixed code
-------------------
< fine = gets
> fine = gets.chomp

< if fine.String=="good"
> if fine == "good"

----- 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?
3d4c2229e352a9d0b41ecead595560ca?d=identicon&s=25 viswesh (Guest)
on 2006-05-17 17:15
Hi mike,

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...

thanx

viswesh wrote:
> Hi Mike and Daniel,
>
> That's wonderfull quick time replies and iam thankfull for both.
> Yeah Mr.Mike ur right.
> thanks again..
> bye
>
>
> Mike Nelson wrote:
>> 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
>>
>> I think you forgot the .chomp on the second gets. This is probably your
>> main issue. Some style suggestions,
>>
>>
>> puts " what is your name"
>> name = gets.chomp
>>
>> puts " hello #{name} how are you"
>> fine = gets.chomp
>>
>> if fine == "good"
>>   puts " good buddy"
>> else
>>   puts "oh iam sorry"
>> end
5da4c52f43677f395aff5bde775593c2?d=identicon&s=25 Daniel Schierbeck (dasch)
on 2006-05-17 18:35
(Received via mailing list)
viswesh wrote:
>
> if fine.String=="good"
> 	puts " good buddy"
> 	else
> 		puts "oh iam sorry"
> 	end

   puts "what is your name?"
   name = gets.chomp

   puts "hello #{name}, how are you?"

   puts gets.chomp == "fine" ? "good, buddy!" : "oh, i'm sorry!"


Cheers,
Daniel
Ad4caa46eebe9347305177fc33468563?d=identicon&s=25 Mike Nelson (miken700)
on 2006-05-17 20:30
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)
E2bbe31fb94e92f5f404c5cb550c371f?d=identicon&s=25 unknown (Guest)
on 2006-05-17 22:12
(Received via mailing list)
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
31867bf7d269297b4f757f6dd1014ba2?d=identicon&s=25 Xiao-Feng Li (Guest)
on 2006-05-18 11:09
(Received via mailing list)
You'd get gets.chomp for fine and use fine=="good" for direct
comparison.

-xiaofeng
3d4c2229e352a9d0b41ecead595560ca?d=identicon&s=25 viswesh (Guest)
on 2006-05-18 14:46
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)
2c51fec8183a5d21c4e11b430beabb47?d=identicon&s=25 Patrick Hurley (Guest)
on 2006-05-18 15:28
(Received via mailing list)
On 5/18/06, viswesh <visweshwar_03@rediffmail.com> wrote:
> be great if u could use that method in this code.
> >
> >
> > 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
62002cee15efcf4628cd7efc19425a07?d=identicon&s=25 Tim Becker (Guest)
on 2006-05-18 16:05
(Received via mailing list)
On 5/18/06, Patrick Hurley <phurley@gmail.com> wrote:
> > 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
B37f0f80e0afbb4f5c62d36f437d8c8c?d=identicon&s=25 Chris Klaiber (Guest)
on 2006-05-19 20:59
(Received via mailing list)
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)
Ff63c03fd68754adbadd2c6314646bef?d=identicon&s=25 Bill Guindon (agorilla)
on 2006-05-19 22:44
(Received via mailing list)
On 5/19/06, Chris Klaiber <cklaiber@gmail.com> wrote:
>                  "Good old buddy"],
>
> 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)
This topic is locked and can not be replied to.