Why Does Array.To_S Return Brackets

Why is my array not returned in brackets?

Calling #puts on an array will output each element in that array to its own line. You can output the array, along with brackets, by calling #to_s on the array object:

puts values.to_s
# => ["your", "array", "on", "one", "line"]

Hope that helps!

Ruby 1.9 Array.to_s behaves differently?

Yes, you're calling to_s on an array of strings. In 1.8 that is equivalent to calling join, in 1.9 it is equivalent to calling inspect.

To get the behavior you want in both 1.8 and 1.9, call join instead of to_s.

Get array element values without square brackets and double quotes

def join_with_commas_and_and(array)
if array.length <= 2
array.join(' and ')
else
[array[0..-2].join(', '), array[-1]].join(' and ')
end
end

EDIT: to ignore the last element, add this line as the first line in the function:

array = array[0..-2]

ruby 1.9 how to convert array to string without brackets

Use interpolation instead of concatenation:

reportStr = "In the first quarter we sold #{myArray[3]} #{myArray[0]}(s)."

It's more idiomatic, more efficient, requires less typing and automatically calls to_s for you.

Map function returning variables with brackets and quotes

You're using string interpolation in an improper way to include values into an SQL statement.

This will work, instead:

query = "INSERT INTO pd_activities (id) VALUES ("#{query_values.join(', ')}")"

If the values in activities are integers, you can avoid string interpolation in the activities.map step. To do this, simply use this:

query_values = activities.map {|activity| activity['id'] }

If activities = [12, 34, 98, 142], this will produce:

"INSERT INTO pd_activities (id) VALUES (12, 34, 98, 142)"

However, if the values in activities are strings, you should do this instead:

query_values = activities.map {|activity| "'#{activity['id']}'" }

If activities = ["12", "34", "98", "142"], this will produce:

"INSERT INTO pd_activities (id) VALUES ('12', '34', '98', '142')"

Getting the string from a rails pluck to display n

Try this:

<%= chatroom.messages.last.created_at %>

And this:

<%= chatroom.messages.last.body %>

Keep in mind that pluck returns an array, so that would explain your brackets.

I don't think you need pluck here since you are just accessing an attribute on a single item.

how do i print the element of an array without brackets or quotes?

If your array only has one element you can do this:

# creating the values manually for the example
parsed_category_parent = "first"
saved_category_parent = ["first"]

# solution
parsed_category_parent = saved_category_parent[0]

You can try it in IRB to see that it works.

By using the [] operator you are accessing the first element of the array which is a string, thus comparing two strings together rather than comparing a string with an array containing a string.

You could also use other methods of accessing elements in an array like "first".

Ruby automatically calls to_s method when object is created

You're sending it to puts, which will try to render the object as a string using to_s.

If you changed your last line to: puts A.new("hello world", 5).to_a, it would instead call to_s on the returned Array and A's to_s would not be called.



Related Topics



Leave a reply



Submit