Learn to Program(1)

本文介绍了编程的基础概念,包括数值运算、字符串操作、变量赋值等基本语法,并深入探讨了字符串方法、数学运算及随机数生成等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Learn to Program(1)

0. Getting Started
1. Numbers
1.1 Introduction to puts
puts simply writes onto the screen whatever comes after it.

1.2 Integer and Float
numbers without decimal points are called integers, and nunbers with decimal points are usually called floating-point numbers(floats).

1.3 Simple Arithmetic
addition + subtraction - multiplication * division /

2. Letters
2.1 String Arithmetic
puts 'I like' + 'applie pic.' #add string

puts 'blink ' * 4 #multiply string
7*3 really just means 7 + 7 + 7, so 'blink ' * 3 just means 'blink ' + 'blink ' + 'blink '

2.2 12 vs '12'
12 is a number, '12' is a string of two digits.

2.3 Problems
puts '12' + 12
puts '2' * '5'

error message: : can't convert Fixnum into String (TypeError)

And 'pig' * 5 is ok, but 5 * 'pig' is silly.

puts 'You're well!' =====> puts 'You\'re well!'

3. Variables and Assignment
Using vairables
str = '...you can say hello...'
puts str
puts str

We can assign an object to a variable, we can reassgin a different object to that variable.
var = 'just go go go'
puts var
var = 5 * (1 + 2)
puts var

In fact, variables can point to just about anything... except other variables.
var1 = 8
var2 = var1
puts var1
puts var2

var1 = 'eight'
puts var1
puts var2

And the result is:
8 8 eight 8

4. Mixing It Up
4.1 Conversions
Using .to_s to get the string version of an object.
puts 2.to_s + '5'

Using to_i gieves the integer version of an object, to_f gives the float version.
puts 2 + '5'.to_i

puts 99.99.to_i
puts '5 is a number'.to_f
puts 'number 5 is here'.to_i

and the result is 99, 5, 0

4.2 Another Look at Puts
Before puts tries to write out an object, it uses to_s to get the string version of that object. In fact, the s in puts stands for string.

4.3 The Methods gets and chomp
gets just sits there, reading what you type until you press Enter.
puts gets

puts gets.chomp

chomp takes off any Enters hanging out at the end of your string.

5. More About Methods
Every verb needs a noun, so every method needs an object.

If I say puts ' something', what I am really saying is self.puts ' something'.

5.1 Fancy String Methods
string method reverse which gives a backwards version of a string.
var1 = 'stop'
var2 = 'love'

puts var1.reverse
puts var2.reverse

Its results are pots, evol, it is really stange to see the string reverse.

var = 'carl'
puts 'the length of string carl is ' + var.length.to_s

upcase changes every lowercase letter to uppercase.
downcase changes every uppercase letter to lowercase.
swapcase switches the case of every leter in the string
capitalize is just like downcase, except that it switches the first character to uppercase.

var = 'caRl'
puts var.upcase
puts var.downcase
puts var.swapcase
puts var.capitalize

center adds spaces to the beginning and end of the string to make it cenered.
lineWidth = 50
puts('carl is a good guy'.center(lineWidth))

ljust and rjust, which stand for left justify and right justify. They are similar to center.

5.2 Higher Math
% puts 7%3 shows 1

abs, it just takes the absolute value of the number.
puts((-3).abs)

5.3 Random Numbers
The method to get a randomly chosen number is rand.
puts rand
puts rand
puts rand(100)
puts rand(100)

Note that I used rand(101) to get back numbers from 0 to 100, and that rand(1) always gives back 0.
Not understanding the range of possible return values is the biggest mistake.

srand ---> seed and rand

5.4 The Math Object
puts(Math::PI)
puts(Math.sqrt(4))

results: 3.14...., 2.0

references:
http://pine.fm/LearnToProgram/?Chapter=00
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值