Appropriate use of "inject()"?

I have a hash table HASH containing strings, and want to generate a string
for an equivalent Javascript variable JS from it e.g.

HASH = {'a' => 'A', 'b' => 'B'}

"var JS = { 'a' : 'A', 'b' : 'B' }"

I thought inject was just the ticket:

'var JS = {' +
  HASH.inject("") { |str, kv| str << "\n\t'#{kv[0]}' : #{kv[1]}, " } +
  "};"

But this puts a "," after the last item. Is there a simple alternative?

Thanks.

itsme213 said:

I have a hash table HASH containing strings, and want to generate a string
for an equivalent Javascript variable JS from it e.g.

HASH = {'a' => 'A', 'b' => 'B'}

"var JS = { 'a' : 'A', 'b' : 'B' }"

Here is another option:

HASH = {'a' => 'A', 'b' => 'B'}
puts "var JS = { #{HASH.collect{|k,v| " '#{k}' : '#{v}'"}.join(',')} }"

Ryan

I think it's best solved in two steps, like this:

HASH.map { |k,v| "'#{k}' : '#{v}'"}.join(',')

Or you can do it with inject, but it's not as nice:

HASH.inject do |str,kv|
  if str.class == Array
    str = "'#{str[0]}' : '#{str[1]}'"
  end
  str << ", '#{kv[0]}' : '#{kv[1]}'"
end

···

On Fri, Sep 16, 2005 at 03:31:34AM +0900, itsme213 wrote:

I have a hash table HASH containing strings, and want to generate a string
for an equivalent Javascript variable JS from it e.g.

HASH = {'a' => 'A', 'b' => 'B'}

"var JS = { 'a' : 'A', 'b' : 'B' }"

I thought inject was just the ticket:

'var JS = {' +
  HASH.inject("") { |str, kv| str << "\n\t'#{kv[0]}' : #{kv[1]}, " } +
  "};"

But this puts a "," after the last item. Is there a simple alternative?

itsme213 wrote:

I have a hash table HASH containing strings, and want to generate a string
for an equivalent Javascript variable JS from it e.g.

HASH = {'a' => 'A', 'b' => 'B'}

"var JS = { 'a' : 'A', 'b' : 'B' }"

I thought inject was just the ticket:

'var JS = {' +
  HASH.inject("") { |str, kv| str << "\n\t'#{kv[0]}' : #{kv[1]}, " } +
  "};"

But this puts a "," after the last item. Is there a simple alternative?

Thanks.

puts "var JS = {\n\t" + HASH.map{|k,v|
  "'#{k}' : #{v}" }.join(",\n\t") + " };"

The simplest is the two-pass

'var JS = {' + HASH.map {|k,v| "'#{k}' : '#{v}'"}.join(",\n\t") + "};"

#join has extra logic to specialcase the last element, which is painful
with #inject.

martin

···

itsme213 <itsme213@hotmail.com> wrote:

I have a hash table HASH containing strings, and want to generate a string
for an equivalent Javascript variable JS from it e.g.

HASH = {'a' => 'A', 'b' => 'B'}

"var JS = { 'a' : 'A', 'b' : 'B' }"

I thought inject was just the ticket:

'var JS = {' +
  HASH.inject("") { |str, kv| str << "\n\t'#{kv[0]}' : #{kv[1]}, " } +
  "};"

But this puts a "," after the last item. Is there a simple alternative?

Ah, I need to think more "functional programming".

Thanks a bunch, all. Useful general lesson for me.

"Martin DeMello" <martindemello@yahoo.com> wrote in message
news:3BjWe.204172$Hk.56955@pd7tw1no...

> I have a hash table HASH containing strings, and want to generate a

string

···

itsme213 <itsme213@hotmail.com> wrote:
> for an equivalent Javascript variable JS from it e.g.
>
> HASH = {'a' => 'A', 'b' => 'B'}
>
> "var JS = { 'a' : 'A', 'b' : 'B' }"
>
> I thought inject was just the ticket:
>
> 'var JS = {' +
> HASH.inject("") { |str, kv| str << "\n\t'#{kv[0]}' : #{kv[1]}, " } +
> "};"
>
> But this puts a "," after the last item. Is there a simple alternative?

The simplest is the two-pass

'var JS = {' + HASH.map {|k,v| "'#{k}' : '#{v}'"}.join(",\n\t") + "};"

#join has extra logic to specialcase the last element, which is painful
with #inject.

martin

itsme213 wrote:

Ah, I need to think more "functional programming".

Thanks a bunch, all. Useful general lesson for me.

"Martin DeMello" <martindemello@yahoo.com> wrote in message
news:3BjWe.204172$Hk.56955@pd7tw1no...

I have a hash table HASH containing strings, and want to generate a
string for an equivalent Javascript variable JS from it e.g.

HASH = {'a' => 'A', 'b' => 'B'}

"var JS = { 'a' : 'A', 'b' : 'B' }"

I thought inject was just the ticket:

'var JS = {' +
  HASH.inject("") { |str, kv| str << "\n\t'#{kv[0]}' : #{kv[1]}, "
  } + "};"

But this puts a "," after the last item. Is there a simple
alternative?

The simplest is the two-pass

'var JS = {' + HASH.map {|k,v| "'#{k}' : '#{v}'"}.join(",\n\t") +
"};"

#join has extra logic to specialcase the last element, which is
painful with #inject.

Here's a solution with #inject:

HASH = {'a' => 'A', 'b' => 'B'}

=> {"a"=>"A", "b"=>"B"}

"var JS = {" << HASH.inject("") {|s,(k,v)| s << ", " unless s.empty?; s

<< "'" << k << "' : '" << v << "'"} << "}"
=> "var JS = {'a' : 'A', 'b' : 'B'}"

This is ugly.

    robert

···

itsme213 <itsme213@hotmail.com> wrote: