How can I redo this conditional code in ruby?

How to reorganize this function?

  def split_description(first_n)
    description_lines = description.split "\n"
    line_num = description_lines.length
    if line_num > first_n
      @description_first = to_html(description_lines[0..first_n].join("\n"))
      @description_remain = to_html(description_lines[first_n + 1..line_num].join("\n"))
    elsif line_num > 1
      @description_first = to_html(description_lines[0..first_n].join("\n"))
      @description_remain = ''
    else
      @description_first = ''
      @description_remain = ''
    end
  end

I run Ruby and come across this rubocup warning: Method has too many lines. [13/10]

Below is the whole code url: https://github.com/RubyStarts3/YPBT-app/blob/master/views_objects/video_info_view.rb

+4
source share
4 answers

the code

def split_description(description, first_n)
  @description_first, @description_remain =
  case description.count("\n")
  when 0..first_n
    [description, '']
  else
    partition_description(description, first_n)
  end.map(&:to_html)
end

def partition_description(description, first_n)
  return ['', description] if first_n.zero?
  offset = 0
  description.each_line.with_index(1) do |s,i|
    offset += s.size
    return [description[0,offset], description[offset..-1]] if i == first_n
  end
end

I assumed to_html('') #=> '', but if it is not, then the modification is simple.

Example

So that we can see the effect to_html, we define it this way.

def to_html(description)
  description.upcase
end

description =<<_
It was the best of times
it was the worst of times
it was the age of wisdom
it was the age of fools
_

split_description(description, 0)
@description_first
  #=> "" 
@description_remain
  #=> "IT WAS THE BEST OF TIMES\n..WORST OF TIMES\n..AGE OF WISDOM\n..AGE OF FOOLS\n" 

split_description(description, 1)
@description_first
  #=> "IT WAS THE BEST OF TIMES\n" 
@description_remain
  #=> "IT WAS THE WORST OF TIMES\n..AGE OF WISDOM\n..AGE OF FOOLS\n" 

split_description(description, 2)
@description_first
  #=> "IT WAS THE BEST OF TIMES\nIT WAS THE WORST OF TIMES\n" 
@description_remain
  #=> "IT WAS THE AGE OF WISDOM\nIT WAS THE AGE OF FOOLS\n" 

split_description(description, 3)
@description_first
  #=> "IT WAS THE BEST OF TIMES\n..WORST OF TIMES\n..AGE OF WISDOM\n" 
@description_remain
  #=> "IT WAS THE AGE OF FOOLS\n" 

split_description(description, 4)
@description_first
  #=> "IT WAS THE BEST OF TIMES\n..WORST OF TIMES\n..AGE OF WISDOM\n..AGE OF FOOLS\n" 
@description_remain
  #=> "" 

Explanation

First, it turns out that descriptionis a local variable containing a string. If so, this should be the argument of the method (along with first_n).

def split_description(description, first_n)

,

  @description_first, @description_remain =

: , to_html. .

  case description.count("\n")

,

  when 0
    [description, '']

, ['', '']; .

, 1 first_n. @description_first @description_remain .

  when 1..first_n
      [description, '']

when 0 when 1..first_n , :

  when 0..first_n
    [description, '']

, first_n . , , first_n.

  else
    partition_description(description, first_n)

partition_description description first_n - , .

, case, , to_html,

  end.map(&:to_html)
end

, to_html('') #=> ''. , .

, , , , .

+3

, .

  def split_description(first_n)
    description_lines = description.split "\n"
    line_num = description_lines.length

    @description_first = ''
    @description_remain = ''

    if line_num > first_n
      @description_first = to_html(description_lines[0..first_n].join("\n"))
      @description_remain = to_html(description_lines[first_n + 1..line_num].join("\n"))
    elsif line_num > 1
      @description_first = to_html(description_lines[0..first_n].join("\n"))
    end
  end

description_lines[first_n + 1..line_num].join("\n") , to_html( whatever_that_is( lines, from, to) ) . , , , .

+1

first_n 1, , :

...

@description_first = to_html(description_lines[0..first_n].join("\n")) if line_num > 1

  if line_num > first_n
        @description_remain = to_html(description_lines[first_n + 1..line_num].join("\n"))
  end
end
+1

:

def split_description(description, first_n = 0)
  lines = description.each_line
  @description_first  = to_html(lines.take(first_n).join)
  @description_remain = to_html(lines.drop(first_n).join)
end

take drop , , @Cary Swoveland :

  • ,
  • , ud ,

:

[1,2].take(99) #=> [1, 2]
[1,2].drop(99) #=> []

each_line Array of Strings, . split, chomp join("\n").

+1

Source: https://habr.com/ru/post/1664118/


All Articles