# Simple syntax question (models, validation)

**URL:** <https://www.ruby-forum.com/t/simple-syntax-question-models-validation/125991>\
**Category:** Rails\
**Created:** [January 14, 2008, 3:14pm UTC](https://www.ruby-forum.com/t/simple-syntax-question-models-validation/125991 "2008-01-14T15:14:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ajs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@ajs](https://www.ruby-forum.com/u/ajs)\
**Post date:** [January 14, 2008, 3:14pm UTC](https://www.ruby-forum.com/t/simple-syntax-question-models-validation/125991/1 "2008-01-14T15:14:58Z")

</div>

who can explain this to me?

def before\_validation  
logger.info url  
url = “http://” \<\< url if url[0…3]==“www.”  
end

the logger line correctly logs any URL that’s been submitted, let’s  
say it’s “[www.amazon.co.uk](http://www.amazon.co.uk)”

BUT Rails raises an exception on the next line, complaining about nil.  
[] (obviously from the url[0…3] bit)

It works if I replace that line with  
self.url = “http://” \<\< self.url if self.url[0…3]==“www.”

I just want an explanation why. It seems to be happy with logger.info  
url, but why not url[0…3].

Why is ‘self.’ only necessary SOME of the time when manipulating model  
attributes?

---

<div class="post-metadata">

**Author:** ![ajs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@ajs](https://www.ruby-forum.com/u/ajs)\
**Post date:** [January 14, 2008, 3:36pm UTC](https://www.ruby-forum.com/t/simple-syntax-question-models-validation/125991/2 "2008-01-14T15:36:08Z")

</div>

On 14 Jan 2008, at 14:14, AJS wrote:

> BUT Rails raises an exception on the next line, complaining about nil.  
> [] (obviously from the url[0…3] bit)
> 
> It works if I replace that line with  
> self.url = “http://” \<\< self.url if self.url[0…3]==“www.”
> 
> I just want an explanation why. It seems to be happy with logger.info  
> url, but why not url[0…3].

Well you always need self.url= if you want to set the value of url, if  
not you are just setting a local variable.  
When you write url you could either be referring to the local variable  
url or trying to call the method url.  
On the line logger.info url there is no local variable called url, so  
its unambigious what you’re referring to.

But when you write url = “http://” \<\< url if url[0…3]==“www.”  
then a local variable called url is created (containing) nil. Now when  
you say url ruby assumes you mean the local variable.  
When local variables magic into existance is a slightly odd corner of  
ruby.  
For example, open up an irb prompt:

x #=\> NameError: undefined local variable or method `x’ for main:Object  
if false then x = 1; end #=\> nil  
x #=\> nil

Fred

---

<div class="post-metadata">

**Author:** ![ajs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@ajs](https://www.ruby-forum.com/u/ajs)\
**Post date:** [January 14, 2008, 3:46pm UTC](https://www.ruby-forum.com/t/simple-syntax-question-models-validation/125991/3 "2008-01-14T15:46:44Z")

</div>

thanks - that’s tremendously helpful.

On Jan 14, 2:35 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
