Confusion with Ruby command line options

Can any one help me to understand when and how to use the below two Ruby
command line options:

-0[octal] and -a

Thanks

Am 13.02.2013 22:37, schrieb Love U Ruby:

Can any one help me to understand when and how to use the below two Ruby
command line options:

-0[octal] and -a

Please STOP asking us to rewrite the documentation just for you!

If you cannot make any sense out of the existing documentation
then maybe that’s a sign that you are trying to understand concepts
for which you are not ready yet.

Judging from how often you post questions like “Please explain
the following 10 methods to me” you obviously try to learn the
language by looking at each and every method from a to z.
That’s not a good method!

Get an introductory book / tutorial and start with the basics.

PLEASE.

From the
doc

-0digit =>

specifies the input record separator ($/) as an octal number. If no
digits given, the null character is the separator. Other switches may
follow the digits. -00 turns Ruby into paragraph mode. -0777 makes Ruby
read whole file at once as a single string, since there is no legal
character with that value.

My bad! Couldn’t digest it at all. So let’s started to play with it.

C:\>ruby -0777 -e 'a= gets; puts a '
Hi, This is Ram.
Could you help me here?
^Z #~~~> pressed ENTER here after CTRL+Z and got the output as

below-
Hi, This is Ram.
Could you help me here?
C:>ruby #~~~> return the prompt

C:\>ruby -000 -e 'a= gets; puts a '
Hi, This is Ram.
Could you help me here?
^Z #~~~> pressed ENTER here after CTRL+Z and got the output as

below-
Hi, This is Ram.
Could you help me here?
C:>ruby #~~~> return the prompt

C:\>ruby -00 -e 'a= gets; puts a '
Hi, This is Ram.
Could you help me here? #~~~> pressed ENTER
#~~~> pressed ENTER here and got the output as below-
Hi, This is Ram.
Could you help me here?
C:\>ruby #~~~> return the prompt

Can I set such octal or $/ to as below ?

carraige return (\r)
tab (\t)

If so, Can I have one example for each to see their behaviours?

I also tried to print the value of "$/" and nothing printed. So how
should I see that?

C:\>ruby -00 -e 'a= gets; puts a ;puts "here is: #{$/}"'
hi

hi

here is:


C:\>ruby -0777 -e 'a= gets; puts a ;puts "here is: #{$/}"'
Hey! Arjun
Are you going school?

^Z
Hey! Arjun
Are you going school?

here is:

C:\>

Hans M. wrote in post #1096880:

its because #{$/} uses to_s but you need the inspected stuff
#{$/.inspect}

ruby -00 -e ‘a= gets; puts a ;puts “here is: #{$/.inspect}”’
R
R
here is: “\n\n”

ruby -0777 -e ‘a= gets; puts a ;puts “here is: #{$/.inspect}”’
E
E
here is: nil

ruby -e ‘a= gets; puts a ;puts “here is: #{$/.inspect}”’
Z
Z
here is: “\n”

Thank you very much! @Hans

Can I set such octal or $/ to as below ?

carraige return (\r)
tab (\t)

Thank you very much! @Hans

Can I set such octal or $/ to as below ?

carraige return (\r)
tab (\t)

Come up with a solution as below :

C:>ruby -00 -e ‘a= gets; puts a ;puts “here is: #{$/.inspect}”’
Hi

Hi

here is: “\n\n”

C:>ruby -015 -e ‘a= gets; puts a ;puts “here is: #{$/.inspect}”’
hi
hi
^Z
hi
hi
here is: “\r”

C:>ruby -011 -e ‘a= gets; puts a ;puts “here is: #{$/.inspect}”’
Hello

^Z
Hello

here is: “\t”

C:>

Query: Couldn’t understand how the “\t” and “\r” effecting the CODE
output. Need help here.

its because #{$/} uses to_s but you need the inspected stuff
#{$/.inspect}

ruby -00 -e ‘a= gets; puts a ;puts “here is: #{$/.inspect}”’
R
R
here is: “\n\n”

ruby -0777 -e ‘a= gets; puts a ;puts “here is: #{$/.inspect}”’
E
E
here is: nil

ruby -e ‘a= gets; puts a ;puts “here is: #{$/.inspect}”’
Z
Z
here is: “\n”

from the doc:

-a autosplit mode with -n or -p (splits $_ into $F)

@ubuntu:~$ ruby -ane ‘puts $;puts $.split;puts $F[4]’ # I used
explicitly

hi i am a good boy
hi i am a good boy
hi
i
am
a
good
boy
good

@ubuntu:~$ ruby -ne 'puts $;puts $.split; $F = $.split;
puts $F[4]’ # I used -a not explicitly,rather implicitly by
# $F = $
.split
aa ss ff fgz fkf kl
aa ss ff fgz fkf kl
aa
ss
ff
fgz
fkf
kl
fkf

From the above link :- http://www.december.com/html/spec/ascii.html

I got the octal code of many more escape characters and also played with
them with Ruby command line -0 option:

Here is the full list of example… read and digest it. :slight_smile:

@ubuntu:~$ ruby -010 -e ‘a= gets; puts a ;puts “here is: #{$/.inspect}”’
This is a spoon. Do you need it.^H
This is a spoon. Do you need it.
here is: “\b”

@ubuntu:~$ ruby -033 -e ‘a= gets; puts a ;puts “here is: #{$/.inspect}”’
Hi…How are you?
Are you going ^[ school today?
Hi…How are you?
Are you going
here is: “\e”

@ubuntu:~$ ruby -011 -e ‘a= gets; puts a ;puts “here is: #{$/.inspect}”’
Today is very hot outside the room
Today is very hot outside
here is: “\t”

@ubuntu:~$ ruby -015 -e ‘a= gets; puts a ;puts “here is: #{$/.inspect}”’
Are you mad?hey…^Mhello…I am telling you dude…
Are you mad?hey…
here is: “\r”

@ubuntu:~$ ruby -012 -e ‘a= gets; puts a ;puts “here is: #{$/.inspect}”’
Are you there?
Are you there?
here is: “\n”

@ubuntu:~$ ruby -040 -e ‘a= gets; puts a ;puts “here is: #{$/.inspect}”’
Iammmmmmmmmm a
Iammmmmmmmmm
here is: " "

@ubuntu:~$ ruby -014 -e ‘a= gets; puts a ;puts “here is: #{$/.inspect}”’
Earth is an planet.Remember it…^L dear brother
Earth is an planet.Remember it…

here is: “\f”

@ubuntu:~$ ruby -000 -e ‘a= gets; puts a ;puts “here is: #{$/.inspect}”’
Hiiiii…

Hiiiii…

here is: “\n\n”
@ubuntu:~$

Feel free to ask me any doubt… love to share my knowledge…
although it is limited :slight_smile: :slight_smile:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs