Wednesday, June 28, 2006

Some Methods in Ruby

Let us explore some methods in Ruby. So far we had seen a method like puts that writes to the screen. How does one accept user input? For this gets and chomp are useful. The example (Methods.rb) below illustrates the same.
# gets and chomp
puts "In which city do you stay?"
STDOUT.flush
city = gets.chomp
puts "The city is " + city
chomp is a string method and gets retrieves only strings from your keyboard. You must have realised that gets returns a string and a '\n' character, while chomp removes this '\n'.

There are many methods in the String class (you don't have to memorize them all; you can look up the documentation) like the reverse that gives a backwards version of a string (reverse does not change the original string). length that tells us the number of characters (including spaces) in the string. upcase changes every lowercase letter to uppercase, and downcase changes every uppercase letter to lowercase. swapcase switches the case of every letter in the string, and finally, capitalize is just like downcase, except that it switches the first character to uppercase (if it is a letter).

More on methods: If objects (such as strings, integers and floats) are the nouns in Ruby language, then methods are the verbs. Every method needs an object. It's usually easy to tell which object is performing the method: it's what comes right before the dot. Sometimes, though, it's not quite as obvious. When we are using puts, gets - where are their objects? In Ruby, the implicit object is whatever object you happen to be in. But we don't even know how to be in an object yet; we've always been inside a special object Ruby has created for us that represents the whole program. You can see always see what object you are in by using the special variable self.


First Post | Previous | Next


Technorati Tags:
Blogs linking to this article

17 Comments:

Blogger Unknown said...

Vikrant -
(a) We have not done class and method declaration, so I don't expect you to use it yet
(b) What about the other conditions, as mentioned in my email?

11:51 AM  
Blogger Jatinder Singh said...

year = gets.chomp

if((year%4 ==0 && year%100 !=0) || year%400 ==0)
puts year.to_s+" is a leap year"

else
puts year.to_s+" is not a leap year"

end

1:59 PM  
Blogger Unknown said...

Jatinder, there's some problem with your logic

2:29 PM  
Blogger Aspirations said...

An observation:

print 'Enter the number: '
varbase = gets()
puts varbase

When this code is executed in SCiTE editor, a command prompt appears. You can not really enter any value on the command prompt. Instead, you have to enter a value in the SCiTE console.

Also, on the SCiTE console, the puts statement appears after value is entered and an enter is pressed. reason?

4:08 PM  
Blogger Jatinder Singh said...

-begin
Modified program
1) converted year to integer using year.to_i
-end
year = gets.chomp
year = year.to_i

if((year % 4 ==0 && year % 100 !=0) || year % 400 ==0)
puts year.to_s+" is a leap year"

else
puts year.to_s+" is not a leap year"

end

4:27 PM  
Blogger Unknown said...

Anish, your first observation is correct. You have to enter your value on the SciTE output window.
As to why the puts statement appears after value is entered; I am not sure. Trying to find an answer.

4:40 PM  
Anonymous Anonymous said...

puts "please enter an year"

year = gets.chomp

if ( year % 4 == 0 && year % 100 != 0 || year % 400 ==0 )

puts "Year is leap year"

else

puts "Year is not leap year"

end

4:50 PM  
Blogger Unknown said...

Please do not post here as Anonymous

4:57 PM  
Blogger Jatinder Singh said...

Hi Satish,

I tried running my program with 1964 as an argument, following is the output..

>ruby LeapYear.rb
1964
1964 is a leap year
>Exit code: 0

Am i missing something here..

5:34 PM  
Blogger Unknown said...

Jatinder it works. Smita your problem has a problem

6:05 PM  
Blogger Ashish Kulkarni said...

Satish,

I have updated my LeapYear program to include the additional constructs of && and ||.

http://ashishkulkarni.blogspot.com

Regards,

Ashish.

8:51 PM  
Blogger Aspirations said...

#A program to check Lear year
print 'Kindly enter the year, which you would want to be checked: '
year = gets.to_i
puts ''
if year%4==0
if year%100==0
if year%400==0
puts("The year #{year} is a leap year 400")
else
puts("The year #{year} is not a leap year 400")
end
else
puts("The year #{year} is a leap year 100")
end
else
puts("The year #{year} is not a leap year 4")
end

10:18 PM  
Blogger Unknown said...

Okay, I hope you are able to execute your programs in SciTE. bitsmithusa, you need to write a program that runs in SciTE.

4:57 AM  
Blogger Anil said...

puts "Enter an year"
y = gets.chomp
y = y.to_i
if ((y%4==0&&y%100!=0)||y%400==0)
puts y.to_s+" is a leap year"
else
puts y.to_s+" is not a leap year"
end

Done using SciTE!!

10:14 AM  
Blogger Unknown said...

Sandip, there's nothing wrong with your program. All of us are facing the same problem.

12:02 PM  
Blogger Ashish Kulkarni said...

Hi Sandip and Satish,

You need to flush the standard output buffer after your have used a puts using:

STDOUT.flush

This will display the output and then the next statement will run which in this case is gets.

3:05 PM  
Blogger Unknown said...

Thanks Ashish. So here's the code:

# gets and chomp
puts "In which city do you stay?"
STDOUT.flush
city = gets.chomp
puts "The city is " + city

3:09 PM  

Post a Comment

<< Home