草庐IT

rational-numbers

全部标签

ruby - 为什么在 ruby​​ 中创建 Rational 不需要新方法

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:Rubysyntaxquestion:Rational(a,b)andRational.new!(a,b)我正在阅读ruby镐书,我对创建有理数的语法感到困惑。Rational(3,4)*Rational(1,2)产生=>3/8为什么Rational不需要new方法(我还注意到例如我可以在没有new方法的情况下创建字符串)?

ruby - 调用 instance_eval(&lambda) 传递当前上下文时出现错误 'wrong number of arguments'

要清楚-此代码运行完美-codewithproc但如果我将Proc.new更改为lambda,则会出现错误ArgumentError:wrongnumberofarguments(1for0)这可能是因为instance_eval想要将self作为参数传递,而lambda将其视为一种方法并且不接受未知参数?有两个例子-第一个是工作:classRuledefget_ruleProc.new{putsname}endendclassPersonattr_accessor:namedefinit_rule@name="ruby"instance_eval(&Rule.new.get_rule

ruby - 如何检查一个值是否是 Integer()、Float() 或 Rational() 的有效输入?

这大致基于“HowtoconvertaStringtoIntegerorFloat”。如果我想使用Ruby的内置转换机制将数字字符串输入转换为其“最合适的类型”,我可以这样做:defconvert(input)value=Integer(input)rescuenilvalue||=Float(input)rescuenilvalue||=Rational(input)rescuenilvalueendconvert('1')#=>1convert('1_000')#=>1000convert('0xff')#=>255convert('0.5')#=>0.5convert('1e2'

ruby - 我如何从 Rational(或任何没有构造函数的类)继承?

例如,我可以很容易地继承自String,如下所示:classMyString'thingsandstuff'但是我如何继承没有构造函数的Rational呢?例如:defMyRatNoMethodError:undefinedmethod`new'forMyRat:ClassMyRat(10).inc#=>NoMethodError:undefinedmethod`MyRat'formain:ObjectMyRat.send(:initialize,10).inc#=>TypeError:alreadyinitializedclass#???#Noneofitworks!我找不到初始化新

Ruby 语法问题 : Rational(a, b) 和 Rational.new!(a, b)

今天我在Rational中遇到了奇怪的ruby​​语法类:Rational(a,b)(请注意,与普通的Ruby语法相比,缺少.new()部分)。与普通的new语法相比,这究竟意味着什么?更重要的是,我如何在自己的代码中实现这样的东西,为什么要实现这样的东西?专门针对Rational类,为什么使用这种语法而不是正常的实例化呢?为什么new方法在rational类中是私有(private)的?(以及我如何/为什么要在我自己的ruby​​代码中这样做?)预先感谢您的回答,特别是因为我问了这么多问题。 最佳答案 您所要做的就是声明一个与您的

ruby (在 Rails 上)正则表达式 : removing thousands comma from numbers

这看起来很简单,但我遗漏了一些东西。我有大量来自各种来源和不同格式的输入。数字输入123123.45123,45(notethecommausedheretodenotedecimals)1,2341,234.5612,345.6712,345,67(notethecommausedheretodenotedecimals)关于输入的附加信息数字永远小于100万编辑:这些是价格,因此要么是整数,要么是百分之一我正在尝试编写一个正则表达式并使用gsub去除千位逗号。我该怎么做?我写了一个正则表达式:myregex=/\d+(,)\d{3}/当我在Rubular中测试它时,它表明它只在我想

ruby - 参数错误 : wrong number of arguments (1 for 0) when using afer_save

ArgumentError:wrongnumberofarguments(1for0)from/Users/Castillo/Desktop/gainer/app/models/status.rb:13:in`update_remaining_nutrients'from/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.11/lib/active_record/associations/collection_association.rb:507:in`blockincallback'from/usr/local/rvm/

ruby-on-rails - "if (number in range) then..."的 Ruby 快捷方式

是否有以下Ruby快捷方式?if(x>2)and(x我以为我看到了类似的东西,但找不到相关的引用资料。当然,当您不知道要查找的运算符时,很难查找。 最佳答案 if(3..9).include?x#whateverend作为旁注,您还可以对范围使用三等号运算符:if(3..9)===x#whateverend这让您也可以在case语句中使用它们:casexwhen3..9#Dosomethingwhen10..17#Dosomethingelseend 关于ruby-on-rails-"i

ruby - 为什么日期之间的减法会返回 Rational 类型?

我正在尝试对日期执行减法运算。date_sent=Date.parse("2013-01-01")#=>Tue,01Jan2013date_now=Date.today#=>Wed,04Sep2013days=(date_now-date_sent)#=>(246/1)为什么date_now-date_sent返回一个Rational类型? 最佳答案 这是预期的行为。来自docs:d-other→dateorrationalDate.new(2001,2,3)-1#=>#Date.new(2001,2,3)-Date.new(200

ruby - "wrong number of arguments"使用圆形时出现 ArgumentError

我正在尝试将温度从华氏度转换为摄氏度:puts'ConvertirgradosFahrenheitaCelcius'STDOUT.flushx=gets.chompaprox=(x*100.0).round(2)/100.0resultado=(aprox-32)/1.8putsresultado我使用正确的公式将华氏度转换为摄氏度:Celsius=Fahrenheit-32/1.8但是,当我在控制台中运行它时,出现以下错误:`round':wrongnumberofarguments(1for0)(ArgumentError)我尝试过不同的方法,但我不明白为什么这不起作用。