变量魔术和从文件读取 [英] Variables magic and read from file
问题描述
name =彼得
年龄= 26
family_status =已婚
在一个随机的顺序,谎言:
family_status =已婚
name =Peter
年龄= 26
在我的程序中,我还有变量 family_status , / strong>和名称。如何在单个循环中读取文件中的这些行并为其赋值?
>这取决于几个事实。
- 你有什么样的变量(局部变量,实例变量,类变量或全局变量)
- family_status是什么类型(
String,
符号
,无论什么)
我假设你正在使用实例变量:
def read_vars(io,vars)
io.each do | line |
#确保行有正确的格式(name = var)
提升格式错误,除非行=〜/ ^ \s *(\w +)\s * = \s * ?(。*?)?\s + $ /
var =:@#{$ 1}
#选择数据类型
value = case vars [var]
当字符串
$ 2
当整数
$ 2.to_i
当符号
$ 2.to_sym
else
提高invalid var
end
instance_variable_set(var,value)
end
end
$ b read_vars(File.read(input.txt,:@age = > Integer,:@name => String,:@family_status => Symbol)
你没有使用实例变量,你必须改变
instacne_variable_set
和var =:@ ...
这个代码具有以下优点:$ b
$ b- 您可以控制哪些变量可以设置
- 控制这些变量的类型是什么
- 您可以轻松地添加新的变量您可以使用此代码读取完全不同的文件,而无需为其写入新代码。
/ ul>
阅读为YAML
如果您的需求不如您的问题具体,我会采用完全不同的方法到这个。
我会把 input.txt
写成yaml文件。在yaml语法中,它看起来像这样:
---
名称:Peter
年龄:26
family_status:已婚
您可以阅读:
YAML.load(File.read(input.txt))#=> {name=> 彼得,年龄=> 26,family_status=> :married}
如果您不控制 input.txt
文件,您不能控制数据的类型。我将命名文件 input.yaml
而不是 input.txt
。如果你想了解更多,关于如何写yaml文件请看: http://yaml.kwiki .ORG /?YamlInFiveMinutes 的。关于yaml和ruby的更多信息可以在 http:// www.ruby-doc.org/stdlib/libdoc/yaml/rdoc/index.html 。
Assume, i got the following file (input.txt):
name = "Peter"
age = 26
family_status = married
Mentioned lines can be stored in a random order, lie:
family_status = married
name = "Peter"
age = 26
In my program I also have variables family_status, age and name. How do I in a single cycle read those lines from file and assign correspond variables with the values?
Setting Variables
This depends on several facts.
- What kind of variables do you have (local variables, instance variables, class variables, or global variables)
- What kind of type is family_status (
String,
Symbol
, whatever)
I assume you are using instance variables for this:
def read_vars(io, vars)
io.each do |line|
# ensure the line has the right format (name = var)
raise "wrong format" unless line=~ /^\s*(\w+)\s*=\s*"?(.*?)"?\s+$/
var= :"@#{$1}"
# pick the type of the data
value= case vars[var]
when String
$2
when Integer
$2.to_i
when Symbol
$2.to_sym
else
raise "invalid var"
end
instance_variable_set(var, value)
end
end
read_vars(File.read("input.txt", :@age => Integer, :@name => String, :@family_status => Symbol )
If you are not using instance variables you have to change the instacne_variable_set
and var= :"@...
line to you needs. This code has the following advantages:
- You control which variables can be set
- You control which types these variables have
- You can easily add new variables and/or change types of variables without changing the read code
- You can use this code to read entirely different files, without writing new code for it
reading as YAML
If your needs are not as specific as in your question I would go an entirely different approach to this.
I would write the input.txt
as a yaml file. In yaml syntax it would look like this:
---
name: Peter
age: 26
family_status: :married
You can read it with:
YAML.load(File.read("input.txt")) # => {"name" => "Peter", "age" => 26, "family_status" => :married }
Be carefull if you don't control the input.txt
file, you don't control which types the data will have. I would name the file input.yaml
instead of input.txt
. If you want to know more, about how to write yaml files have a look at: http://yaml.kwiki.org/?YamlInFiveMinutes. More infos about yaml and ruby can be found at http://www.ruby-doc.org/stdlib/libdoc/yaml/rdoc/index.html.
这篇关于变量魔术和从文件读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!