# Ruby one liner killed my script

**URL:** <https://www.ruby-forum.com/t/ruby-one-liner-killed-my-script/128836>\
**Category:** Ruby\
**Created:** [February 12, 2008, 1:06am UTC](https://www.ruby-forum.com/t/ruby-one-liner-killed-my-script/128836 "2008-02-12T01:06:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [February 12, 2008, 1:06am UTC](https://www.ruby-forum.com/t/ruby-one-liner-killed-my-script/128836/1 "2008-02-12T01:06:35Z")

</div>

Talk about redundancy!

I spent some time with irb with the aim of getting an understanding of  
methods and class building. I originally wanted to convert a binary  
number to denary, with input from the user.

# strike one!

ruby -e ‘p 0b10010101111’  
does it on one line.

Anyway, I still looked at the few lines that I had and I’m wondering  
how I can convert a user’s input into a fixnum?

class B2d  
def initialize  
@ustring = " "  
end

def get\_bin\_string  
p 'binary numbers : ’  
@ustring = gets.chomp  
end

def test\_bin\_string  
if @ustring =~ /[a-z]|[2-9]|[A-Z]| /  
p “binary digits only and no spaces, please!”  
exit 1  
end  
end

def put\_bin\_string

# this is the line in which I originally tried to convert the user’s

binary digit to denary puts “your bin #{@ustring.to\_i} =  
0b#{@ustring.to\_i}” end  
end

t = B2d.new  
t.get\_bin\_string  
t.test\_bin\_string  
t.put\_bin\_string

–

Regards,

John M.

–

Regards,

John M.  
MSc (DIC)  
+44 7739 171 531

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [February 12, 2008, 1:50am UTC](https://www.ruby-forum.com/t/ruby-one-liner-killed-my-script/128836/2 "2008-02-12T01:50:32Z")

</div>

John M. wrote:

> I’m wondering  
> how I can convert a user’s input into a fixnum?
> 
> def put\_bin\_string
> 
> # this is the line in which I originally tried to convert
> 
> # the user’s binary digit to denary:
> 
> puts “your bin #{@ustring.to\_i} =  
> 0b#{@ustring.to\_i}” end  
> end

Does this help:

input = “10000001”  
puts input.to\_i(base=2)

–output:–  
129

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [February 12, 2008, 2:16am UTC](https://www.ruby-forum.com/t/ruby-one-liner-killed-my-script/128836/3 "2008-02-12T02:16:18Z")

</div>

Where can I find out more about that method within ri?  
ri Object?

On Tue, 12 Feb 2008 09:50:25 +0900  
7stud – [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > end
> 
> Does this help:
> 
> input = “10000001”  
> puts input.to\_i(base=2)
> 
> –output:–  
> 129

–

Regards,

John M.  
MSc (DIC)  
+44 7739 171 531

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [February 12, 2008, 2:54am UTC](https://www.ruby-forum.com/t/ruby-one-liner-killed-my-script/128836/4 "2008-02-12T02:54:59Z")

</div>

From: John M. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# Where can I find out more about that method within ri?

# ri Object?

judging fr stud’s reply, maybe you can try the string object first, eg,

botp@pc4all:~$ qri to\_i  
------------------------------------------------------ Multiple choices:

```
 Class#to_i, Float#to_i, IO#to_i, IPAddr#to_i, Integer#to_i,
 NilClass#to_i, Process::Status#to_i, Rational#to_i, String#to_i,
 Symbol#to_i, Time#to_i

```

## botp@pc4all:~$ qri string.to\_i ------------------------------------------------------------ String#to\_i str.to\_i(base=10) =\> integer

```
 Returns the result of interpreting leading characters in str as an
 integer base base (2, 8, 10, or 16). Extraneous characters past
 the end of a valid number are ignored. If there is not a valid
 number at the start of str, 0 is returned. This method never
 raises an exception.

    "12345".to_i #=> 12345
    "99 red balloons".to_i #=> 99
    "0a".to_i #=> 0
    "0a".to_i(16) #=> 10
    "hello".to_i #=> 0
    "1100101".to_i(2) #=> 101
    "1100101".to_i(8) #=> 294977
    "1100101".to_i(10) #=> 1100101
    "1100101".to_i(16) #=> 17826049

```

botp@pc4all:~$

kind regards -botp
