LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-02-2011, 05:30 AM   #1
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Ruby: $_ not getting changed as expected


So it is probably simple and a question of scoping but it is bugging me.

Here is a simple example of my question:
Code:
$ echo "hello world" | ruby -pe '$_.split[1] = "help"'
hello world
Now to me the expected output should have been:
Code:
hello help
My thinking is that $_ is defined as follows:
Quote:
$_ The last input line of string by gets or readline.
So in our example this would mean it equals "hello world".

Now the split will use the default separator ($;) which is whitespace as this
value is set to nil. Therefore I would expect the second element of the array
created by split to be "world" which bears true and can be tested with:
Code:
$ echo "hello world" | ruby -pe 'puts $_.split[1]'
world
hello world
So my question is, if I am setting the second part of the array to "help"
why does this not show in the results?
 
Old 10-02-2011, 12:50 PM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
split does not manipulate $_

Hi grail,

I found some documentation here:
http://www.troubleshooters.com/codec...l.htm#_Strings

If you scroll down a bit you will find this for split:
Code:
mystring.split(pattern, limit)

Returns a new array with parts of the string split wherever pattern was encountered as a substring.
If limit is given, returns at most that many elements in the array.
So $_.split does not manipulate "$_" itself, i.e. it does not turn $_ into an array. Instead it returns a new array with the contents of $_ as its elements.
When you do
Code:
ruby -pe '$_.split[1] = "help"'
you get as a first step the array
Code:
[ "hello", "world" ]
in return. In a second step the element at position [1] gets the value "help" assigned. However, since there is no variable to store the returned array those changes are lost.
Consider the following:
Code:
$ echo "hello world"|ruby -ne '$F = $_.split;$F[1] = "help";puts "field [0]: "+$F[0];puts "field [1]: "+$F[1];puts $_'
field [0]: hello
field [1]: help
hello world
As you can see the array does get changed but $_ is never touched.

Last edited by crts; 10-02-2011 at 12:51 PM.
 
Old 10-02-2011, 07:32 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Original Poster
Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Thanks crts ... it was one of those things I looked at for ages and it was making my head hurt so I thought I would ask a question as no amount of searching or altering of the code was helping

I am still learning Ruby but pretty impressed so far

Thanks again.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Any issues installing Ruby Gems and Ruby on Rails in Slackware? Lufbery Slackware 8 02-09-2011 07:22 PM
Ruby SDL/ Ruby OpenGL tutorials/reference? LinuxNoob75 Programming 0 12-30-2007 12:47 PM
PHP v Ruby/Ruby on Rails Neko_D Linux - General 4 02-08-2007 11:56 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration