6 Replies - 1117 Views - Last Post: 10 May 2014 - 11:09 AM Rate Topic: -----

#1 fiberoptck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 08-May 14

Trying to understand a Ruby lab

Posted 10 May 2014 - 08:16 AM

Ok just started learning Ruby and I am excited. But having issues understanding one l problem

I have a table, contains ID (1, 2, 3, 4,) an Item (potion, Ether, Map,) and a description (heals, heals poison, shows location).

Now the name of the table is Bag. I want to change update the description for the map. This is what I did initially:

$ Bag.find(3)
#<Bag id: 3, Item: "Map", Description: "Shows Location">


Now I wanted to update that Map Description, so I did:

$ bag.description = "Shows your location"
#<SyntaxError: Syntax error, unexpected tCONSTANT, expecting $end>


So this perplexed me I then tried adding a variable:

$ b = Bag.find(3)
#<Bag id: 3, Item: "Map", Description: "Shows Location">

$ b.Bag = "Shows you location"
"Shows you location"
$ b.save
true


Now I am trying to understand this, I require a variable to tell the model to do something? I guess I am not certain of why I needed a variable. Is it always required when changing something in a datastore to add a variable for it?

Is This A Good Question/Topic? 0
  • +

Replies To: Trying to understand a Ruby lab

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,315
  • Joined: 21-June 11

Re: Trying to understand a Ruby lab

Posted 10 May 2014 - 08:41 AM

There is no way that bag.description = "Shows your location" would cause the syntax error you describe. The problem with that line is that bag does not exist, but that would cause a different error message.

PS: Just to clarify. You're using Rails and Bag is an ActiveRecord model, correct? And the commands you've shown where entered into the Rails console?
Was This Post Helpful? 1
  • +
  • -

#3 fiberoptck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 08-May 14

Re: Trying to understand a Ruby lab

Posted 10 May 2014 - 08:51 AM

I should of said it was an example I am sorry I am not all that far into understanding Ruby

let me try this again

so erase my ill example,

I have a table it is named Bag. I wanted to change the description of the item Map that has an ID of 3. I couldn't change the description without adding a variable.

I guess my real question is why do I need to add a variable. All I wanted to do was change the :Description.

what does the variable actually doing?
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,315
  • Joined: 21-June 11

Re: Trying to understand a Ruby lab

Posted 10 May 2014 - 09:25 AM

How did you try to change it without a variable? If you try to use a setter method followed by save, then of course you need a variable because how are you going to call two methods on an object without storing it in a variable first? If you use update, it should work fine without a variable.

That said, what's wrong with using a variable?
Was This Post Helpful? 1
  • +
  • -

#5 fiberoptck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 08-May 14

Re: Trying to understand a Ruby lab

Posted 10 May 2014 - 09:58 AM

Ok so what I am gathering here is that the use of a Variable is to declare a value so

$ x = 10
=> 10


So by doing that above I have declared that x has a value of 10. so if I do
x = table.find(:ID)
I am declaring that x has a value of table.find(:ID). Am I understanding this right? Its not that I am questioning the use of variable. I am trying to understand the use of adding the variable.

I am sorry I am so confusing haha, as time goes I promise I will start to sound more literate when trying to explain myself through syntax.
Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,315
  • Joined: 21-June 11

Re: Trying to understand a Ruby lab

Posted 10 May 2014 - 10:30 AM

Yes, you evaluate the expression table.find(:ID) and x then has that value. The use of that is that you can then refer to that object through the variable. Otherwise you'd only be able to call a method on it once and then the object would be gone.

Do note that if you call table.find(:ID) multiple times, you'll get back multiple independent objects. So table.find(:ID).foo(); table.find(:ID).bar() is not the same as x = table.find(:ID); x.foo(); x.bar().
Was This Post Helpful? 2
  • +
  • -

#7 fiberoptck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 08-May 14

Re: Trying to understand a Ruby lab

Posted 10 May 2014 - 11:09 AM

Ok thank you, I still learning so that last note was a little outside my realm as of now. This day two of my classes and the instructor has us learning Ruby the hard way and referring to _why for inspiration. I feeling confident but he has us just looking at stuff and then copying it to see the result. I don't really like to just copy things and see the result I really was hoping to dissect the formula rather than just see the output. So I was just sort of confused by the Variable and what really came from it.

Thank you for taking the time though. I feel somewhat better about what the heck I am doing! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1