Q1. Write a Ruby program that asks for a numeric value of the temperature in degrees Fahrenheit. Finally, the program displays the equivalent value in degrees Centigrade. To format the output to say 2 decimal places, we can use the Kernel's format method. For example, if x = 45.5678 then format("%.2f", x) will return a string 45.57. Another way is to use the round function as follows: puts (x*100).round/100.0
print 'Please input the temperature in degrees Fahrenheit :'
STDOUT.flush
temp = gets.chomp.to_f
if temp=~/^%d+\.%d*$/
puts temp
end
Q2. Write a Ruby program that asks for a year and then displays to the user whether the year entered by him/her is a leap year or not.
print 'Please input a year : '
STDOUT.flush
year = gets.chomp.to_i
if (year%4==0&&year%100>0)||year%400==0
puts "#{year} is a leap-year!!"
else
puts "#{year} is not a leap-year!!"
end
Q3. Write a method leap_year. Accept a year value from the user, check whether it's a leap year and then display the number of minutes in that year.
def if_leap_year(year)
(year%4==0&&year%100>0)||year%400==0
end
print 'Please input a year : '
STDOUT.flush
year = gets.chomp.to_i
if if_leap_year(year)
puts "#{year} is a leap-year!! it has #{366*24*60} minutes"
else
puts "#{year} is a leap-year!! it has #{365*24*60} minutes"
end
Q4. Write a Ruby program that, when given an array as collection = [1, 2, 3, 4, 5] it calculates the sum of its elements.
#solution I
coll = [1,2,3,4,5]
sum = 0
coll.inject{|sum,each| sum+=each}
puts sum
#solution II
coll = [1,2,3,4,5]
sum = 0
coll.each {|i| sum+=i}
puts sum
Q5. Write a Ruby program that, when given an array as collection = [12, 23, 456, 123, 4579] it displays for each number, whether it is odd or even.
coll = [12, 23, 456, 123, 4579]
coll.each do |i|
if i%2==0
puts "#{i} is odd"
else
puts "#{i} is even"
end
end
Q6.Given a string s = 'key=value', create two strings s1 and s2 such that s1 contains key and s2 contains value. Hint: Use some of the String functions.
#solution I
s = 'key=value'
s1 = s.split(/=/)[0]
s2 = s.split(/=/)[1]
puts s1,s2
#solution II
s = 'key=value'
posion = s.index('=')
s1 = s[0...posion]
s2 = s[posion+1..(s.size)]
puts s1,s2
Q7. Write a Deaf Grandma program. Whatever you say to grandma (whatever you type in), she should respond with HUH?! SPEAK UP, SONNY!, unless you shout it (type in all capitals). If you shout, she can hear you (or at least she thinks so) and yells back, NO, NOT SINCE 1938! To make your program really believable, have grandma shout a different year each time; maybe any year at random between 1930 and 1950. You can't stop talking to grandma until you shout BYE.
year = (1930..1950).to_a
puts 'you say :'
STDOUT.flush
until (say=gets.chomp).eql?('Bye')
if say.eql?(say.upcase)
puts "I got what u say in #{year[rand(year.size)]}"
else
puts "huh ?? speak louder!!!"
end
puts 'you say :'
STDOUT.flush
end
Q8. Extend your Deaf Grandma program: What if grandma doesn't want you to leave? When you shout BYE, she could pretend not to hear you. Change your previous program so that you have to shout BYE three times in a row. Make sure to test your program: if you shout BYE three times, but not in a row, you should still be talking to grandma.
year = (1930..1950).to_a
puts 'you say :'
STDOUT.flush
until (say=gets.chomp.to_s)=~/^ByeByeBye$/
if say.eql?(say.upcase)
puts "I got what u say in #{year[rand(year.size)]}"
else
puts "huh ?? speak louder!!!"
end
puts 'you say :'
STDOUT.flush
end
分享到:
- 2007-11-14 00:54
- 浏览 1083
- 评论(0)
- 论坛回复 / 浏览 (0 / 2454)
- 查看更多
相关推荐
这本书“Ruby入门到精通”显然旨在引导初学者逐步掌握Ruby语言。以下是一些关键知识点,它们可能是书中涉及的内容: 1. **基础语法**:包括变量(本地、实例、类和全局变量)、常量、符号、字符串、数字、数组和...
在二十分钟的Ruby入门教程中,我们将快速浏览Ruby的基础知识,包括语法、数据类型、控制结构以及简单的类定义。 首先,让我们从Ruby的基本语法开始。Ruby的注释以`#`字符开始,一直持续到行尾。例如: ```ruby # 这...
"Ruby从入门到精通"的教程旨在帮助新手快速掌握这门语言的核心概念和实际应用。Ruby由日本人松本行弘在1993年创建,它的设计目标是让编程更加愉悦,同时也更注重代码的可读性和表达性。 1. **基础概念**: - 变量...
本教程是为那些想要快速入门Ruby编程的初学者准备的,它以易读性和实用性为特点,旨在帮助你迅速掌握Ruby的基础知识。 1. **Ruby的基本概念** - **变量**:Ruby中的变量分为局部变量、实例变量、类变量和全局变量...
这个"Ruby语言入门教程(中文版)[PDF]"涵盖了Ruby的基础到进阶内容,适合初学者和希望巩固Ruby知识的开发者。通过阅读并实践教程中的例子,读者可以逐步掌握Ruby编程,并开启在Ruby世界中的探索之旅。
2. 控制结构:Ruby支持常见的控制结构,如if/else、unless、case、while、for以及循环控制语句如break、next等。 3. 函数定义:Ruby使用`def`关键字定义函数,函数名后跟参数列表,最后以`end`结束。Ruby支持块...
Ruby的控制结构包括条件语句(如if、unless、case)、循环(如while、for、each)和流程控制(如break、next、redo、retry)。Ruby还支持块(Block),这是通过花括号或do...end包裹的一段代码,常与迭代器配合使用...
Ruby的控制结构包括条件语句(如if、unless、case)和循环(如for、while、until、each)。Ruby的块(block)和Proc对象是其独特的特性,它们可以捕获代码并作为参数传递,这在函数式编程中非常有用。 Ruby的类和...
2. 控制结构:包括条件语句(如`if`、`unless`、`case`)和循环(如`for`、`while`、`until`、`each`)。Ruby还提供了简洁的块(block)语法,可以与循环和条件语句结合,使代码更易读。 3. 函数和方法:Ruby中的...
这个“Ruby入门培训ppt/pdf及代码”资源提供了一个全面的学习路径,帮助初学者快速掌握Ruby的基础知识和实际应用。 Ruby的核心特性包括: 1. **面向对象**:Ruby是最纯粹的面向对象语言之一,几乎一切皆对象,包括...
一、理解 Ruby 及其特点 Ruby 是一种简单快捷的面向对象脚本语言,由日本人松本行弘(Yukihiro ...条件语句和循环:Ruby 使用 if...else、case、while、for 等条件语句和循环语句来控制程序的流程。你需要理解这些语
Ruby在很多方面与Perl、Python和Smalltalk有相似之处,但其语法更加简洁明了,特别适合初学者入门。 Ruby的核心特性包括: 1. 面向对象:Ruby的万物皆对象,就连基本的数据类型如整数、字符串和布尔值都是对象,...
总之,Ruby语言入门教程将带你走进这个充满魅力的编程世界,从基础语法到高级特性,一步步引导你成为Ruby程序员。通过不断练习和探索,你会发现Ruby不仅易于学习,而且功能强大,能帮你实现各种创新的编程理念。
本入门教程旨在帮助初学者快速理解Ruby的基础概念,掌握其核心特性,并能够开始编写简单的程序。 一、Ruby的基本语法 Ruby的语法非常直观,它强调代码的可读性。变量在Ruby中有四种类型:局部变量(以小写字母或...