0

I want to split the statement array into 2 different lines in RUBY:

statement = ["Statement1", "Statement2"] 

This is how I am splitting the array

result = statement.split(", ") 

The output what I want is:

Statement1 Statement2 

But I am getting the error as:

undefined method `split' for ["Statement1", "Statement2"]:Array (NoMethodError) 

Can someone please help me? Thanks in advance

7
  • split is not defined for lists. It is defined only for strings. Commented Dec 27, 2016 at 13:58
  • Then what should i use to split it into 2 different lines? Commented Dec 27, 2016 at 14:00
  • I am really sorry for giving you half question. I want this in Ruby Commented Dec 27, 2016 at 14:03
  • Okay. I deleted my answer then. Commented Dec 27, 2016 at 14:05
  • See ideone.com/uW57E8, you do not need to split the items, you need to access them with indices, or using .first/.last. A list is already split, why split it again? Commented Dec 27, 2016 at 14:14

3 Answers 3

4

You can iterate over the array.

stmtAry = ["Statement1", "Statement2"] stmtAry.each{|i| puts i} 

You can directly output each statement without iterating on them if you don't need any other operation other than printing out.

stmtAry = ["Statement1", "Statement2"] puts stmtAry 

Both codes provides the same output:

Output:

Statement1 Statement2 
Sign up to request clarification or add additional context in comments.

2 Comments

IO#puts with an Array will already put each element on its own line without the need to iterate using #each.
@engineersmnky I update the answer according to your comment
3

It doesn't go much shorter :

puts statement # Statement1 # Statement2 

From the puts documentation :

If called with an array argument, writes each element on a new line.

If you want to get just one string from your array, you could use Array#join, which is the opposite of String#split :

statement.join("\n") 

It returns one string :

"Statement1\nStatement2" 

when displayed with puts :

Statement1 Statement2 

Comments

1

Your are trying call split method of Array class but Array class doesn't have this method.

You can check whether method is existed in calling class or not by using this way

statement = ["Statement1", "Statement2"] statement.methods [:inspect, :to_s, :to_a, :to_h, :to_ary, :frozen?, :==, :eql?, :hash, :[], :[]=, :at, :fetch, :first, :last, :concat, :<<, :push, :pop, :shift, :unshift, :insert, :each, :each_index, :reverse_each, :length, :size, :empty?, :find_index, :index, :rindex, :join, :reverse, :reverse!, :rotate, :rotate!, :sort, :sort!, :sort_by!, :collect, :collect!, :map, :map!, :select, :select!, :keep_if, :values_at, :delete, :delete_at, :delete_if, :reject, :reject!, :zip, :transpose, :replace, :clear, :fill, :include?, :<=>, :slice, :slice!, :assoc, :rassoc, :+, :*, :-, :&, :|, :uniq, :uniq!, :compact, :compact!, :flatten, :flatten!, :count, :shuffle!, :shuffle, :sample, :cycle, :permutation, :combination, :repeated_permutation, :repeated_combination, :product, :take, :take_while, :drop, :drop_while, :bsearch, :any?, :pack, :entries, :sort_by, :grep, :find, :detect, :find_all, :flat_map, :collect_concat, :inject, :reduce, :partition, :group_by, :all?, :one?, :none?, :min, :max, :minmax, :min_by, :max_by, :minmax_by, :member?, :each_with_index, :each_entry, :each_slice, :each_cons, :each_with_object, :chunk, :slice_before, :slice_after, :slice_when, :lazy, :nil?, :===, :=~, :!~, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :freeze, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :extend, :display, :method, :public_method, :singleton_method, :define_singleton_method, :object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__] 

So there is no split method in this above response that's why your are getting this error.

So if you face any issue in future like this and it would help you.

1 Comment

you could also use Array.public_method_defined?(:split) #=> false this will tell you if that class or an instance of that class has a method defined by name (excludes private and protected methods).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.