# Nuby question - printing substring as non-ASCII

**URL:** <https://www.ruby-forum.com/t/nuby-question-printing-substring-as-non-ascii/76314>\
**Category:** Ruby\
**Created:** [November 17, 2006, 3:34pm UTC](https://www.ruby-forum.com/t/nuby-question-printing-substring-as-non-ascii/76314 "2006-11-17T15:34:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![tlockney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5e9695/32.png) [@tlockney](https://www.ruby-forum.com/u/tlockney)\
**Post date:** [November 17, 2006, 3:34pm UTC](https://www.ruby-forum.com/t/nuby-question-printing-substring-as-non-ascii/76314/1 "2006-11-17T15:34:54Z")

</div>

I’m reading in a string of numbers, as follows:

" 1 45.3456 "

How do I write out the first non-whitespace element as a number? Ruby  
wants to express as it in ASCII, and the .to\_i method doesn’t seem to  
work on a substring.

Thanks in advance.

TPL

---

<div class="post-metadata">

**Author:** ![tlockney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5e9695/32.png) [@tlockney](https://www.ruby-forum.com/u/tlockney)\
**Post date:** [November 17, 2006, 3:42pm UTC](https://www.ruby-forum.com/t/nuby-question-printing-substring-as-non-ascii/76314/2 "2006-11-17T15:42:17Z")

</div>

Thomas L. wrote:

> I’m reading in a string of numbers, as follows:
> 
> " 1 45.3456 "
> 
> How do I write out the first non-whitespace element as a number? Ruby  
> wants to express as it in ASCII, and the .to\_i method doesn’t seem to  
> work on a substring.

Doesn’t seem to? Code please.

Also, the following works for me:

irb(main):009:0\> " 1 45.3456 ".strip.split(/\s+/).map { |i|  
i.to\_i  
}  
=\> [1, 45]

(Of course, to\_i coerces to integers, so the body of the map block would  
have to be more complicated to get floats too.)

David V.

---

<div class="post-metadata">

**Author:** ![tlockney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5e9695/32.png) [@tlockney](https://www.ruby-forum.com/u/tlockney)\
**Post date:** [November 17, 2006, 3:46pm UTC](https://www.ruby-forum.com/t/nuby-question-printing-substring-as-non-ascii/76314/3 "2006-11-17T15:46:17Z")

</div>

Thomas L. wrote:

> TPL
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

irb(main):001:0\> " 1 45.3456 ".strip.to\_i()  
=\> 1

---

<div class="post-metadata">

**Author:** ![tlockney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5e9695/32.png) [@tlockney](https://www.ruby-forum.com/u/tlockney)\
**Post date:** [November 17, 2006, 3:56pm UTC](https://www.ruby-forum.com/t/nuby-question-printing-substring-as-non-ascii/76314/4 "2006-11-17T15:56:19Z")

</div>

Dmitry B. wrote:

> Then why not call to\_i on a whole string?
> 
> " 1 45.3456 ".to\_i  
> " 1 45.3456 ".to\_f
> 
> It will find the first number and convert it to Fixnum or Float.
> 
> Dmitry  
> You’re absolutely right, Dmitry… .strip() is not necessary. But I  
> tend to be a bit anal-retentive about such things :o|

---

<div class="post-metadata">

**Author:** ![tlockney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5e9695/32.png) [@tlockney](https://www.ruby-forum.com/u/tlockney)\
**Post date:** [November 17, 2006, 3:46pm UTC](https://www.ruby-forum.com/t/nuby-question-printing-substring-as-non-ascii/76314/5 "2006-11-17T15:46:18Z")

</div>

Then why not call to\_i on a whole string?

" 1 45.3456 ".to\_i  
" 1 45.3456 ".to\_f

It will find the first number and convert it to Fixnum or Float.

Dmitry

---

<div class="post-metadata">

**Author:** ![tlockney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5e9695/32.png) [@tlockney](https://www.ruby-forum.com/u/tlockney)\
**Post date:** [November 17, 2006, 4:27pm UTC](https://www.ruby-forum.com/t/nuby-question-printing-substring-as-non-ascii/76314/6 "2006-11-17T16:27:46Z")

</div>

Thomas L. wrote:

> I’m reading in a string of numbers, as follows:
> 
> " 1 45.3456 "
> 
> How do I write out the first non-whitespace element as a number? Ruby  
> wants to express as it in ASCII, and the .to\_i method doesn’t seem to  
> work on a substring.
> 
> Thanks in advance.
> 
> TPL

Thanks. Dumb question, but I’m kinda deep in the throes of the “suck”  
stage of learning Ruby. The following also seems to work (at least for  
the purposes of reading in and writing formatted output):

irb(main):001:0\> string=" 1 45.3456 "  
=\> " 1 45.3456 "

irb(main):003:0\> string.strip  
=\> “1 45.3456”

irb(main):004:0\> string.split[1]  
=\> “45.3456”

irb(main):005:0\> string.split[0]  
=\> “1”

---

<div class="post-metadata">

**Author:** ![tlockney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5e9695/32.png) [@tlockney](https://www.ruby-forum.com/u/tlockney)\
**Post date:** [November 17, 2006, 6:02pm UTC](https://www.ruby-forum.com/t/nuby-question-printing-substring-as-non-ascii/76314/7 "2006-11-17T18:02:44Z")

</div>

Hi –

On Sat, 18 Nov 2006, Wilson B. wrote:

> > > Thanks in advance.
> 
> Here is one way:
> 
> substrings.first is the same as substrings[0]

Another possibility:

irb(main):001:0\> require ‘scanf’  
=\> true  
irb(main):002:0\> string = " 1 45.3456 "  
=\> " 1 45.3456 "  
irb(main):003:0\> string.scanf("%d")  
=\> [1]

You have to get it out of the array, but you don’t have to convert it  
(it’s already an integer).

David

---

<div class="post-metadata">

**Author:** ![tlockney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5e9695/32.png) [@tlockney](https://www.ruby-forum.com/u/tlockney)\
**Post date:** [November 18, 2006, 6:25pm UTC](https://www.ruby-forum.com/t/nuby-question-printing-substring-as-non-ascii/76314/8 "2006-11-18T18:25:03Z")

</div>

----- Original Message -----  
From: “Patrick S.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: “ruby-talk ML” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Friday, November 17, 2006 9:45 AM  
Subject: Re: Nuby question - printing substring as non-ASCII

> > Thanks in advance.
> > 
> > TPL
> > 
> > –  
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> irb(main):001:0\> " 1 45.3456 ".strip.to\_i()  
> =\> 1

no need to strip  
" 1 45.3456".to\_i  
=\> 1

---

<div class="post-metadata">

**Author:** ![tlockney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5e9695/32.png) [@tlockney](https://www.ruby-forum.com/u/tlockney)\
**Post date:** [November 17, 2006, 5:16pm UTC](https://www.ruby-forum.com/t/nuby-question-printing-substring-as-non-ascii/76314/9 "2006-11-17T17:16:38Z")

</div>

On 11/17/06, Thomas L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> irb(main):003:0\> string.strip  
> =\> “1 45.3456”
> 
> irb(main):004:0\> string.split[1]  
> =\> “45.3456”
> 
> irb(main):005:0\> string.split[0]  
> =\> “1”

Here is one way:  
irb(main):001:0\> string = " 1 45.3456 "  
=\> " 1 45.3456 "  
irb(main):002:0\> substrings = string.scan /[^\s]+/  
=\> [“1”, “45.3456”]  
irb(main):003:0\> substrings.first.to\_i  
=\> 1

The second line of code is saying: Scan the string, and return an  
Array of strings that were made up of one or more non-whitespace  
characters.

substrings.first is the same as substrings[0]
