# A complete beginners question

Hi there,

Sorry this is probably going to be a real easy question but I am totally
new to programming.

I was following some tutorial online and at the end of the first chapter
it said 'Now create a program that asks the user for a number and then
suggests a higher number as a better option.

So what I wanted to happen below is it takes the users input and just
adds 1 to it. But is is coming back with "rb:3:in '+' can't convert
fixnum into string'

puts 'What is your favourite number?'
number = gets.chomp
number+=1
puts 'Do you not think' + number + 'is a better number?'

Any help would be appreciated its doing my head in.

Thanks

···

--
Posted via http://www.ruby-forum.com/.

Hi there,
puts 'What is your favourite number?'
number = gets.chomp
number+=1
puts 'Do you not think' + number + 'is a better number?'

puts 'Do you not think' + number.to_s + 'is a better number?'

···

On Sat, May 29, 2010 at 03:29:15AM +0900, Ant Walliams wrote:

Any help would be appreciated its doing my head in.

When you call gets.chomp it returns a string (lets say it's "5"). That looks like a number to you and me but ruby treats it as text. You need to tell ruby to make it a number using .to_i.
ie The second line should read gets.chomp.to_i

Then ruby will be adding 5+1 and not "5"+1. I found that a good way to think of this was to replace "5" with a word or something. Could you do "hello"+1?

Angus

···

On 28/05/10 19:29, Ant Walliams wrote:

Hi there,

Sorry this is probably going to be a real easy question but I am totally
new to programming.

I was following some tutorial online and at the end of the first chapter
it said 'Now create a program that asks the user for a number and then
suggests a higher number as a better option.

So what I wanted to happen below is it takes the users input and just
adds 1 to it. But is is coming back with "rb:3:in '+' can't convert
fixnum into string'

puts 'What is your favourite number?'
number = gets.chomp
number+=1
puts 'Do you not think' + number + 'is a better number?'

Any help would be appreciated its doing my head in.

Thanks

puts 'What is your favourite number?'
number = gets.chomp
number = number.to_i # converts the number from String to Integer
number+=1
puts 'Do you not think' + number + 'is a better number?'

···

On Sat, 2010-05-29 at 03:29 +0900, Ant Walliams wrote:

Hi there,

Sorry this is probably going to be a real easy question but I am totally
new to programming.

I was following some tutorial online and at the end of the first chapter
it said 'Now create a program that asks the user for a number and then
suggests a higher number as a better option.

So what I wanted to happen below is it takes the users input and just
adds 1 to it. But is is coming back with "rb:3:in '+' can't convert
fixnum into string'

puts 'What is your favourite number?'
number = gets.chomp
number+=1
puts 'Do you not think' + number + 'is a better number?'

Any help would be appreciated its doing my head in.

Thanks

Regards
A.K.Karthikeyan,
Prop. Mind As Lab
http://mindaslab.in

Reid Thompson wrote:

···

On Sat, May 29, 2010 at 03:29:15AM +0900, Ant Walliams wrote:

Hi there,
puts 'What is your favourite number?'
number = gets.chomp
number+=1
puts 'Do you not think' + number + 'is a better number?'

puts 'Do you not think' + number.to_s + 'is a better number?'

Thank for the reply but I am still getting the same error even after

Thanks
Ant
--
Posted via http://www.ruby-forum.com/.

Hi, i am in Osx and i had installed Ruby 1.8.7. I want to make a
tutorial, i am a beginner in ruby. My problem is that when i want to
execute a program on ruby, example: code.rb, with "ruby code.rb" on
Terminal, like all tutorials sais, a error come back: "-bash: code.rb:

···

--
Posted via http://www.ruby-forum.com/.

unsubscribe

the problem is with number+=1

number is a string because gets.chomp is a string

number+=1 is syntactic sugar for
number = number + 1

And the expression number + 1 is trying to add 1 (a Fixnum) to a string

Try changing:

number = gets.chomp.to_i

to convert the string into an integer (which if small enough will be held in an instance of Fixnum).

Then, you'll have the opposite problem with the last line unless you incorporate Reid's suggestion.

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

···

On May 28, 2010, at 2:59 PM, Ant Walliams wrote:

Reid Thompson wrote:

On Sat, May 29, 2010 at 03:29:15AM +0900, Ant Walliams wrote:

Hi there,
puts 'What is your favourite number?'
number = gets.chomp
number+=1
puts 'Do you not think' + number + 'is a better number?'

puts 'Do you not think' + number.to_s + 'is a better number?'

Thank for the reply but I am still getting the same error even after

Thanks
Ant

You need to convert the input from String to Fixnum

puts "What is your favourite number?"
number = gets.chomp.to_i
number+=1
puts "Do you not think #{number} is a better number?"

···

On Fri, May 28, 2010 at 8:59 PM, Ant Walliams <anthonywainwright@googlemail.com> wrote:

Reid Thompson wrote:

On Sat, May 29, 2010 at 03:29:15AM +0900, Ant Walliams wrote:

Hi there,
puts 'What is your favourite number?'
number = gets.chomp
number+=1
puts 'Do you not think' + number + 'is a better number?'

puts 'Do you not think' + number.to_s + 'is a better number?'

Thank for the reply but I am still getting the same error even after

Thanks
Ant
--
Posted via http://www.ruby-forum.com/.

--
Jens-Harald Johansen
--
There are 10 kinds of people in the world: Those who understand binary and
those who don't...

It's ok, you want to run it as a normal command if i understand well. First thing to do, is adding a "shebang"[1] at top of your ruby file. After that, you have to make it executable, and, to do it you have to write on bash command line, chmod +x code.rb and voilà, your ruby script as executable in this way: ./code.rb

Have fun with ruby,
-Francesco

[1] shebang: "/usr/bin/env ruby"

···

Il 29/05/10 21.43, Emma Pidre ha scritto:

Hi, i am in Osx and i had installed Ruby 1.8.7. I want to make a
tutorial, i am a beginner in ruby. My problem is that when i want to
execute a program on ruby, example: code.rb, with "ruby code.rb" on
Terminal, like all tutorials sais, a error come back: "-bash: code.rb:

# add a shebang to the top of a file called hello_world.rb
# this directs the system(?) to use Ruby to interpret the file
\$ echo '#!/usr/bin/env ruby' > hello_world.rb

# append a statement that will output "hello world" to hello_world.rb
\$ echo 'puts "hello world"' >> hello_world.rb

# set permissions of hello_world.rb so that you can execute it
\$ chmod 755 hello_world.rb

# execute hello_world.rb and see that it outputs "hello world"
# the ./ in front is important, it says the file is located in the current
directory
\$ ./hello_world.rb
hello world

···

On Sat, May 29, 2010 at 2:43 PM, Emma Pidre <equisigriegazeta@gmail.com>wrote:

Hi, i am in Osx and i had installed Ruby 1.8.7. I want to make a
tutorial, i am a beginner in ruby. My problem is that when i want to
execute a program on ruby, example: code.rb, with "ruby code.rb" on
Terminal, like all tutorials sais, a error come back: "-bash: code.rb:

--
Posted via http://www.ruby-forum.com/.

The final solution is look like this:

puts 'What is your favourite number?'
number = gets.chomp.to_i
number +=1
puts 'Do you not think #{number} is a better number?'

Rob Biedenharn wrote:

···

On May 28, 2010, at 2:59 PM, Ant Walliams wrote:

Thanks
Ant

the problem is with number+=1

number is a string because gets.chomp is a string

number+=1 is syntactic sugar for
number = number + 1

And the expression number + 1 is trying to add 1 (a Fixnum) to a string

Try changing:

number = gets.chomp.to_i

to convert the string into an integer (which if small enough will be
held in an instance of Fixnum).

Then, you'll have the opposite problem with the last line unless you
incorporate Reid's suggestion.

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

Thank you very much, that worked. And thanks for the explanation of how
it works as well, that really helps.

Thanks
Ant
--
Posted via http://www.ruby-forum.com/.

Hi, just do this:

puts "What is your favourite number?"
number = gets.chomp
number+=1
puts "Do you not think #{number.to_s} is a better number?"

Best regards.

···

On Fri, May 28, 2010 at 12:13 PM, J-H Johansen <ondemannen@gmail.com> wrote:

On Fri, May 28, 2010 at 8:59 PM, Ant Walliams > <anthonywainwright@googlemail.com> wrote:
> Reid Thompson wrote:
>> On Sat, May 29, 2010 at 03:29:15AM +0900, Ant Walliams wrote:
>>> Hi there,
>>> puts 'What is your favourite number?'
>>> number = gets.chomp
>>> number+=1
>>> puts 'Do you not think' + number + 'is a better number?'
>> puts 'Do you not think' + number.to_s + 'is a better number?'
>
> Thank for the reply but I am still getting the same error even after
> making your changes. Any ideas?
>
> Thanks
> Ant
> --
> Posted via http://www.ruby-forum.com/.
>
>

You need to convert the input from String to Fixnum

puts "What is your favourite number?"
number = gets.chomp.to_i
number+=1
puts "Do you not think #{number} is a better number?"

--
Jens-Harald Johansen
--
There are 10 kinds of people in the world: Those who understand binary and
those who don't...

--
Jaime Gonzalez.

You're right, maybe my answer can get someone confused, to be 100% sure, Josh, let me write a ruby script

ruby_sample.rb:

···

Il 30/05/10 13.20, Josh Cheek ha scritto:

On Sat, May 29, 2010 at 2:43 PM, Emma Pidre<equisigriegazeta@gmail.com>wrote:

Hi, i am in Osx and i had installed Ruby 1.8.7. I want to make a
tutorial, i am a beginner in ruby. My problem is that when i want to
execute a program on ruby, example: code.rb, with "ruby code.rb" on
Terminal, like all tutorials sais, a error come back: "-bash: code.rb:

--
Posted via http://www.ruby-forum.com/.

# add a shebang to the top of a file called hello_world.rb
# this directs the system(?) to use Ruby to interpret the file
\$ echo '#!/usr/bin/env ruby'> hello_world.rb

# append a statement that will output "hello world" to hello_world.rb
\$ echo 'puts "hello world"'>> hello_world.rb

# set permissions of hello_world.rb so that you can execute it
\$ chmod 755 hello_world.rb

# execute hello_world.rb and see that it outputs "hello world"
# the ./ in front is important, it says the file is located in the current
directory
\$ ./hello_world.rb
hello world

--
#!/usr/bin/env ruby

puts "hello world"
--

and after you've to do what Josh say i mean, 3rd and 4th step.

-Francesco

You don't need chomp.

"1\n".to_i # => 1
"1".to_i # => 1

···

On Mon, May 31, 2010 at 10:53 PM, Guten <ywzhaifei@gmail.com> wrote:

The final solution is look like this:

puts 'What is your favourite number?'
number = gets.chomp.to_i
number +=1
puts 'Do you not think #{number} is a better number?'

the first line of code in the example i trying to run : "#!/usr/bin/env
ruby".

The error is the same:
"ruby: No such file or directory -- ./code.rb (LoadError)"

The person who sold me the Macbook, said to me that he made some
troubles with ruby. I don't know what is wrong, maybe is a configuration
troubles or something like that. i can use directly from textMate, but i
want to resolve the problem form Terminal, to be more sure that all work
fine.

there is a way for restart ruby, i mean, unistall o delete all files or
something like that for make a re-install later ? What did you advise
me?

Thanks for all ! im very happy to find i place where find help.

···

--
Posted via http://www.ruby-forum.com/.

Please follow the instructions above, exactly, and let us know whether the
hello world example works. (ie if it did not work, it should not have said

···

On Sun, May 30, 2010 at 12:01 PM, Emma Pidre <equisigriegazeta@gmail.com>wrote:

the first line of code in the example i trying to run : "#!/usr/bin/env
ruby".

The error is the same:
"ruby: No such file or directory -- ./code.rb (LoadError)"

The person who sold me the Macbook, said to me that he made some
troubles with ruby. I don't know what is wrong, maybe is a configuration
troubles or something like that. i can use directly from textMate, but i
want to resolve the problem form Terminal, to be more sure that all work
fine.

there is a way for restart ruby, i mean, unistall o delete all files or
something like that for make a re-install later ? What did you advise
me?

Thanks for all ! im very happy to find i place where find help.
--
Posted via http://www.ruby-forum.com/.

If you are having difficulty following it, this is what it looks like on my
cmputer:
http://img404.imageshack.us/i/screenshot20100530at254.png/

···

On Sun, May 30, 2010 at 2:53 PM, Josh Cheek <josh.cheek@gmail.com> wrote:

On Sun, May 30, 2010 at 12:01 PM, Emma Pidre <equisigriegazeta@gmail.com>wrote:

the first line of code in the example i trying to run : "#!/usr/bin/env
ruby".

The error is the same:
"ruby: No such file or directory -- ./code.rb (LoadError)"

The person who sold me the Macbook, said to me that he made some
troubles with ruby. I don't know what is wrong, maybe is a configuration
troubles or something like that. i can use directly from textMate, but i
want to resolve the problem form Terminal, to be more sure that all work
fine.

there is a way for restart ruby, i mean, unistall o delete all files or
something like that for make a re-install later ? What did you advise
me?

Thanks for all ! im very happy to find i place where find help.
--
Posted via http://www.ruby-forum.com/.

Please follow the instructions above, exactly, and let us know whether the
hello world example works. (ie if it did not work, it should not have said