Seven Languages In Seven Weeks: Week 1 (Ruby) - Day 1

打算收集这一系列的文章,好不容易发现的,感觉作者有些功力,希望能学到东西,哇咔咔!

E文一般的同学也能看懂,近期有时间会翻译一两篇。

Seven Languages In Seven Weeks: Week 1 (Ruby) - Day 1

27 Nov 2011

Out of the seven languages in this book, Ruby is the one I was most familiar with previously. It made for an easy start, and gave me a bit of momentum before I started to tackle languages like Prolog. I’m appreciative of that, because Prolog sure was a struggle for a while.

(This article is part of a series of posts I am doing about my journey through the exercises of the book Seven Languages In Seven Weeks. This article is the first one in the series. For an overview see the project page.)

Day 1 of Week 1 was pretty basic - a bit of string manipulation and looping. Ruby makes things like this very short and pretty.

For example, being able to repeat a string like: "Nick " * 10 or loop like: (1..10).each { |num| do_stuff(num) } is great.

Here is my full code listing for the exercises from Day 1 of Ruby. The home of this piece of code is with the other exercises on github.

# Find:
# 1. A method that substitutes a part of a string
puts "\nFind:"
puts "\n1."
puts "BAM".gsub "M", "TMAN"


# Do:
# 1. Print the string "Hello World"
puts "\nDo:"
puts "\n1."
puts "Hello World"

# 2. For the string "Hello, Ruby," find the index of the word "Ruby."
puts "\n2."
# literally:
p "Hello, Ruby,".index "Ruby."
# realistically:
puts "Hello, Ruby".index "Ruby"

# 3. Print your name ten times
puts "\n3."
puts "Nick " * 10

# 4. Print the string "This is sentence number 1," where the number 1 changes
# from 1 to 10
puts "\n4."
(1..10).each { |num| puts "This is sentence number #{num}" }

# Bonus: Write a program that picks a random number. Let a player guess the
# number, telling the player whether the guess is too low or too high.
puts "\nBonus:"

random_number = rand(1000) + 1
guess = 0

while guess != random_number do
    print "Pick a number between 1 and 1000: "
    guess = gets.to_i
    puts "Too low!" if guess < random_number
    puts "Too high!" if guess > random_number
end

puts "Got it! It was #{random_number}"
And the output:
$ ruby day1.rb

Find:

1.
BATMAN

Do:

1.
Hello World

2.
nil
7

3.
Nick Nick Nick Nick Nick Nick Nick Nick Nick Nick 

4.
This is sentence number 1
This is sentence number 2
This is sentence number 3
This is sentence number 4
This is sentence number 5
This is sentence number 6
This is sentence number 7
This is sentence number 8
This is sentence number 9
This is sentence number 10

Bonus:
Pick a number between 1 and 1000: 500
Too high!
Pick a number between 1 and 1000: 250
Too high!
Pick a number between 1 and 1000: 125
Too low!
Pick a number between 1 and 1000: 192
Too high!
Pick a number between 1 and 1000: 158
Too high!
Pick a number between 1 and 1000: 141
Too high!
Pick a number between 1 and 1000: 133
Too high!
Pick a number between 1 and 1000: 129
Got it! It was 129



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值