Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
方法未在本地计算机中为ruby返回值_Ruby - Fatal编程技术网

方法未在本地计算机中为ruby返回值

方法未在本地计算机中为ruby返回值,ruby,Ruby,我对Ruby很陌生。几天前就开始了。我从一个叫做的网站上学习,在那里他们遇到了这个问题,我必须大写一个单词。我知道如何大写,但我使用的方法似乎没有返回大写字符串 我试过同样的代码。在那里,它似乎返回了我所期望的结果,但在我的本地机器上无法得到相同的结果。我不能理解这个问题。有人能给我解释一下吗 代码如下: class Book attr_accessor :title def title= str str.capitalize! end #title end #book

我对Ruby很陌生。几天前就开始了。我从一个叫做的网站上学习,在那里他们遇到了这个问题,我必须大写一个单词。我知道如何大写,但我使用的方法似乎没有返回大写字符串

我试过同样的代码。在那里,它似乎返回了我所期望的结果,但在我的本地机器上无法得到相同的结果。我不能理解这个问题。有人能给我解释一下吗

代码如下:

class Book
  attr_accessor :title
  def title= str
    str.capitalize!
  end #title

end #book

@book = Book.new
@book.title="inferno"
对于vs,后者就地修改字符串,如果未进行替换,则返回
nil
。但是,前者返回一个新字符串,并进行替换。然而,在ruby中使用这样的setter有一件奇怪的事情,我还没有完全理解:ruby返回传递给setter的值,而不是正常执行的最后一行

class YourBook
  attr_accessor :title

  def title= str
    str.capitalize!
  end #title
end #book

new_title = "inferno"
@book = YourBook.new
puts @book.title = new_title # => Inferno # side note, this is because you modified the original string as seen down below
puts @book.title             # =>         # was never set
puts new_title               # => Inferno # the string in the original variable was mutated, which is why the setter returned the "correct" version

class MyBook
  attr_reader :title

  def title=(str)
    @title = str.capitalize
  end

  def set_title(val) # even with the odd return value from `title=`, I still recommend that instead of using `set_` methods
    @title = val.capitalize
  end
end

new_title = "inferno"
@book = MyBook.new
puts @book.title = new_title  # => inferno # side note, not sure why it does this, but ruby returned the string you entered, not the string you set
puts @book.title              # => Inferno # was set correctly
puts new_title                # => inferno # the original string was not mutated

new_title = "inferno two"
puts @book.set_title(new_title) # => Inferno two # as expected
puts @book.title                # => Inferno two # also as expected
puts new_title                     # => inferno two # again, didn't modify the original

这将取决于你到底想要实现什么。我试着猜你想把整个单词大写,你必须使用
upcase
方法

编辑
我已经重写了您的代码以反映您的TDD需求。我正在以与教学TDD相同的方式初始化对象。您只需要
attr\u访问器
,它为
标题
生成setter和getter

class Book
  attr_accessor :title

  def name_capitalize
    # only first letter capitalized
    title.capitalize
  end #title

end #book

@book = Book.new
@book.title="inferno" # stores the book title in the `@book.title`
# to print the book's name and first letter in capital
puts @book.name_capitalize # uses the stored `title` variable to return the capitalized name

看看代码,试着理解你想要什么,也许我能让你头脑清醒

您的代码运行良好,大写方法工作正常。正如您在评论中所说,如果在方法中写入put,它将返回您期望的结果

@mudasobwa如果我使用看跌期权,它会将字符串改为大写 按预期打印方法内的字符串。但我不是 获得预期的回报

现在检查此方法的调用

@book.title="inferno"
在这里,我们可以看到“inferno”为title方法传递一个参数。然后这个方法返回值,但什么也得不到

如果您想检查返回的内容,只需使用puts(@book.title=“inferno”)


使用
大写
,不要使用
大写。而且,你的二传手什么也不设定。它会覆盖
attr\u accessor
中的setter。您是否在repl.it和您的本地计算机上尝试相同的输入?@SergioTulentsev我希望它返回大写字符串,但没有设置
@title
实例变量。是的,我对这两个都使用相同的输入。如果我不使用
大写字符串未更改。我不明白setter的意思。@mudasobwa:是的,除非字符串不能大写。“如果不做任何更改,则返回nil。”我希望使用字符串并将第一个字符大写,因此不是
upcase
,该项目是一个测试驱动的开发(这是问题的关键。根据这一点,是否有必要使用initialize方法?我明白了。要回答第一个主题,请只使用
标题。大写
。对于第二个主题,我将编辑答案以反映您向我展示的TDD链接。谢谢,这解决了问题。但您能给我一些解释吗错误???@radioactive001我唯一更改的是1)
attr\u访问器
->
attr\u读取器
,因为您定义了自己的setter,所以不需要默认的setter 2)您没有在setter方法中设置实例变量
@title
(顺便说一句,即使有这么一点奇怪,我还是推荐使用
set\u title
)和3)
大写将返回nil,并对原始字符串进行变异,这使事情看起来可以与
大写一起工作
和not with
大写
,您会注意到,在您给出的示例中,作者在调用setter后检查
@book.title
,而不是检查setter的返回值。这是为了确保他们记住了实际设置实例变量,因为我的代码示例中显示了奇怪的返回值
class Book
  attr_accessor :title
  def title= str
    str.capitalize!
  end #title

end #book

@book = Book.new
puts(@book.title="inferno")