# Extracting lines from an address

**URL:** <https://www.ruby-forum.com/t/extracting-lines-from-an-address/100099>\
**Category:** Ruby\
**Created:** [November 17, 2005, 12:14pm UTC](https://www.ruby-forum.com/t/extracting-lines-from-an-address/100099 "2005-11-17T12:14:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![krisleech](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/59ef9b/32.png) [@krisleech](https://www.ruby-forum.com/u/krisleech)\
**Post date:** [November 17, 2005, 12:14pm UTC](https://www.ruby-forum.com/t/extracting-lines-from-an-address/100099/1 "2005-11-17T12:14:18Z")

</div>

I have an address stored in mysql as a text field.  
I’m using ActiveRecord to access the field.  
But how do I get each line of the address in to their own strings?

eg.

Address = “12, HouseName  
Street  
City  
Country”

–\>

Address\_line1 = “12, HouseName”  
Address\_line2 = “Street”  
Address\_line3 = “City”  
Address\_line4 = “Country”

Many thanks, K.

---

<div class="post-metadata">

**Author:** ![krisleech](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/59ef9b/32.png) [@krisleech](https://www.ruby-forum.com/u/krisleech)\
**Post date:** [November 17, 2005, 12:33pm UTC](https://www.ruby-forum.com/t/extracting-lines-from-an-address/100099/2 "2005-11-17T12:33:17Z")

</div>

there might be a cleaner way of doing it, but this will work:

# assign database entry to a variable

address = “12, HouseName  
Street  
City  
Country”

# make an array from the string, dividing each line by line breaks

lines = address.split(“\n”)

# each line is an element in the array, strip takes whitespace out

address\_line1 = lines[0].strip  
address\_line2 = lines[1].strip  
address\_line3 = lines[2].strip  
address\_line4 = lines[3].strip

“Kris L.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:removed\_email\_address@domain.invalid…

---

<div class="post-metadata">

**Author:** ![krisleech](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/59ef9b/32.png) [@krisleech](https://www.ruby-forum.com/u/krisleech)\
**Post date:** [November 17, 2005, 12:48pm UTC](https://www.ruby-forum.com/t/extracting-lines-from-an-address/100099/3 "2005-11-17T12:48:22Z")

</div>

sorry, i should be more careful with the definition of strip() in this  
comment:

> # strip takes whitespace out

more correcly, strip “Returns a copy of the string with leading and  
trailing  
whitespace removed.”

luke

---

<div class="post-metadata">

**Author:** ![krisleech](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/59ef9b/32.png) [@krisleech](https://www.ruby-forum.com/u/krisleech)\
**Post date:** [November 17, 2005, 12:54pm UTC](https://www.ruby-forum.com/t/extracting-lines-from-an-address/100099/4 "2005-11-17T12:54:24Z")

</div>

Kris L. wrote:

> –\>
> 
> Address\_line1 = “12, HouseName”  
> Address\_line2 = “Street”  
> Address\_line3 = “City”  
> Address\_line4 = “Country”

address = “12, HouseName  
Street  
City  
Country”

address\_lines = address.split( /\n\s\*/ )

puts address\_lines[0]

address\_line1, address\_line2, address\_line3, address\_line4 =  
address.split( /\n\s\*/ )

puts address\_line4

---

<div class="post-metadata">

**Author:** ![krisleech](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/59ef9b/32.png) [@krisleech](https://www.ruby-forum.com/u/krisleech)\
**Post date:** [November 17, 2005, 1:18pm UTC](https://www.ruby-forum.com/t/extracting-lines-from-an-address/100099/5 "2005-11-17T13:18:35Z")

</div>

> > I have an address stored in mysql as a text field.  
> > I’m using ActiveRecord to access the field.  
> > But how do I get each line of the address in to their own strings?

> address = "12, HouseName
> 
> puts address\_line4

This method has a couple of minor shortcomings - it does not eliminate  
all  
unnecessary whitespace (specifically, it does not look at whitespace at  
the beginning of the address field - if there were any, nor does it look  
at trailing whitespace).

In that case, may I suggest the following solution?  
(for readability, I have shortened Address\_line1-4 to al1-4…

```
address = " 12, HouseName
            Street
            City
            Country "

# btw. you should never assume you'll just receive the 4 lines
# you were expecting - this is why I added in the *remains which
# will receive the extra lines, if any.
al1,al2,al3,al4,*remains =address.split("\n").map { |l| l.strip }

puts "A : '#{address}'"
puts "A1: '#{al1}'"
puts "A2: '#{al2}'"	# yes - for this example, I'm not checking
puts "A3: '#{al3}'"	# whether I get LESS than the 4 lines I
puts "A4: '#{al4}'"	# wanted...

if remains != []
  puts "oops... there are more than just the expected 4 lines"
  remains.each do |r|
    puts " trailing line: #{r}"
  end
end

```

Running this will print:

```
A : ' 12, HouseName
            Street
            City
            Country '
A1: '12, HouseName'
A2: 'Street'
A3: 'City'
A4: 'Country'

```

Benedikt

ALLIANCE, n. In international politics, the union of two thieves who  
have their hands so deeply inserted in each other’s pockets that  
they cannot separately plunder a third.  
(Ambrose Bierce, The Devil’s Dictionary)

---

<div class="post-metadata">

**Author:** ![krisleech](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/59ef9b/32.png) [@krisleech](https://www.ruby-forum.com/u/krisleech)\
**Post date:** [November 17, 2005, 1:33pm UTC](https://www.ruby-forum.com/t/extracting-lines-from-an-address/100099/6 "2005-11-17T13:33:42Z")

</div>

Hi –

On Thu, 17 Nov 2005, Kris L. wrote:

> I have an address stored in mysql as a text field.  
> I’m using ActiveRecord to access the field.  
> But how do I get each line of the address in to their own strings?
> 
> eg.
> 
> Address = "12, HouseName

You probably want a local variable there, rather than a constant.

> ```
> Street
> City
> Country"
> 
> ```
> 
> –\>
> 
> Address\_line1 = “12, HouseName”  
> Address\_line2 = “Street”  
> Address\_line3 = “City”  
> Address\_line4 = “Country”

This is actually a case where the sometimes odd-seeming way that  
String#map works (as well as #each and #to\_a) can work in your favor:

house,street,city,country = address.map {|line| line.strip }

David

---

<div class="post-metadata">

**Author:** ![krisleech](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/59ef9b/32.png) [@krisleech](https://www.ruby-forum.com/u/krisleech)\
**Post date:** [November 17, 2005, 1:39pm UTC](https://www.ruby-forum.com/t/extracting-lines-from-an-address/100099/7 "2005-11-17T13:39:44Z")

</div>

Kris L. wrote:

> –\>
> 
> Address\_line1 = “12, HouseName”  
> Address\_line2 = “Street”  
> Address\_line3 = “City”  
> Address\_line4 = “Country”

> > adr = “12, HouseName  
> > Street  
> > City  
> > Country”  
> > =\> “12, HouseName\n Street\n City\n  
> > Country”
> 
> > adr.to\_a.each {|s| s.strip! }  
> > =\> [“12, HouseName”, “Street”, “City”, “Country”]

Kind regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![krisleech](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/59ef9b/32.png) [@krisleech](https://www.ruby-forum.com/u/krisleech)\
**Post date:** [November 17, 2005, 11:02pm UTC](https://www.ruby-forum.com/t/extracting-lines-from-an-address/100099/8 "2005-11-17T23:02:57Z")

</div>

Benedikt H. wrote:

> > > Address\_line2 = “Street”  
> > > puts address\_lines[0]  
> > > at trailing whitespace).
> 
> # btw. you should never assume you’ll just receive the 4 lines
> 
> # you were expecting - this is why I added in the \*remains which
> 
> # will receive the extra lines, if any.
> 
> al1,al2,al3,al4,\*remains =address.split("\n").map { |l| l.strip }

I can’t help but commend your work. However, it has the minor  
shortcoming that the split is not needed:

al1,al2,al3,al4,\*remains =address.map { |l| l.strip }

---

<div class="post-metadata">

**Author:** ![krisleech](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/59ef9b/32.png) [@krisleech](https://www.ruby-forum.com/u/krisleech)\
**Post date:** [November 17, 2005, 4:56pm UTC](https://www.ruby-forum.com/t/extracting-lines-from-an-address/100099/9 "2005-11-17T16:56:09Z")

</div>

Thanks for the replies, very useful.  
As always there appears to many ways of doing the same thing in ruby 🙂

Cheers K.
