Literal syntax for array of arrays of strings

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…

From: Joel VanderWerf [mailto:[email protected]]

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 :slight_smile: (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”]]

Peña wrote:

t = table %q{\

           ^^^^

what is w the backquote?

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

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!

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”]]

2009/2/19 Joel VanderWerf [email protected]:

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

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 %?

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.

Robert K. wrote:

2009/2/19 7stud – [email protected]:

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?

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

2009/2/19 7stud – [email protected]:

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}”

On Fri, Feb 20, 2009 at 7:54 AM, Robert K.
[email protected] wrote:

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

On Fri, Feb 20, 2009 at 7:54 AM, Robert K.
[email protected] wrote:

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

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

Simon K. wrote:

mfg, simon … l

True, but that doesn’t allow copy-and-paste from a source that doesn’t
know about %.

On Sun, Feb 22, 2009 at 11:04 PM, Simon K. [email protected] wrote:

Page 320f
Would you mind telling us?
R.

On Sun, Feb 22, 2009 at 11:04 PM, Simon K. [email protected] 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

On Mon, Feb 23, 2009 at 1:04 PM, Simon K. [email protected] wrote:

  • Robert D. <robert.dober@gmail.
    "Double-quoted string literals (“stuff”, %Q/stuff/, and %/stuff/)
    thanx Simon, that what I wanted to know