[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

literal syntax for array of arrays of strings

Joel VanderWerf

2/19/2009 7:13:00 AM


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

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

16 Answers

Peña, Botp

2/19/2009 7:54:00 AM

0

From: Joel VanderWerf [mailto:vjoel@path.berkeley.edu]=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"]]


Joel VanderWerf

2/19/2009 8:21:00 AM

0

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!

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Robert Klemme

2/19/2009 8:23:00 AM

0

2009/2/19 Joel VanderWerf <vjoel@path.berkeley.edu>:
>
> 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

--
remember.guy do |as, often| as.you_can - without end

William James

2/19/2009 8:34:00 AM

0

Joel VanderWerf wrote:

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

ThoML

2/19/2009 9:11:00 AM

0

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

7stud --

2/19/2009 12:57:00 PM

0

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 %?
--
Posted via http://www.ruby-....

Robert Klemme

2/19/2009 1:11:00 PM

0

2009/2/19 7stud -- <bbxx789_05ss@yahoo.com>:
> 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 %?

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


--
remember.guy do |as, often| as.you_can - without end

7stud --

2/19/2009 1:27:00 PM

0

Robert Klemme wrote:
> 2009/2/19 7stud -- <bbxx789_05ss@yahoo.com>:
>>
>> 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?
--
Posted via http://www.ruby-....

Robert Klemme

2/20/2009 6:53:00 AM

0

On 19.02.2009 14:26, 7stud -- wrote:
> Robert Klemme wrote:
>> 2009/2/19 7stud -- <bbxx789_05ss@yahoo.com>:
>>> 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?

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 Dober

2/20/2009 9:15:00 AM

0

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

--
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)