收藏本站 收藏本站
积木网首页 - 软件测试 - 常用手册 - 站长工具 - 技术社区
首页 > ruby > 正文

首页 - PHP - 数据库 - 操作系统 - 游戏开发 - JS - Android - MySql - Redis - MongoDB - Win8 - Shell编程 - DOS命令 - jQuery - CSS样式 - Python - Perl

Access - Oracle - DB2 - SQLServer - MsSql2008 - MsSql2005 - Sqlite - PostgreSQL - node.js - extjs - JavaScript vbs - Powershell - Ruby

Ruby语法笔记

接受用户输入

first_name = gets.chomp

首字母大写

first_name.capitalize!

字母变大写

first_name.upcase!

字母变小写

first_name.downcase!

多行输出

print <<EOF
  # 多行输出
EOF

注释

# 我是注释

变量获取

#{first_name}

变量

全局变量 $ 类变量 @@ 方法变量 @ 局部变量 小写字母或_

if/else

if a < b
  puts '1'
elsif b < a
  puts '2'
end    


class Classname
  def functionname(params)
    puts params
  end
end

class1 = Classname.new
class1.functionname('1')
unless
unless false
  print 'ok'
else
  print 'no'
end    

是否包含字符

print 'puts'

user_input = gets.chomp

user_input.downcase!

if user_input.include?"s"
  print 'has s'
end

替换字符

# s -> th
user_input.gsub!(/s/,"th")

在字符串中输出变量值

puts 'okok #{user_input}'

while

counter = 1

while counter < 11
  puts counter
  counter = counter + 1
end

Until

counter = 1
until counter > 10
  print counter
  counter = counter + 1
end 
 

+= 、 -= 、 *=、 /=
Some languages have the increment operators ++ and -- (which also add or subtract 1 from a value), but Ruby does not
for循环
# 如果 1...10 包含1-9,如果 1..10 包含1-10

for num in 1...10
  puts num
end  

Loop Method
An iterator is just a Ruby method that repeatedly invokes a block of code.

i = 20
loop do
  i -= 1
  print "#{ i }"
  break if i <= 0
end  

Next

i = 20
loop do
 i -= 1
 next if i%2 != 0
 print "#{i}"
 break if i <= 0
end

数组

my_array = [1,2,3,4,5]

The .each Iterator迭代器

numbers = [1, 2, 3, 4, 5]

# one way to loop
numbers.each { |item| puts item }

# another way to loop
numbers.each do |item|
 puts item
end

The .times Iterator 次数迭代器

10.times { print 'ok'})

Looping with 'While'

num = 1

while num <= 50 do
  print num
  num += 1
end  

Looping with 'Until'

num = 1
until num > 50 do
  print num
  num += 1
end  

Loop the Loop with Loop

num = 0

loop do
  num += 1
  print "Ruby!"
  break if num == 30
end  

The .split Method,

text.split(",")

puts "Text to search through: "
text = gets.chomp
puts "Word to redact"
redact = gets.chomp

words = text.split(" ")

words.each do |word|
  print word
end

Ruby的安装与运行
Ruby的安装注意点OSX或linux较好不同版本的实现:c++、C、java本次CRuby安装方法linux或OSX都有预装包管理器(apt、yum、brew),并非最新版本控制系统:rvm(rvm.i

CentOS7下搭建ruby on rails开发环境
1、安装rvmcurl-Lhttps://get.rvm.io|bash-sstablervm是一个命令行工具,提供多版本ruby环境的管理和切换,还可以根据项目管理不同的gemset。如果没有生成sshkey,

Windows下安装配置Ruby的debug工具ruby-debug-base19
gem使用代理的两种方式:方式一:SEThttp_proxy=http://ip_or_host:PORTgeminstallthe_package_name方式二:gemlist-phttp://user:passwd@ip_or_host:8080-r安装ruby-debug-base19有2种方法

本周排行

更新排行

强悍的草根IT技术社区,这里应该有您想要的!
Copyright © 2010 Gimoo.Net. All Rights Rreserved  京ICP备05050695号