# Syntax for accessing methods and variables

**URL:** <https://www.ruby-forum.com/t/syntax-for-accessing-methods-and-variables/79356>\
**Category:** Rails\
**Created:** [December 14, 2006, 3:36pm UTC](https://www.ruby-forum.com/t/syntax-for-accessing-methods-and-variables/79356 "2006-12-14T15:36:52Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![patwa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b77776/32.png) [@patwa](https://www.ruby-forum.com/u/patwa)\
**Post date:** [December 14, 2006, 3:36pm UTC](https://www.ruby-forum.com/t/syntax-for-accessing-methods-and-variables/79356/1 "2006-12-14T15:36:52Z")

</div>

Hi all,

I have a controller named aircrafts. In there I put:

def showtime  
@time = Time.now  
end

In the default views/layouts/aircrafts.rhtml, I want to use the method  
above to show the current time on the page.

But adding \<%= @time %\> does nothing. Do I have to have some kind of  
\<%= showtime.time %\> syntax or something, like in Java?

Thanks. New to Ruby, and trying to learn as I go, so please forgive  
some of these ‘simple’ questions.

Hussein.

---

<div class="post-metadata">

**Author:** ![patwa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b77776/32.png) [@patwa](https://www.ruby-forum.com/u/patwa)\
**Post date:** [December 14, 2006, 3:59pm UTC](https://www.ruby-forum.com/t/syntax-for-accessing-methods-and-variables/79356/2 "2006-12-14T15:59:46Z")

</div>

Hussein P. wrote:

> Hi all,
> 
> I have a controller named aircrafts. In there I put:
> 
> def showtime  
> @time = Time.now  
> end
> 
> In the default views/layouts/aircrafts.rhtml, I want to use the method  
> above to show the current time on the page.
> 
> But adding \<%= @time %\> does nothing. Do I have to have some kind of  
> \<%= showtime.time %\> syntax or something, like in Java?
> 
> Thanks. New to Ruby, and trying to learn as I go, so please forgive  
> some of these ‘simple’ questions.
> 
> Hussein.

Hiya Hussein,

It doesnt quite work like that. I’m sure someone will be able to  
explain why better than me.

To do what you want though you can use helpers. Look in the directory  
/app/helpers/aircrafts\_helper.rb

Add the code above into there and you will be able to access it as you  
wish

Cheers  
Luke

---

<div class="post-metadata">

**Author:** ![patwa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b77776/32.png) [@patwa](https://www.ruby-forum.com/u/patwa)\
**Post date:** [December 14, 2006, 4:00pm UTC](https://www.ruby-forum.com/t/syntax-for-accessing-methods-and-variables/79356/3 "2006-12-14T16:00:54Z")

</div>

Opps forgot to add if you put that into application\_helper.rb then it  
will be available to all of your views.

---

<div class="post-metadata">

**Author:** ![patwa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b77776/32.png) [@patwa](https://www.ruby-forum.com/u/patwa)\
**Post date:** [December 14, 2006, 4:04pm UTC](https://www.ruby-forum.com/t/syntax-for-accessing-methods-and-variables/79356/4 "2006-12-14T16:04:42Z")

</div>

Hi.

Ok, so I guess the helpers files act somewhat like your java  
superclasses. Would the functions be accessed just through their name  
as in @time?

Also, how would you access controller-speific functions, as in the one I  
had in my original post?

Thanks.

Hussein.

---

<div class="post-metadata">

**Author:** ![patwa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b77776/32.png) [@patwa](https://www.ruby-forum.com/u/patwa)\
**Post date:** [December 14, 2006, 5:46pm UTC](https://www.ruby-forum.com/t/syntax-for-accessing-methods-and-variables/79356/5 "2006-12-14T17:46:12Z")

</div>

Hussein P. wrote:

> Also, how would you access controller-speific functions, as in the one I  
> had in my original post?

You can create a function in the controller and use the helper\_method  
specifier.

eg

class ApplicationController \< ActionController::Base

helper\_method :give\_time

def give\_time  
Time.now # return the current time  
end

end

Then in you view, you can put a  
\<%= give\_time %\>

In fact, helper\_method makes a method visible both in the controller and  
in the view. But, IMHO you should take care of not making wrong usage of  
this specifier.  
In most cases you just have to put these methods in the  
application\_helper.rb which makes it visible in all views.

---

<div class="post-metadata">

**Author:** ![patwa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b77776/32.png) [@patwa](https://www.ruby-forum.com/u/patwa)\
**Post date:** [December 14, 2006, 4:03pm UTC](https://www.ruby-forum.com/t/syntax-for-accessing-methods-and-variables/79356/6 "2006-12-14T16:03:17Z")

</div>

Hussein P. wrote:

> Hi all,
> 
> I have a controller named aircrafts. In there I put:
> 
> def showtime  
> @time = Time.now  
> end
> 
> In the default views/layouts/aircrafts.rhtml, I want to use the method  
> above to show the current time on the page.
> 
> But adding \<%= @time %\> does nothing. Do I have to have some kind of  
> \<%= showtime.time %\> syntax or something, like in Java?
> 
> Thanks. New to Ruby, and trying to learn as I go, so please forgive  
> some of these ‘simple’ questions.
> 
> Hussein.

Methods in the controller generally map to the view of that name. So,  
if you have a file called views/showtime.rhtml with \<%= @time %\>, it  
will display correctly.

You need to change it to \<%= showtime %\> if you want it to work.

However, this method should not be in the controller. It should probably  
be in helpers/aircraft\_helper.rb or helpers/application\_helper.rb

I’m still green to rails, so there could be a better way, but both of my  
options will work.

---

<div class="post-metadata">

**Author:** ![patwa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b77776/32.png) [@patwa](https://www.ruby-forum.com/u/patwa)\
**Post date:** [December 14, 2006, 11:12pm UTC](https://www.ruby-forum.com/t/syntax-for-accessing-methods-and-variables/79356/7 "2006-12-14T23:12:31Z")

</div>

Hi and thanks for the input. I think I understand now. I’ll work on  
this tonight and post back if there’s anything else.

Hussein.

---

<div class="post-metadata">

**Author:** ![patwa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b77776/32.png) [@patwa](https://www.ruby-forum.com/u/patwa)\
**Post date:** [December 14, 2006, 7:48pm UTC](https://www.ruby-forum.com/t/syntax-for-accessing-methods-and-variables/79356/8 "2006-12-14T19:48:08Z")

</div>

Hussein P. wrote:

> Hi all,
> 
> I have a controller named aircrafts. In there I put:
> 
> def showtime  
> @time = Time.now  
> end

Hi,

In Ruby 1)if you declare a method without associating it to any class  
you just call it directly:

showtime

1. if you declare a method within a class say X without using private,  
protected, or public you call it publicly(default way in Ruby)

obj=X.new  
obj.showtime

1. if you run them from within the script you need to add “puts” to see  
the results:  
puts showtime

puts obj.showtime

Li

### 

C:\>irb  
irb(main):001:0\> def showtime  
irb(main):002:1\> @time = Time.now  
irb(main):003:1\> end  
=\> nil  
irb(main):004:0\> showtime  
=\> Thu Dec 14 13:42:19 -0500 2006  
irb(main):005:0\> class X  
irb(main):006:1\> def showtime  
irb(main):007:2\> @time=Time.now  
irb(main):008:2\> end  
irb(main):009:1\> end  
=\> nil  
irb(main):010:0\> obj=X.new  
=\> #\<X:0x2c22b74\>  
irb(main):011:0\> obj.showtime  
=\> Thu Dec 14 13:43:12 -0500 2006  
irb(main):012:0\>

---

<div class="post-metadata">

**Author:** ![patwa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b77776/32.png) [@patwa](https://www.ruby-forum.com/u/patwa)\
**Post date:** [December 15, 2006, 2:24am UTC](https://www.ruby-forum.com/t/syntax-for-accessing-methods-and-variables/79356/9 "2006-12-15T02:24:18Z")

</div>

Honestly I would just put the code in the view:

\<%= Time.now %\>

If you use the time in many views and want to standardize the  
formatting, go with application\_helper.rb
