I believe the way this works in rails has to do with the order in which variables are resolved. In this case, @name is an instance variable that's already been assigned elsewhere (your controller). Rails loads the view after the controller class has been instantiated. For this to work, the view is probably "mixed-into" the controller class somehow to ensure that the instance variable is visible. If you try this with a standard ruby function, it doesn't work.
def my_view "hello #{name}" end name = "bilbo" puts my_view() `my_view': undefined local variable or method `name' for main:Object (NameError) You could obviously use a global variable, but that would be really nasty. With Clojure, your function can see the variable so long as it's defined ahead of time. (def my-name "bilbo") ; name is a reserved word (defn my-view [] (str "hello " my-name)) (my-view) "hello bilbo" The my-name variable doesn't even have to be bound in this case, just created. I think what you're really looking for though is a way to define a function with no arguments referencing a variable that isn't defined at all. I'm not sure how you would accomplish that. -Travis On Sep 3, 8:42 am, ngocdaothanh <ngocdaoth...@gmail.com> wrote: > Hi, > > In Rails you can create a view like this: > > my_view.erb: > <%= "hello" + @name %> > > I'm new to Clojure. I want to create something like the above like > this: > > (defn my-view [] > (str "hello" name)) > > The point is: > * name is not known when my-view is defined > * I don't want to pass name as a argument of my-view, this is kind of > ugly boilerplate > > How can I implement this feature in Clojure? > > Thanks. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---