Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/62.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 on rails 有人能帮我理解如何回收文本吗?_Ruby On Rails_Ruby_Rubymotion_Rubymotion Promotion - Fatal编程技术网

Ruby on rails 有人能帮我理解如何回收文本吗?

Ruby on rails 有人能帮我理解如何回收文本吗?,ruby-on-rails,ruby,rubymotion,rubymotion-promotion,Ruby On Rails,Ruby,Rubymotion,Rubymotion Promotion,所以我到处都查过了,很难找到这个。我正在用RedPotion在RubyMotion中编写一个应用程序,但我在这里需要的大部分帮助是Ruby。这个应用程序基本上是一个图书应用程序,所以我试图找出如何正确存储文本。基本上,我每行有一定数量的字符,在屏幕上只能使用17行。一旦填充完毕,我希望剩余的文本被存储起来,以便在页面翻转时,使用相同的方法显示在屏幕上的下一组文本中。然后,如果用户向后滑动,该文本也会返回。我试过数组,散列。不同的方法。在这个问题上已经疯狂了3周了。任何人都可以用ruby方法帮忙,

所以我到处都查过了,很难找到这个。我正在用RedPotion在RubyMotion中编写一个应用程序,但我在这里需要的大部分帮助是Ruby。这个应用程序基本上是一个图书应用程序,所以我试图找出如何正确存储文本。基本上,我每行有一定数量的字符,在屏幕上只能使用17行。一旦填充完毕,我希望剩余的文本被存储起来,以便在页面翻转时,使用相同的方法显示在屏幕上的下一组文本中。然后,如果用户向后滑动,该文本也会返回。我试过数组,散列。不同的方法。在这个问题上已经疯狂了3周了。任何人都可以用ruby方法帮忙,或者调整我的方法来工作

def on_load
 @texts = [
            "In the beginning of God's creation of the heavens and the           earth.\nNow the earth was
            astonishingly empty, and darkness was on the face of the deep, and the spirit of God was
            hovering over the face of the water.\nAnd God said, 'Let there be light,' and there was light.
            \nAnd God saw the light that it was good, and God separated between the light and between the darkness.
            \nAnd God called the light day, and the darkness He called night, and it was evening and it was morning,
            one day.\nAnd God said, 'Let there be an expanse in the midst of the water, and let it be a separation
            between water and water.'"
          ]


  @recycle = [ @texts[ 0 ] ]

  @page_idx = 0


  @header = append!( UIImageView, :header )
  @text_view = append!( UITextView, :text_view )
  text_view( @texts[ 0 ] )

   @text_view.on(:swipe_right) do |sender|
     recycle_text_forward( @recycle[ -1 ] )
   end

   @text_view.on(:swipe_left) do |sender|
     recycle_text_back( @recycle[ @page_idx ] )
   end
end

def text_view( text )
   page_words = text.split( ' ' )

   number_of_lines_idx = 17
   each_line_chars = 27
   chars = ""
   setter = ""
   idx = 0
   all_idx = page_words.length - 1

   @text_view.text = ""

   while number_of_lines_idx != 0
     until chars.length >= each_line_chars
     break if page_words[ idx ] == nil
     chars << page_words[ idx ] + " "
     chars << "\n" if chars.length >= each_line_chars
     idx += 1
   end

   break if page_words[ idx ] == nil

   number_of_lines_idx -= 1

   if number_of_lines_idx == 0
     @recycle << page_words[ idx..all_idx ]
     @page_idx = @recycle.length - 2
   end

   setter = chars
   chars = ""
   @text_view.text += setter
  end
end

def recycle_text_forward( text )
 text_view( text.join( ' ' ) )
end

def recycle_text_back( text )
 text_view( text )
end

我不确定我是否正确理解了这个问题,但以下是我的建议:

input = "In the beginning of God's creation..."

_, lines = input.split(/\s+/).each_with_object(["", []]) do |word, (line, lines)|
  if line.length + word.length + 1 <= 27
    line << " " << word
  else
    lines << line.dup
    line.replace(word)
  end
end #⇒ Here we got an array of lines, now split by screen size:

lines.each_slice(17).map { |*lines| lines.join(' ').strip }
#⇒ ["In the beginning...", "and it was evening and"]

我相信,这将是进一步调整的良好开端。

哇!非常感谢你所做的一切。我无法告诉你我有多感激你的回答,而且我的回答太快了!这非常有帮助,尤其是在重新格式化时。唯一剩下的就是我来存储数据。这17行只是全文的一部分。因此,我需要能够存储用户刷卡时的剩余文本,这样我就可以获得接下来的17行。但当用户向右滑动时,它还需要返回到前17行。我希望我说的有点道理。它已经存储在最后一行的结果中。它产生一系列屏幕。你自己试试看。