Ruby 1.9 How to Convert Array to String Without Brackets

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.

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.

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.

What's a good way to create a string array in Ruby based on integer variables?

I would do something like this:

coins = { toonie: 2, loonie: 1, quarter: 1, dime: 0, nickel: 1, penny: 3 }
coins.map { |k, v| pluralize(v, k) if v > 0 }.compact.join(', ')
#=> "2 toonie, 1 loonie, 1 quarter, 1 nickel, 3 penny"

Note that pluralize is a ActionView::Helpers::TextHelper method. Therefore it is only available in views and helpers.

When you want to use your example outside of views, you might want to use pluralize from ActiveSupport instead - what makes the solution slightly longer:

coins.map { |k, v| "#{v} #{v == 1 ? k : k.pluralize}" if v > 0 }.compact.join(', ')
#=> "2 toonie, 1 loonie, 1 quarter, 1 nickel, 3 penny"

removing array brackets

no, this doesn't make sense really, because ['a','b','c'],['d','e','f'] in this notation are two separate objects/arrays not inside any other data structure...

you could do an assignment, like :

a,b = [['a','b','c'],['d','e','f']]

and then

> a
=> ["a", "b", "c"]
> b
=> ["d", "e", "f"]

or better just iterate over the outer array (because you don't know how many elements it has):

input = [['a','b','c'],['d','e','f']]
input.each do |x|
puts "element #{x.inspect}"
end

=>
element ["a", "b", "c"]
element ["d", "e", "f"]

How could I convert an array of paths into nested array or hash dependant upon length

Code

def recurse(arr)
w_slash, wo_slash = arr.partition { |s| s.include?('/') }
a = w_slash.group_by { |s| s[/[^\/]+/] }.
map { |k,v| { k.to_sym=>recurse(v.map { |s| s[/(?<=\/).+/] }) } }
wo_slash.map(&:to_sym) + a
end

Examples

recurse array
#=> [:info,
# :services,
# {:about=>[:company, {:history=>[:part1, :part2]}]}]

arr = (array + ["a/b", "a/b/c", "a/b/c/d", "a/b/c/d/e"]).shuffle
#=> ["a/b/c", "services", "about/company", "about/history/part1", "info",
# "a/b", "about/history/part2", "a/b/c/d/e", "a/b/c/d"]
recurse arr
#=> [:services,
# :info,
# {:a=>[:b, {:b=>[:c, {:c=>[:d, {:d=>[:e]}]}]}]},
# {:about=>[:company, {:history=>[:part1, :part2]}]}]

Explanation

See Enumerable#partition and Enumerable#group_by.

The regular expression /[^\/]+/ reads, "match one or more characters that are not forward slashes". This could alternatively be written, s[0, s.index('/')].

The regular expression /(?<=\/).+/ reads, "match all characters following the first forward slash", (?<=\/) being a positive lookbehind. This could alternatively be written, s[s.index('/')+1..-1].

The initial steps are as follows.

w_slash, wo_slash = array.partition { |s| s.include?('/') }
#=> [["about/company", "about/history/part1", "about/history/part2"],
# ["info", "services"]]
w_slash
#=> ["about/company", "about/history/part1", "about/history/part2"]
wo_slash
#=> ["info", "services"]
h = w_slash.group_by { |s| s[/\A[^\/]+/] }
#=> {"about"=>["about/company", "about/history/part1", "about/history/part2"]}
a = h.map { |k,v| { k.to_sym=>recurse(v.map { |s| s[/(?<=\/).+/] }) } }
#=> [{:about=>[:company, {:history=>[:part1, :part2]}]}]
b = wo_slash.map(&:to_sym)
#=> [:info, :services]
b + a
#=> <as shown above>

In computing a, the first (and only) key-value pair of h is passed to the block and the two block variables are assigned values:

k,v = h.first
#=> ["about", ["about/company", "about/history/part1", "about/history/part2"]]
k #=> "about"
v #=> ["about/company", "about/history/part1", "about/history/part2"]

and the block calculations are preformed:

c = v.map { |s| s[/(?<=\/).+/] }
#=> ["company", "history/part1", "history/part2"]
{ k.to_sym=>recurse(c) }
#=> {:about=>[:company, {:history=>[:part1, :part2]}]}

and so on.

Add square bracket to the first character of string

With regex:

"abc".sub(/(.)/, '[\1]')

Without regex:

s = "abc"
s[0] = "[#{s[0]}]"


Related Topics



Leave a reply



Submit