what is wrong? `series = struct.new(:strng) { def slices(n) n > @strng.length ? argumenterror : @strng.each_char.each_cons(n).map(&:join) end }` in ruby

The code you provided has a few errors. Here's the corrected version:

main.rb
series = Struct.new(:strng) do
  def slices(n)
    n > @strng.length ? raise(ArgumentError, "Invalid slice length") : @strng.each_char.each_cons(n).map(&:join)
  end
end
170 chars
6 lines
  1. You need to capitalize Struct when creating a new structure.
  2. The block for Struct.new should be defined using do and end instead of {}.
  3. You need to use raise instead of argumenterror to raise an ArgumentError exception.
  4. each_char and each_cons are methods that can be used for iterating over characters and consecutive slices, respectively.
  5. The map method is used to join each consecutive slice into a string.

Now, you can use the series structure and call the slices method:

main.rb
my_series = series.new("123456")
puts my_series.slices(2) # Output: ["12", "23", "34", "45", "56"]
99 chars
3 lines

This will output an array of consecutive slices of length 2 from the given string.

Note: It is important to make sure the member variable name is correctly spelled when accessing it within methods. In your code, it should be @strng, not strng.

gistlibby LogSnag