give it as a struct: `class series def initialize(string) @string = string end def slices(n) raise argumenterror if n > @string.length @string.each_char.each_cons(n).map(&:join) end end` in ruby

In Ruby, struct is used to create a data structure with named members. However, the code you provided is already written in the form of a class, not a struct. If you want to convert the Series class into a struct, you can do it like this:

main.rb
Series = Struct.new(:string) do
  def slices(n)
    raise ArgumentError if n > string.length

    string.each_char.each_cons(n).map(&:join)
  end
end
150 chars
8 lines

This will define a Series struct with a single member string and a slices method.

related categories

gistlibby LogSnag