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





25 Comments:
#Program to find LeapYear
#Author:Vikrant Chaudhari
#LeapYear.rb
class Leap
def is_leap_year(year)
@year=year
if @year%4==0
puts "It is a Leap Year"
else
puts "It is not a Leap Year"
end
end
end
obj=Leap.new #Instantiation
obj.is_leap_year(2006)#Call method
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
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
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
def leaps(yeer)
if yeer.to_i>0 && yeer.to_i%4==0
if yeer.to_i%400==0
'yeah it\'s a leap year'
elsif yeer.to_i%100==0
'du-oh! not a leap year'
else
'happy leap year'
end
else
'not a leap year'
end
end
puts leaps(gets.chomp)
I think the condition should be like this:
if ( (year % 4 == 0 && year % 100 != 0) || year % 400 ==0 ))
#ASSIGNMENT : given year is leap-year or not
# Fact: A year is a leap year if it is divisible by 4, unless it is divisible by 100. However, years divisible by 400 are leap years.
puts "Please enter the four digit Year "
year_string = gets.chomp
puts "You have entered , YEAR : " + year_string
# convert the inputted string into integer
year_integer=year_string.to_i
if year_integer<1000
puts "Invalid input!! Please reenter a 4 digit number"
exit
end
if( ( ( year_integer%4==0) and ( year_integer%100 != 0 ) ) or (year_integer%400 == 0 ) )
puts "Its a leap year."
else
puts "Its not a leap year"
end
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
puts "Please enter a year"
y = gets.chomp
if y.to_i % 100 == 0
if y.to_i % 400 == 0
puts y + " is a Leap Year"
end
elsif y.to_i % 4 == 0
puts y + " is a Leap Year"
end
Okay, I hope you are able to execute your programs in SciTE. bitsmithusa, you need to write a program that runs in SciTE.
Here is the code that I could run after understanding how gets work :
puts "Please enter a year"
y = gets.chomp
if y.to_i % 100 == 0
if y.to_i % 400 == 0
puts y + " is a Leap Year"
else
puts y + " is not a Leap Year"
end
elsif y.to_i % 4 == 0
puts y + " is a Leap Year"
else
puts y + " is not a Leap Year"
end
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!!
Hi Satish,
I run a program Methods.rb which you given in the bolg. I think the program should asks for the city first and when user puts city name the result will shown on the sceen but the progrm asks for the city without showing the text "In which city do you stay?" and when i put city name and press enter the program executes showing the above text and the result text. Could u please clearify if i missing some thing.
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
Links to this post:
Create a Link
<< Home