Forum: Ruby literal syntax for array of arrays of strings

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Joel VanderWerf (Guest)
on 2009-02-19 09:13
(Received via mailing list)
We have this:

a = %w{ a b c d e f }
p a # ==> ["a", "b", "c", "d", "e", "f"]

Has anyone ever felt the need for something like

a = %t{
  a b c
  d e f
} # ==> [["a", "b", "c"], ["d", "e", "f"]]

It's probably too special (this is the first time in 8 years I've wanted
it), and it is easy to implement it as a method rather than a literal:

def table s
   s.split("\n").map!{|t|t.split}
end

t = table %q{\
  a b c
  d e f
}

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

Really just an idle question...
Peña, Botp (Guest)
on 2009-02-19 09:55
(Received via mailing list)
From: Joel VanderWerf [mailto:removed_email_address@domain.invalid]
# Has anyone ever felt the need for something like
#
# a = %t{
#   a b c
#   d e f
# } # ==> [["a", "b", "c"], ["d", "e", "f"]]

now the invisible ink (newline) has power :) (cp dblack)


# It's probably too special (this is the first time in 8 years
# I've wanted
# it), and it is easy to implement it as a method rather than a literal:
#
# def table s
#    s.split("\n").map!{|t|t.split}
# end

now that is cool

# t = table %q{\

               ^^^^
what is w the backquote?

#   a b c
#   d e f
# }
#
# p t  # ==> [["a", "b", "c"], ["d", "e", "f"]]

on my case i get a,

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

had to do this instead,

> table %q{ a b c
d e f
}

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


but it's still untamed,

> table %q{ a b c
            d e f
          }
#=> [["a", "b", "c"], ["d", "e", "f"], []]


i prefer instead the here-doc

> table <<HERE
a b c
d e f
HERE
#=> [["a", "b", "c"], ["d", "e", "f"]]


and you can indent it

> table <<-HERE
    a b c
    d e f
  HERE

#=> [["a", "b", "c"], ["d", "e", "f"]]
Joel VanderWerf (Guest)
on 2009-02-19 10:23
(Received via mailing list)
Peña wrote:
> # t = table %q{\
>
>                ^^^^
> what is w the backquote?

To avoid an empty array, but I messed that up :(

This is what I had originally (but I inserted the 'q' to make it a
single-quoted string, like the first example):

tb = table %{\
  a b c
  d e f
}

p tb

> i prefer instead the here-doc
>
>> table <<HERE
> a b c
> d e f
> HERE
> #=> [["a", "b", "c"], ["d", "e", "f"]]

Nice!
Robert K. (Guest)
on 2009-02-19 10:24
(Received via mailing list)
2009/2/19 Joel VanderWerf <removed_email_address@domain.invalid>:
>  d e f
>  a b c
>  d e f
> }
>
> p t  # ==> [["a", "b", "c"], ["d", "e", "f"]]
>
> Really just an idle question...

You can also exploit the line iteration capabilities of String, which
is especially easy in 1.8.x

09:20:42 ddl$ irb19
Ruby version 1.9.1
irb(main):001:0> s = <<EOF
irb(main):002:0" a b c
irb(main):003:0" 1 2 3
irb(main):004:0" r t z
irb(main):005:0" EOF
=> "a b c\n1 2 3\nr t z\n"
irb(main):006:0> s.each_line.map {|x| x.scan /\S+/}
=> [["a", "b", "c"], ["1", "2", "3"], ["r", "t", "z"]]
irb(main):007:0> exit
09:21:14 ddl$ irb
Ruby version 1.8.7
irb(main):001:0> s = <<EOF
irb(main):002:0" a b c
irb(main):003:0" 1 2 3
irb(main):004:0" r t z
irb(main):005:0" EOF
=> "a b c\n1 2 3\nr t z\n"
irb(main):006:0> s.map {|x| x.scan /\S+/}
=> [["a", "b", "c"], ["1", "2", "3"], ["r", "t", "z"]]
irb(main):007:0> exit
09:21:48 ddl$

Kind regards

robert
William J. (Guest)
on 2009-02-19 10:40
(Received via mailing list)
Joel VanderWerf wrote:

>   d e f
> t = table %q{\
>   a b c
>   d e f
> }
>
> p t  # ==> [["a", "b", "c"], ["d", "e", "f"]]
>
> Really just an idle question...

class Array
  def slices n
    a = []
    each_slice(n){|s| a << s}
    a
  end
end
    ==>nil
%w[a b c d e f].slices 3
    ==>[["a", "b", "c"], ["d", "e", "f"]]
Tom L. (Guest)
on 2009-02-19 11:12
(Received via mailing list)
> t = table %q{\
>   a b c
>   d e f
> }

I'd rather prefer a String#to_table method, I guess.

class String
    def to_table(rx=nil)
        split("\n").map!{|t|t.split(rx)}
    end
end

Regards,
Thomas.
7stud -. (Guest)
on 2009-02-19 14:57
Joel VanderWerf wrote:
>
> This is what I had originally (but I inserted the 'q' to make it a
> single-quoted string, like the first example):
>
> tb = table %{\
>   a b c
>   d e f
> }
>

What's the difference between %Q and %?
Robert K. (Guest)
on 2009-02-19 15:11
(Received via mailing list)
2009/2/19 7stud -- <removed_email_address@domain.invalid>:
>
> What's the difference between %Q and %?

irb(main):002:0> %{foo bar #{1+2}}
=> "foo bar 3"
irb(main):003:0> %Q{foo bar #{1+2}}
=> "foo bar 3"
irb(main):004:0> %q{foo bar #{1+2}}
=> "foo bar \#{1+2}"
7stud -. (Guest)
on 2009-02-19 15:26
Robert K. wrote:
> 2009/2/19 7stud -- <removed_email_address@domain.invalid>:
>>
>> What's the difference between %Q and %?
>
> irb(main):002:0> %{foo bar #{1+2}}
> => "foo bar 3"
> irb(main):003:0> %Q{foo bar #{1+2}}
> => "foo bar 3"
> irb(main):004:0> %q{foo bar #{1+2}}
> => "foo bar \#{1+2}"

Uhmm...yeah.  Those are the same results I got.  Why would anyone use %Q
then?  And where is the use of % like that documented?
Robert K. (Guest)
on 2009-02-20 08:55
(Received via mailing list)
On 19.02.2009 14:26, 7stud -- wrote:
> Uhmm...yeah.  Those are the same results I got.  Why would anyone use %Q
> then?  And where is the use of % like that documented?

To make the quoting more obvious.

It must be somewhere in the Pickaxe but I don't have my copy handy so I
cannot provide the page reference.

Kind regards

  robert
Robert D. (Guest)
on 2009-02-20 11:16
(Received via mailing list)
On Fri, Feb 20, 2009 at 7:54 AM, Robert K.
<removed_email_address@domain.invalid> wrote:
<snip>
> It must be somewhere in the Pickaxe but I don't have my copy handy so I
> cannot provide the page reference.
The new Pickaxe does not mention %, I have posted this to the erratum.
Would be interesting what was mentioned in the second edition.
Cheers
R
Simon K. (Guest)
on 2009-02-23 00:05
(Received via mailing list)
* Robert D. <removed_email_address@domain.invalid> (2009-02-20) schrieb:

> On Fri, Feb 20, 2009 at 7:54 AM, Robert K.
> <removed_email_address@domain.invalid> wrote:
> <snip>
>> It must be somewhere in the Pickaxe but I don't have my copy handy so I
>> cannot provide the page reference.
> The new Pickaxe does not mention %, I have posted this to the erratum.
> Would be interesting what was mentioned in the second edition.

Page 320f

mfg,                  simon .... always lying next to me
Simon K. (Guest)
on 2009-02-23 00:06
(Received via mailing list)
* Joel VanderWerf <removed_email_address@domain.invalid> (2009-02-19) schrieb:

> Has anyone ever felt the need for something like
>
> a = %t{
>   a b c
>   d e f
> } # ==> [["a", "b", "c"], ["d", "e", "f"]]

a = [ %w(a b c), %w(d e f) ]

mfg,                     simon .... l
Joel VanderWerf (Guest)
on 2009-02-23 00:32
(Received via mailing list)
Simon K. wrote:
>
> mfg,                     simon .... l

True, but that doesn't allow copy-and-paste from a source that doesn't
know about %.
Robert D. (Guest)
on 2009-02-23 01:46
(Received via mailing list)
On Sun, Feb 22, 2009 at 11:04 PM, Simon K. 
<removed_email_address@domain.invalid> wrote:

> Page 320f
Would you mind telling us?
R.
Simon K. (Guest)
on 2009-02-23 14:06
(Received via mailing list)
* Robert D. <removed_email_address@domain.invalid> (00:45) schrieb:

> On Sun, Feb 22, 2009 at 11:04 PM, Simon K. <removed_email_address@domain.invalid> wrote:
>
>> Page 320f
>
> Would you mind telling us?

It's in chapter 22 The Ruby Language, Basic Types, Strings:

"Double-quoted string literals ("stuff", %Q/stuff/, and %/stuff/)
undergo additional substitutions, shown in table 22.2 on the next page."

That table shows the backslash escapes and #{code}.

mfg,                       simon .... l
Robert D. (Guest)
on 2009-02-23 15:57
(Received via mailing list)
On Mon, Feb 23, 2009 at 1:04 PM, Simon K. <removed_email_address@domain.invalid> 
wrote:
> * Robert D. <robert.dober@gmail.
> "Double-quoted string literals ("stuff", %Q/stuff/, and %/stuff/)
thanx Simon, that what I wanted to know
This topic is locked and can not be replied to.