ruby表达式

ri String#chomp

a=1;b=2;c=6
a
=b= c
puts 
"a=#{a} b=#{b} c=#{c}"


a
=1 b=2 c=6

 #{ }在双引号下起作用

 平行赋值a=b=c

1. 条件表达式

 

#Conditional Execution
print   ' Enter something: '
str   =  gets
if   str .chomp  ==   ' p'
  puts  ' You entered a p!'
else
  puts 
" You entered #{str.chomp} bye "
end

print   ' Enter somthing else: '
str   =  gets

case   str
  when 
' p'
    puts  ' You entered a p!'
  when  ' 1'
    puts  ' You entered a 1. That is a number'
   else
    puts 
" You entered a #{str}. "
end

 

调用系统命令:

puts `date `
puts `
dir`

该符号与~同一个键

2.循环:

 

i = 1
while  i > 0
  
print   " #{i} "
  i
+= 1
  
if  i > 9
    break
  
end
  
end

puts

for  var  in   1 .. 9
  
print   " #{var} "
end

puts 

9 .times  do  |var|
  var
= var + 1
  
print   " #{var} "
end

puts

1 .upto( 9 do  |var|
  
print   " #{var} "
end

puts

1 .step( 9 , 3 do  |var|
  
print   " #{var} "
end

 int.times {|i| block }     => int
-----------------------------------------------------------------------
Iterates block int times, passing in values from zero to int - 1.

   5.times do |i|
     print i, " "
   end

produces:

   0 1 2 3 4

 int.upto(limit) {|i| block }     => int
-----------------------------------------------------------------------
Iterates block, passing in integer values from int up to and including limit.

   5.upto(10) { |i| print i, " " }

produces:

   5 6 7 8 9 10

使用fxri来查询#step

num.step(limit, step ) {|i| block }     => num
-----------------------------------------------------------------------
Invokes block with the sequence of numbers starting at num, incremented by step on each call. The loop finishes when the value to be passed to the block is greater than limit (if step is positive) or less than limit (if step is negative). If all the arguments are integers, the loop operates using an integer counter. If any of the arguments are floating point numbers, all are converted to floats, and the loop is executed floor(n + n*epsilon)+ 1 times, where n = (limit - num)/step. Otherwise, the loop starts at num, uses either the < or > operator to compare the counter against limit, and increments itself using the + operator.

   1.step(10, 2) { |i| print i, " " }
   Math::E.step(Math::PI, 0.2) { |f| print f, " " }

produces:

   1 3 5 7 9
   2.71828182845905 2.91828182845905 3.11828182845905

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值