-q, --quiet quiet
-f, --files files:if… input files
-n, --number num:n a float number
-i, --integer num:i an integer number
EOF
p args[‘-q’]
p args[‘-f’]
p args.unused
= What’s NEW in v1.20?
Parameter definitions can now also be specified using GNU style help
lines, like in the example above.
Parameter definitions can now be recognized by the use either of one
or more tabs (like before) or by 3 consecutive spaces. This simplifies
parameter definitions quite a lot, particularly for those using editors
that are not friendly to tabs.
But I never knew that block quoted strings worked like this:
args = Getopt::Declare.new(<<‘EOF’)
-q, --quiet quiet
-f, --files files:if… input files
-n, --number num:n a float number
-i, --integer num:i an integer number
EOF
That’s just completely cool! And it also makes a lot of sense: I think
I’ll actually remember it now.
You can do somethin like this:
irb(main):008:0> puts <<‘STRING_END’.gsub(‘a’) {‘XXX’}
irb(main):009:0’ bab
irb(main):010:0’ aba
irb(main):011:0’ STRING_END
bXXXb
XXXbXXX
You can even do this (discovered just now in one of those, “surely this
wont work too, will it?” Ruby moments)!..
irb(main):015:0> puts <<BLOCK_ONE_END + <<BLOCK_TWO_END
irb(main):016:0" hello
irb(main):017:0" there
irb(main):018:0" BLOCK_ONE_END
irb(main):019:0" and how about
irb(main):020:0" some more?
irb(main):021:0" BLOCK_TWO_END
hello
there
and how about
some more?
Brilliant!
Anyone know when you do and don’t need to place ''s around the string
terminator?
Anyone know when you do and don’t need to place ''s around the string
terminator?
You don’t, really, but it is better to use ‘’, to have other
programmers not think you are actually appending a previously defined
constant (and prevent your program mis-behaving because of that).
You also do need “” if you expect #{} to work, thou.
Also, you may want to get into the habit of using:
<<-‘EOF’
EOF
The ‘-’ makes sure that Ruby will still read the block properly even if
someone else touches the code and makes the EOF not show up in the
first column.
Anyone know when you do and don’t need to place ''s around the string
terminator?
‘’ tells Ruby not to interpolate, while the default (without quotes) is
to do so. You can also use quotes (single or double) to use a more funky
delimiter. - lets you indent the ending delimiter.
Anyone know when you do and don’t need to place ''s around the string
terminator?
‘’ tells Ruby not to interpolate, while the default (without quotes) is
to do so. You can also use quotes (single or double) to use a more funky
delimiter. - lets you indent the ending delimiter.
The above is correct, unlike (I think) some other answers in this
thread. To be clear about it:
a = <<FOO
a#{1+1}
FOO
b = <<‘FOO’
b#{1+1}
FOO
c = <<“FOO”
c#{1+1}
FOO
d = <<-FOO
d#{1+1}
FOO
e = <<-‘FOO’
e#{1+1}
FOO
f = <<-“FOO”
f#{1+1}
FOO
puts a, b, c, d, e, f
#=> 2
#=> #{1+1}
#=> 2
#=> 2
#=> #{1+1}
#=> 2