小编Vla*_*nov的帖子

获取类的实例方法列表

我有一节课:

class TestClass
  def method1
  end

  def method2
  end

  def method3
  end
end
Run Code Online (Sandbox Code Playgroud)

我怎样才能在这个类的我的方法列表(method1,method2,method3)?

ruby reflection ruby-1.9.2

92
推荐指数
6
解决办法
9万
查看次数

PostgreSQL IF语句

我如何在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)

sql postgresql if-statement plpgsql

63
推荐指数
4
解决办法
16万
查看次数

将方法应用于array/enumerable中的每个元素

这是我的阵列:

array = [:one,:two,:three]
Run Code Online (Sandbox Code Playgroud)

我想将to_s方法应用于我的所有数组元素array = ['one','two','three'].

我该怎么做(将可枚举的每个元素转换为其他元素)?

ruby arrays enumerable

39
推荐指数
4
解决办法
4万
查看次数

用于类和对象的Ruby Singleton方法

我正在学习Ruby Singletons,我找到了一些方法来定义和获取Class和Object单例方法的列表.

类Singleton方法

定义类单例方法的方法:

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)

ruby

21
推荐指数
2
解决办法
9261
查看次数

将文件加载到rails控制台,可以访问此文件中定义的变量

我使用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'变量?

ruby console ruby-on-rails irb

14
推荐指数
1
解决办法
1万
查看次数

Ruby - 退出IF块

在IF块中,我需要检查某些条件是否为真,如果是,则退出块.

#something like this
if 1 == 1
  return if some_object && some_object.property
  puts 'hello'
end
Run Code Online (Sandbox Code Playgroud)

我该怎么做?

ruby if-statement exit

13
推荐指数
1
解决办法
3万
查看次数

Ruby Methods()方法

我想了解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)

ruby

5
推荐指数
2
解决办法
7017
查看次数

如何获得每个外键的最小值

鉴于下表:

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])

sql database postgresql

4
推荐指数
1
解决办法
632
查看次数

正则表达式匹配除反斜杠之外的任何字符

我如何在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)

ruby regex

4
推荐指数
2
解决办法
2万
查看次数

保存到数据库之前处理数据

我的数据库中有十进制字段.用户可以输入两种格式的值:逗号或点(11,11或11.11).

但MySQL允许仅以"点"格式保存数据,所以我想在使用正则表达式保存之前处理数据,如下所示:

sub(/,/,".")
Run Code Online (Sandbox Code Playgroud)

我怎么能在Rails3中做到这一点?

ruby-on-rails ruby-on-rails-3

3
推荐指数
1
解决办法
6387
查看次数

Charles代理 - 如何在Chrome中使用Map Local工具

我在Firefox中使用Charles,现在我决定迁移到Chrome.在Firefox Charles自动配置扩展可用,它工作正常,但在Chrome Map Local不起作用.如何解决?

google-chrome charles-proxy

2
推荐指数
1
解决办法
6180
查看次数

散列数组 - 获取数组元素

我得到包含用户角色,控制器名称和此角色可以访问的控制器操作列表的哈希.

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

1
推荐指数
1
解决办法
309
查看次数