The main point of my short app was to find prices that consists of seven digits (greater than a million) from user's input.
我的简短应用程序的要点是从用户的输入中找到由七位数字(大于一百万)组成的价格。
I wrote this:
我写了这个:
class Price
attr_accessor :data
def initialize(str)
@data = str
end
def lookup
@data.select do |i|
i[/\d{1,7}+/]
end
end
def print_and_max
puts "Here comes the maximum"
# some code and voila
end
end
prices = gets.to_a
str = Price.new(prices)
print str.lookup
I get this error:
我收到此错误:
price.rb:21:in `<main>': undefined method `to_a' for "124789358124 12478912578915 50000000 50204500\n":String (NoMethodError)
OK, let's try again:
好的,让我们再试一次:
class Price
attr_accessor :data
prices = []
def initialize(str)
@data = str
end
def lookup
@data.select do |i|
i[/\d{1,7}+/]
end
end
def print_and_max
puts "Here comes the maximum"
# some code and voila
end
end
prices = gets
str = Price.new(prices)
print str.lookup
And then result is like that:
然后结果是这样的:
price.rb:11:in `lookup': private method `select' called for "124789358124 12478912578915 50000000 50204500":String (NoMethodError)
Seems that I completely don't understand method scope in Ruby. The main idea is to grab the string of numbers separated by spaces or something else, then convert it to array and print it. Writing method that will put out max value is optional. Why select method is private? I've tried to relate my Price class to Array as a child but select method remained private.
似乎我完全不理解Ruby中的方法范围。主要思想是抓住由空格或其他东西分隔的数字串,然后将其转换为数组并打印出来。将输出最大值的写入方法是可选的。为什么选择方法是私有的?我试图将我的Price类与Array关联为子,但select方法仍然是私有的。
I've tried this:
我试过这个:
prices = [125215213]
@data is accessible:
@data可访问:
irb(main):028:0* str.data
=> [125215213]
.lookup is not:
.lookup不是:
irb(main):029:0> str.lookup
TypeError: no implicit conversion of Regexp into Integer
from (irb):11:in `[]'
from (irb):11:in `block in lookup'
from (irb):10:in `select'
from (irb):10:in `lookup'
from (irb):29
from /Users/shu/.rbenv/versions/2.0.0-p481/bin/irb:12:in `<main>'
irb(main):030:0>
What am I doing wrong?
我究竟做错了什么?
2 个解决方案
#1
0
If the user input looks like this: "124789358124 12478912578915 50000000 50204500"
如果用户输入如下所示:“124789358124 12478912578915 50000000 50204500”
Then you can turn that into an array like this:
然后你可以把它变成这样的数组:
prices = gets.split
split is a String class method that splits chunks of text into array elements. By default it splits on whitespace, but you can pass it an argument if you're not splitting on whitespaces (looks like you are).
split是一个String类方法,它将文本块拆分为数组元素。默认情况下,它会在空格上分割,但是如果你没有在空格上分割(你看起来像是这样),你可以传递一个参数。
#2
0
You need to change this line:
您需要更改此行:
price = gets
to this:
price = gets.chomp.split(" ")
That will split the string into an array, separating it at every " " it finds. And chomp, will remove the newline that will be added after the user enters their input.
这将把字符串拆分成一个数组,在它找到的每个“”处将它分开。并且chomp将删除在用户输入其输入后将添加的换行符。
#1
0
If the user input looks like this: "124789358124 12478912578915 50000000 50204500"
如果用户输入如下所示:“124789358124 12478912578915 50000000 50204500”
Then you can turn that into an array like this:
然后你可以把它变成这样的数组:
prices = gets.split
split is a String class method that splits chunks of text into array elements. By default it splits on whitespace, but you can pass it an argument if you're not splitting on whitespaces (looks like you are).
split是一个String类方法,它将文本块拆分为数组元素。默认情况下,它会在空格上分割,但是如果你没有在空格上分割(你看起来像是这样),你可以传递一个参数。
#2
0
You need to change this line:
您需要更改此行:
price = gets
to this:
price = gets.chomp.split(" ")
That will split the string into an array, separating it at every " " it finds. And chomp, will remove the newline that will be added after the user enters their input.
这将把字符串拆分成一个数组,在它找到的每个“”处将它分开。并且chomp将删除在用户输入其输入后将添加的换行符。