Topic: My questions as I (try to) learn ruby


I am, as the title states, trying to learn ruby; and as with HTML, I tend to learn by asking questions.

I'm using: Beginning Ruby From Novice to Professional

and my first question would have to be with an error I am getting.

class Dog
    attr_accessor :color, :age

a_dog =

puts "How old is the dog?"

a_dog.age = gets()

puts "WOW, it's just a baby!" if a_dog.age < 5

the error has to be with the comparison of a string with 5 using <.
Now I tried then creating a variable age and assigning it equal to a_dog.age and then doing if age < 5 but I get the same error

thanks for willing to help out, and sorry for all the basic questions I'll have

Re: My questions as I (try to) learn ruby


You can convert a string to an integer by doing:

a_dog.age = gets.to_i


Re: My questions as I (try to) learn ruby

oh right! Thanks Kevin, I should have noticed

puts a_dog.age.class

returns string, not a number. thanks again, and I'll post back when I hit a bump sooner or later.

Re: My questions as I (try to) learn ruby

If you are just starting out then Peter Cooper's Beginning Ruby would probably be an awesome book for you.

Re: My questions as I (try to) learn ruby

Hey Cherring, that's the book I've decided to use smile

My second question would be with something I tried to create without re-reading the chapter and working through the bugs myself. A little of class creation, defining, gsub, and gets().

Here's what I typed out

class Shop
  attr_accessor :product, :amount
  def ship
    print "SHIPPED!"

a_order =

puts "So, what'd you wana order today?"
a_order.product = gets.to_s.chomp.capitalize

puts "So, you want to order the #{a_order.product}, how many?"
a_order.amount = gets.to_s.chomp

puts "Okay so you want #{a_order.amount} #{a_order.product}s right?"

puts "Or did you say you wanted #{a_order.amount.gsub(a_order.amount , '100')} of them?"

puts "Would you like for us to ship today?"
ship_confirmation = gets.to_s.chomp.downcase

print "Okay, #{a_order.ship if ship_confirmation == "yes"}"

Now the question I have is with the very last line, the call to ship.

Firstly, if I leave it as:

puts a_order.ship if ship_confirmation == "yes"

I get: SHIPPED !nil

however, when I use the line I posted firstly

print "Okay, #{a_order.ship if ship_confirmation == "yes"}"

it reads as following: SHIPPED!Okay,

when It should read Okay, SHIPPED, so why is that?

Lastly, I posted the entire code up there to just see if you guys can spot errors, or point out better ways to do something before I move on.

thanks again!

Re: My questions as I (try to) learn ruby


The function ship needs to return a string (not call print) if you use it in this form.

Remember "print" is just a function. As with all functions, parameters (in this case the string interpolation) must be evaluated first. Thus 'a_order.ship' it will print first, and since it returns nil, the next print will be what follows in your output.

As for feedback, I see no reason to prefix all your variables with the indefinite article (the, a, an).


Re: My questions as I (try to) learn ruby

Hey Kevin, thanks again.


  def ship

be valid? or do you need to store a string in a variable?

Also what naming style would you suggest? would "order" be fine alone, or "order_instance" or anything else you'd suggest would be appreciated

Re: My questions as I (try to) learn ruby

Yes, that is fine. Just 'order'. Also, I noticed you named your class 'Shop', but it doesn't appear to be a shop at all. This might be something to change also.


Re: My questions as I (try to) learn ruby

haha ya Kevin, it's not a shop really. I just deleted all the code I typed using the book and thought of a quick new 'program' to write without using it for help, combining multiple lessons.

thanks again, I'll post when I get another problem.