Ruby on Rails 学习(四)

 1.读出文件
class HelloController < ApplicationController
  def hello
	allText = ""
	File.open("/xxx/black_group.txt","r") do |file| 
		while line  = file.gets 
			allText = allText + line
		end 
	end
	render :text => allText
	return
  end
end


2.数组和数组循环
class HelloController < ApplicationController
  def hello
	#数组
	allLineArray=Array.new
	File.open("/xxx/black_group.txt","r") do |file|
		while line  = file.gets
			allLineArray.push( line )  #向数组尾部添加元素列表 
		end 
	end
	allText = ""
	#数组循环
	for i in allLineArray do  
	   allText = allText + "#{i}"
	end   
	render :text => allText
	return
  end
end


3.接受的参数
class HelloController < ApplicationController
  def hello
	group_name = params[ :group_name ] 
	render :text => group_name
	return
  end
end


访问一下确认:
http://192.168.60.128:3000/hello/hello?group_name=test


4.通过黑名单阻止进入的参数


class HelloController < ApplicationController
  def hello
	group_name = params[ :group_name ] 
	group_name = group_name.gsub(/\s+/,'') #去空格
	#数组
	allLineArray=Array.new
	File.open("/xxx/black_group.txt","r") do |file|
		while line  = file.gets
			line = line.gsub(/\s+|\r|\n/,'') #去空格回车换行
			allLineArray.push( line )  #向数组尾部添加元素列表 
		end 
	end
	allText = ""
	#数组循环
	inBlackFlag = 0
	allText
	for i in allLineArray do  
	   #判断进入的group_name是否在黑名单列表中
	   tmpValue = "#{i}"
	   allText = allText + tmpValue+"="+group_name +"<br>"
	    if( tmpValue == group_name )
		inBlackFlag = 1
		break
	    end   
	end   
	retTxt = group_name + "=" + "#{inBlackFlag}" 
	render :text => retTxt
	return
  end
end




5.封装成私有方法

class HelloController < ApplicationController
	def hello
		group_name = params[ :group_name ] 
		inBlackFlag =   getBlackFlag(group_name) 
		if (inBlackFlag == 1)
			return
		end
		 
	end


	private
	def getBlackFlag(group_name) 
		group_name = group_name.gsub(/\s+/,'') #去空格
		#数组
		allLineArray=Array.new
		File.open("/xxx/black_group.txt","r") do |file|
			while line  = file.gets
				line = line.gsub(/\s+|\r|\n/,'') #去空格回车换行
				allLineArray.push( line )  #向数组尾部添加元素列表 
			end 
		end
		allText = ""
		#数组循环
		inBlackFlag = 0
		allText
		for i in allLineArray do  
		   #判断进入的group_name是否在黑名单列表中
		   tmpValue = "#{i}"
		   allText = allText + tmpValue+"="+group_name +"<br>"
		    if( tmpValue == group_name )
			inBlackFlag = 1
			break
		    end   
		end   
	 
		return inBlackFlag
	end 


end




6.造成黑名单限制功能
class HelloController < ApplicationController
	def hello
		group_name = params[ :group_name ] 
		inBlackFlag =   getBlackFlag(group_name) 
		if (inBlackFlag == 1)
			retTxt = group_name + " is in black list." 
			render :text => retTxt
			return
		end
		 
	end


	private
	def getBlackFlag(group_name) 
		group_name = group_name.gsub(/\s+/,'') #去空格
		#数组
		allLineArray=Array.new
		File.open("/xxx/black_group.txt","r") do |file|
			while line  = file.gets
				line = line.gsub(/\s+|\r|\n/,'') #去空格回车换行
				allLineArray.push( line )  #向数组尾部添加元素列表 
			end 
		end
		allText = ""
		#数组循环
		inBlackFlag = 0
		allText
		for i in allLineArray do  
		   #判断进入的group_name是否在黑名单列表中
		   tmpValue = "#{i}"
		   allText = allText + tmpValue+"="+group_name +"<br>"
		    if( tmpValue == group_name )
			inBlackFlag = 1
			break
		    end   
		end   
	 
		return inBlackFlag
	end 


end

改进

class HelloController < ApplicationController
	def hello
		group_name = params[ :group_name ] 
		inBlackFlag =   getBlackFlag(group_name) 
		if (inBlackFlag == 1)
			retTxt = group_name + " is in black list!" 
			render :text => retTxt
			return
		end
		 
	end

	private
	def getBlackFlag(group_name) 
		group_name = group_name.gsub(/\s+/,'') #去空格		 
		allLineArray=Array.new
		File.open("/xxx/black_group.txt","r") do |file|
			while line  = file.gets
				line = line.gsub(/\s+|\r|\n/,'') #去空格回车换行
				allLineArray.push( line )  #向数组尾部添加元素列表 
			end 
		end		 
		inBlackFlag = 0		 
		if( allLineArray.include?(group_name) )   
			inBlackFlag = 1
		end 	 
		return inBlackFlag
	end
end
 
 参考 
 http://www.phptoruby.com/in_array


 



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值