- 論壇徽章:
- 0
|
本帖最后由 alinunix 于 2013-10-11 18:18 編輯
1 #!/usr/bin/env ruby
2 tool_features = ['f1', 'f2']
3
4 names = ["a", "tool", "b"]
5
6 names.each do |x|
7 tmp_new = "#{x}_features"
8 if tmp_new.empty?
9 puts "no"
10 else
11 puts "yes, tool_features arrary defined!\n"
12 puts tmp_new
13 end
14 end
輸出:
yes, tool_features arrary defined!
a_features
yes, tool_features arrary defined!
tool_features
yes, tool_features arrary defined!
b_features
我的本意是用變量來代表數(shù)組名來判斷一個數(shù)組是否存在,希望12行打印出數(shù)組tool_features的內(nèi)容f1f2
但顯然藍(lán)色部分(第8行)最終判斷的是字符串名是否存在,第12行實際上輸出的只是字符串的內(nèi)容,而不是同名的數(shù)組的內(nèi)容
有沒有辦法可以用變量動態(tài)代表數(shù)組名呢?
*******************************************************
或者換個問法:
1 #!/usr/bin/env ruby
2 tool_features = ['f1', 'f2']
3 a = "tool_features"
4
5 puts "#{a}"
輸出:
tool_features
我的本意是用變量來代表數(shù)組名,希望5行打印出數(shù)組tool_features的內(nèi)容f1f2
但顯然藍(lán)色部分(第5行)最終輸出的只是字符串a(chǎn)的內(nèi)容,而不是a中存的字符串代表的同名數(shù)組的內(nèi)容
有沒有辦法可以用變量動態(tài)代表數(shù)組名呢?
|
|