literal syntax for array of arrays of strings

J

Joel VanderWerf

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...
 
P

Peña, Botp

From: Joel VanderWerf [mailto:[email protected]]=20
# Has anyone ever felt the need for something like
#=20
# a =3D %t{
# a b c
# d e f
# } # =3D=3D> [["a", "b", "c"], ["d", "e", "f"]]

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

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

now that is cool
=20
# t =3D table %q{\

^^^^
what is w the backquote?

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

on my case i get a,

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

had to do this instead,
table %q{ a b c
d e f
}

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

but it's still untamed,
table %q{ a b c
d e f
}
#=3D> [["a", "b", "c"], ["d", "e", "f"], []]


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


and you can indent it
table <<-HERE
a b c
d e f
HERE

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

Joel VanderWerf

Peña said:
# 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!
 
R

Robert Klemme

2009/2/19 Joel VanderWerf said:
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...

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
 
W

William James

Joel said:
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...

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"]]
 
T

Tom Link

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

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

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

Regards,
Thomas.
 
7

7stud --

Joel said:
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 %?
 
R

Robert Klemme

2009/2/19 7stud -- said:
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}"
 
7

7stud --

Robert said:
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?
 
R

Robert Klemme

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
 
R

Robert Dober

On Fri, Feb 20, 2009 at 7:54 AM, Robert Klemme
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

--
There are some people who begin the Zoo at the beginning, called
WAYIN, and walk as quickly as they can past every cage until they get
to the one called WAYOUT, but the nicest people go straight to the
animal they love the most, and stay there. ~ A.A. Milne (from
Winnie-the-Pooh)
 
S

Simon Krahnke

* Robert Dober said:
On Fri, Feb 20, 2009 at 7:54 AM, Robert Klemme

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
 
S

Simon Krahnke

* Joel VanderWerf said:
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
 
J

Joel VanderWerf

Simon said:
* Joel VanderWerf said:
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

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

Robert Dober

Page 320f
Would you mind telling us?
R.


--
There are some people who begin the Zoo at the beginning, called
WAYIN, and walk as quickly as they can past every cage until they get
to the one called WAYOUT, but the nicest people go straight to the
animal they love the most, and stay there. ~ A.A. Milne (from
Winnie-the-Pooh)
 
S

Simon Krahnke

* Robert Dober said:
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
 
R

Robert Dober

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



--
There are some people who begin the Zoo at the beginning, called
WAYIN, and walk as quickly as they can past every cage until they get
to the one called WAYOUT, but the nicest people go straight to the
animal they love the most, and stay there. ~ A.A. Milne (from
Winnie-the-Pooh)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top