我有一节课:
class TestClass
def method1
end
def method2
end
def method3
end
end
Run Code Online (Sandbox Code Playgroud)
我怎样才能在这个类的我的方法列表(method1
,method2
,method3
)?
我如何在Postgres中进行此类查询?
IF (select count(*) from orders) > 0
THEN
DELETE from orders
ELSE
INSERT INTO orders values (1,2,3);
Run Code Online (Sandbox Code Playgroud) 这是我的阵列:
array = [:one,:two,:three]
Run Code Online (Sandbox Code Playgroud)
我想将to_s
方法应用于我的所有数组元素array = ['one','two','three']
.
我该怎么做(将可枚举的每个元素转换为其他元素)?
我正在学习Ruby Singletons,我找到了一些方法来定义和获取Class和Object单例方法的列表.
定义类单例方法的方法:
class MyClass
def MyClass.first_sing_method
'first'
end
def self.second_sing_method
'second'
end
class << self
def third_sing_method
'third'
end
end
class << MyClass
def fourth_sing_method
'fourth'
end
end
end
def MyClass.fifth_sing_method
'fifth'
end
MyClass.define_singleton_method(:sixth_sing_method) do
'sixth'
end
Run Code Online (Sandbox Code Playgroud)
获取班级单身人士方法列表的方法:
#get singleton methods list for class and it's ancestors
MyClass.singleton_methods
#get singleton methods list for current class only
MyClass.methods(false)
Run Code Online (Sandbox Code Playgroud)
定义对象单例方法的方法
class MyClass
end
obj = MyClass.new
class << obj
def first_sing_method
'first'
end
end …
Run Code Online (Sandbox Code Playgroud) 我使用rails控制台,我经常需要预加载一些ruby代码才能使用.
#file that i want to load in rails console
#my_file.rb
a = 1
b = 2
puts a + b
Run Code Online (Sandbox Code Playgroud)
当我使用./script/console运行我的控制台时
rails-console :001 > load 'my_file.rb'
3
=> []
rails-console :002 > a
NameError: undefined local variable or method 'a' for #<Object:123445>
Run Code Online (Sandbox Code Playgroud)
如何在控制台中访问我的'a'和'b'变量?
在IF块中,我需要检查某些条件是否为真,如果是,则退出块.
#something like this
if 1 == 1
return if some_object && some_object.property
puts 'hello'
end
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
我想了解Ruby方法的methods()
工作方式。
我尝试使用“红宝石方法”访问Google,但这不是我所需要的。
我也看过ruby-doc.org,但没有找到这种方法。
您能否详细说明它的工作原理或给我链接?
我尝试了methods()
方法,并得到了以下结果:
class First
def first_instance_mymethod
end
def self.first_class_mymethod
end
end
class Second < First
def second_instance_mymethod
end
def self.second_class_mymethod
end
end
Run Code Online (Sandbox Code Playgroud)
#returns available methods list for class and ancestors
puts Second.methods.grep(/mymethod/)
# => second_class_mymethod
# => first_class_mymethod
#returns Class methods list for current class only
puts Second.methods(false)
# => second_class_mymethod
Run Code Online (Sandbox Code Playgroud)
obj = Second.new
def obj.obj_singleton_mymethod
end
#returns available methods list for object and ancestors
puts obj.methods.grep(/mymethod/)
# => …
Run Code Online (Sandbox Code Playgroud) 鉴于下表:
student discipline mark
------- ---------- ----
1 math 5
1 phylosophy 4
1 literature 3
2 math 2
2 phylosophy 5
2 literature 5
Run Code Online (Sandbox Code Playgroud)
为每个学生获得最小分数的最佳方法是什么?(结果应为[3,2])
我如何在Ruby字符类中说"除反斜杠之外的所有符号"?
/'[^\]*'/.match("'some string \ hello'") => should be nil
Run Code Online (Sandbox Code Playgroud)
两个背面的变体不起作用
/'[^\\]*'/.match("'some string \ hello'") => 'some string \ hello' BUT should be nil
Run Code Online (Sandbox Code Playgroud) 我的数据库中有十进制字段.用户可以输入两种格式的值:逗号或点(11,11或11.11).
但MySQL允许仅以"点"格式保存数据,所以我想在使用正则表达式保存之前处理数据,如下所示:
sub(/,/,".")
Run Code Online (Sandbox Code Playgroud)
我怎么能在Rails3中做到这一点?
我在Firefox中使用Charles,现在我决定迁移到Chrome.在Firefox Charles自动配置扩展可用,它工作正常,但在Chrome Map Local不起作用.如何解决?
我得到包含用户角色,控制器名称和此角色可以访问的控制器操作列表的哈希.
access = {
'admin' => [ 'users' => ['edit','delete'],
'messages' => ['show','update']
],
'user' => [ 'index' => ['index','sign-out'],
'messages' => ['show','index']
]
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能检查access['admin']['users']['edit']
存在的是什么?
ruby ×8
if-statement ×2
postgresql ×2
sql ×2
arrays ×1
console ×1
database ×1
enumerable ×1
exit ×1
irb ×1
plpgsql ×1
reflection ×1
regex ×1
ruby-1.9.2 ×1