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.
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.
Technorati Tags: Some Methods in Ruby
Blogs linking to this article
# gets and chompchomp 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'.
puts "In which city do you stay?"
STDOUT.flush
city = gets.chomp
puts "The city is " + city
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: Some Methods in Ruby
Blogs linking to this article
17 Comments:
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?
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
Jatinder, there's some problem with your logic
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?
-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
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.
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
Please do not post here as Anonymous
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..
Jatinder it works. Smita your problem has a problem
Satish,
I have updated my LeapYear program to include the additional constructs of && and ||.
http://ashishkulkarni.blogspot.com
Regards,
Ashish.
#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
Okay, I hope you are able to execute your programs in SciTE. bitsmithusa, you need to write a program that runs in SciTE.
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!!
Sandip, there's nothing wrong with your program. All of us are facing the same problem.
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.
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
Post a Comment
<< Home