English 中文(简体)
红宝石 Math.radians
原标题:Ruby Math.radians
  • 时间:2010-02-24 08:31:31
  •  标签:
  • ruby
  • math

我需要Math.radians()函数,但找不到它。

radians=(angle/180)* Math::PI
最佳回答

你可能想要在lib/目录中创建一个名为“mymath.rb”的文件,并像这样猴子补丁Math:

require  mathn 
module Math
  def self.to_rad angle
    angle / 180.0 * Math::PI
  end
end

或者你可以像MBO在评论中所说的那样做。链接似乎无法访问,但是Google存档提供了这个信息丰富的小句子,它表明可能比我的解决方案更干净的解决方案(尽管我更喜欢在数学中保留数学内容)。

最简单的解决方案是在Numeric中定义一个转换方法,将角度数转换为弧度数。

作为说明,Ruby 2.0有一个称为“定义”的特征,基本上让你进行当地mon骑派。 它以这种方式开展工作(摘自,这个博客员额:

module RadiansConversion
  refine Math do
    def to_rad angle
      angle / 180.0 * Math::PI
    end
  end
end

然后……它可以在另一个模块内或类似的任何地方使用。

module MyApp
  using RadiansConversion

  p Math.to_rad 180   #=> 3.14159265358979
  p Math.to_rad 235   #=> 4.10152374218667
end
问题回答

或者,你可以扩大Float,增加一至二 convert改:

class Float
 def to_rad
   self/180 * Math::PI
  end
end

请用以下方式翻译:And use it like this。 并且像这样使用它。

radian=angle.to_rad

EDIT(通过表决): Sorry! 我回答时,我需要这样作,才能分享我发现的价值观。

所以,实际上就是:

x * (Math::PI / 180) 的中文翻译为:x * (数学::PI / 180)

我只是想展示我找到的一个执行方法。

# Geocoder::Calculations.to_radians(@geocode)
def to_radians(*args)
  args = args.first if args.first.is_a?(Array)
  if args.size == 1
    args.first * (Math::PI / 180)
  else
    args.map{ |i| to_radians(i) }
  end
end

将度数转换为弧度。如果传递了一个数组(或多个参数),则转换每个值并返回数组。

将此翻译成中文:http://rubydoc.info/github/alexreisner/geocoder/master/Geocoder/Calculations#to_radians-instance_method http://rubydoc.info/github/alexreisner/geocoder/master/Geocoder/Calculations#to_radians-instance_method





相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签