- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我认为 #methods
方法会列出一个类中的所有方法。为什么当我键入 String.methods.sort
时 to_i
和 to_f
没有出现?
to_i
应该将字符串转换为整数,而 to_f
- 将字符串转换为 float 。
String.methods.sort
[:!, :!=, :!~, :<, :<=, :<=>, :==, :===, :=~, :>, :>=, :__id__,
:__send__, :allocate, :ancestors, :autoload, :autoload?, :class,
:class_eval, :class_exec, :class_variable_defined?,
:class_variable_get, :class_variable_set, :class_variables, :clone,
:const_defined?, :const_get, :const_missing, :const_set, :constants,
:define_singleton_method, :deprecate_constant, :display, :dup,
:enum_for, :eql?, :equal?, :extend, :freeze, :frozen?, :hash,
:include, :include?, :included_modules, :inspect, :instance_eval,
:instance_exec, :instance_method, :instance_methods, :instance_of?,
:instance_variable_defined?, :instance_variable_get,
:instance_variable_set, :instance_variables, :is_a?, :itself,
:kind_of?, :method, :method_defined?, :methods, :module_eval,
:module_exec, :name, :new, :nil?, :object_id, :prepend,
:private_class_method, :private_constant, :private_instance_methods,
:private_method_defined?, :private_methods,
:protected_instance_methods, :protected_method_defined?,
:protected_methods, :public_class_method, :public_constant,
:public_instance_method, :public_instance_methods, :public_method,
:public_method_defined?, :public_methods, :public_send,
:remove_class_variable, :remove_instance_variable, :respond_to?,
:send, :singleton_class, :singleton_class?, :singleton_method,
:singleton_methods, :superclass, :taint, :tainted?, :tap, :to_enum,
:to_s, :trust, :try_convert, :untaint, :untrust, :untrusted?]
最佳答案
因为 Object#methods
列出了给定对象的方法。
要列出给定类的实例方法,请使用 Module#instance_methods
:
String.instance_methods.grep /to_i|to_f/ # => [:to_i, :to_f]
或者,如果您有一个实例,您可以搜索它的方法:
'42'.methods.grep /to_i|to_f/ # => [:to_i, :to_f]
关于ruby - 为什么方法 to_i 和 to_f 没有出现在 String.methods 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49003706/
我觉得如果 "".to_i 和 "".to_f 为 nil 或引发异常会很自然,而且确实如此定义 nil.to_i 或 nil.to_f 对我来说意义不大。 nil.to_i #=> 0, "".to
您好,我刚刚遇到一个问题,即 ruby 的 to_f 函数给我的结果不一致。 ruby-1.9.2-head :026 > 8.45.to_f * 100 => 844.999999999999
"9.99".to_f => 999.0 这是预期的行为吗?如何将“9.99”转换为 9.99 最佳答案 您在哪个语言环境中运行?我的猜测是它正在处理“。”作为千位分隔符(在本例中恰好位于错误的位置)
我正在使用 Rails 3.2.1 和 Jbuilder 构建一个简单的 JSON API在 Ruby 1.8.7 上(1.9.x 在这里可能对我有帮助,但我的托管服务提供商只有 1.8.7)。 由于
BigDecimal 类被破坏了吗?似乎永远不会发生以下情况: 注意 a.to_f != a.to_s.to_f a.class => BigDecimal a.to_f => 18658.10729
我试图找出数字字符串与任意字符串之间的区别: '0'.to_f #=> 0.0 'hello'.to_f #=> 0.0 以上两者都返回一个Float。如果用户输入实际值 '0' 或用户输入值 'he
我看到人们使用以下代码: gets.chomp.to_i 或 gets.chomp.to_f 我不明白为什么,当这些行的结果总是与 gets 之后没有 chomp 时的结果相同。 gets.chomp
我认为 #methods 方法会列出一个类中的所有方法。为什么当我键入 String.methods.sort 时 to_i 和 to_f 没有出现? to_i 应该将字符串转换为整数,而 to_f
这对我来说很奇怪: irb(main):012:0> "100.7".to_f.modulo(1) => 0.700000000000003 为什么最后是 3? irb(main):019:0> "1
我是一名优秀的程序员,十分优秀!