使用params关键字的方法示例是 String.Format("", foo, bar, baz)
但是我如何创建一个接受如下枚举数组的方法:
class MyClass
{
public enum Foo { Bar, Baz }
public static void MyMethod(params enum[] Foo) {}
public static void TestMethod()
{
MyMethod();
MyMethod(Foo.Bar);
MyMethod(Foo.Baz);
MyMethod(Foo.Bar, Foo.Baz);
}
}
Run Code Online (Sandbox Code Playgroud) class Store
def check_inventory
@inventory ||= []
@inventory.each { ... }
end
end
Run Code Online (Sandbox Code Playgroud)
具有实例变量的行可以变成单行吗?
我一整天都在做这个,我无法弄清楚。我在下面的字符串中有一些 Ruby 代码,并且只想匹配带有代码的行以及代码的第一个注释(如果存在)。
# Some ignored comment.
1 + 1 # Simple math (this comment would be collected) # ignored
# ignored
user = User.new
user.name = "Ryan" # Setting an attribute # Another ignored comment
Run Code Online (Sandbox Code Playgroud)
这将捕获:
"1 + 1""Simple math""user = User.new"nil"user.name = "Ryan""Setting an attribute"我正在使用/^\x20*(.+)\x20*(#\x20*.+\x20*){1}$/来匹配每一行,但它似乎不适用于所有代码。
假设我有一个类似的课程,如下所示:
class User
attr_accessor :name, :age
def initialize(name, age)
@name, @age = name, age
end
end
Run Code Online (Sandbox Code Playgroud)
现在,将用户保存为单个文件中 User 类的编组实例或者使用带有 ORM 的 Sqlite 数据库会更快吗?基于文件的数据存储有哪些缺点?
在我的代码中,我通常使用以下设置:
module MyLib
VERSION = "0.1.1"
ERROR = [
"You can either give one arg and a block or two args, not both.",
"Yadda yadda..."
]
end
Run Code Online (Sandbox Code Playgroud)
然后我的代码中的某个地方:
def my_method(*args, &blk)
raise(ArgumentError, MyLib::ERROR[0]) if (...condition snipped...)
end
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来定义错误消息?
完整代码:http://friendpaste.com/5TdtGPZaEK0DbDBa2DCUyB
class Options
def method_missing(method, *args, &block)
p method
end
end
options = Options.new
options.instance_eval do
foo
foo = "It aint easy being cheesy!"
end
puts "#===---"
options.foo
options.foo = "It still aint easy being cheesy!"
Run Code Online (Sandbox Code Playgroud)
返回:
:foo
#===---
:foo
:foo=
Run Code Online (Sandbox Code Playgroud)
因为它foo = ""在instance_eval中被视为局部变量,所以它不会将其视为一种方法.
我如何让instance_eval将其视为一种方法?
我怎么能重构这个代码只使用一个Dir []调用?
Dir[ File.join('.', 'directory_a'), '*' ] + Dir[ File.join('.', 'directory_b'), '*' ]
=> [ contents of directory_a and directory_b ]
Run Code Online (Sandbox Code Playgroud) 我有一个类:
class Configuration
def self.files
@@files ||= Array.new
end
end
Run Code Online (Sandbox Code Playgroud)
而不是这样做:
irb(main):001:0> Configuration.files
=> [file, file, file]
Run Code Online (Sandbox Code Playgroud)
我希望能够做到这一点:
irb(main):001:0> Configuration
=> [file, file, file]
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚如何,任何想法?
我正在考虑为'配置'常量添加额外的方法,这是一个哈希.所以如果我有......
Configuration = Hash.new
Configuration[:foo] = 'bar'
Run Code Online (Sandbox Code Playgroud)
我希望能够保存此配置哈希常量,以便能够转储到我希望能够使用的YAML文件并加载...
Configuration.load
Configuration.save
Run Code Online (Sandbox Code Playgroud)
我希望Configuration类看起来像
class Configuration
def self.save
open('config.yml', 'w') {|f| YAML.dump( self , f)}
end
def self.load
open('config.yml') {|f| YAML.load(f)}
end
end
Run Code Online (Sandbox Code Playgroud) 假设我有一个像这样的INI文件:
[123]
name=Ryan
name=Joe
Run Code Online (Sandbox Code Playgroud)
如何使用API调用(如GetPrivateProfileSection和GetPrivateProfileString)检索"name = Ryan \nname = Joe" ?
有没有办法覆盖&对象上的方法,因此它在调用时返回一个数组:
class MyObject
end
o1, o2, o3 = MyObject.new, MyObject.new, MyObject.new
o1 # => #<MyObject0x01>
o1 & o2 # => [#<MyObject0x01>, #<MyObject0x02>]
o1 & o2 & o3 # => [#<MyObject0x01>, #<MyObject0x02>, #<MyObject0x03>]
Run Code Online (Sandbox Code Playgroud) 例如,我怎么能发现下午4点到下午8点之间的下午6点是50%?
或者星期三上午12点是星期二中午12点到星期三中午12点之间的50%?
有人可以帮我弄清楚下面代码的语法吗?
"AddonInfo"
{
"name" "Addon name"
"version" "Current Version"
"up_date" "Date of update"
"author_name" "Addon's Author"
"author_email" ""
"info" "Addon's Info"
"override" "0"
}
Run Code Online (Sandbox Code Playgroud) 鉴于以下类,我如何允许节点与其他节点(例如parent_node和child_nodes)具有多对多的关系?
class Node
include MongoMapper::Document
key :text, String
end
Run Code Online (Sandbox Code Playgroud)