Skip to content

Instantly share code, notes, and snippets.

@tylergaw
Created March 4, 2014 18:31
Show Gist options
  • Select an option

  • Save tylergaw/9352620 to your computer and use it in GitHub Desktop.

Select an option

Save tylergaw/9352620 to your computer and use it in GitHub Desktop.
I am not smart/don't understand scope I guess
# I'm on Ruby 1.9.2
# Using sinatra 1.4.4
require "sinatra"
module TestModule
get "/" do
"Hello world"
end
end
# Running that gives the following error:
# `<module:TestModule>': undefined method `get' for TestModule:Module (NoMethodError) from ex2.rb:10:in `<main>'
#
# Seems that I'm trying to use Sinatra methods out of scope here?
#
# Why am I trying to do this?
# I'm going through Learn Ruby the Hard Way. This is in Exercise 50:
# http://ruby.learncodethehardway.org/book/ex50.html#make-a-simple-hello-world-project
@tylergaw
Copy link
Copy Markdown
Author

tylergaw commented Mar 4, 2014

This is easy enough to get around, but I'm trying to better understand how scope in Ruby works. I can require other libraries and access them within the TestModule without trouble. Is Sinatra different somehow?

@javierjulio
Copy link
Copy Markdown

Yes, if I'm right it is. Sinatra has a modular version. The default assumes you'll subclass the Sinatra::Base class. I've been modifying an app that uses the modular version: https://github.com/javierjulio/sinatra-blog if it helps any to reference. I've looked over the Learn Ruby The Hard Way post (great you're doing this, I started it but didn't finish, found Ruby Koans to be a lot more fun) but not sure how that would work. Seeing it wrapped in a module is new to me. Sorry I'm not able to help more. If I find out something else I'll be sure to share it.

@tylergaw
Copy link
Copy Markdown
Author

tylergaw commented Mar 4, 2014

OK, thanks. Maybe the example code in the book is just outdated? That blog will definitely be helpful. I think I'll just make this work without the module until I understand them more.

@javierjulio
Copy link
Copy Markdown

I would suggest without the module since the Sinatra sites uses it that way. I haven't seen it defined this way before and not sure how it would work. Not sure what really happens. Either way Sinatra does have a modular version but didn't want to go into that much since its not the point here. I also recommend trying out Ruby Koans. Might find it easier to learn and pickup things like modules.

@tylergaw
Copy link
Copy Markdown
Author

tylergaw commented Mar 4, 2014

@javierjulio Cool. Will definitely check out Koans. Thanks

@timmillwood
Copy link
Copy Markdown

I'm not sure why you have it in a module

require "sinatra"

get "/" do
  "Hello world"
end

This works just fine as a classic style app.

If you want modular you can do:

require "sinatra/base"

Class HelloWorld < Sinatra::Base
  get "/" do
    "Hello world"
  end
end

And if you really want it in a module (which can be useful) you can do:

require "sinatra/base"

module TestModule
  Class HelloWorld < Sinatra::Base
    get "/" do
      "Hello world"
    end
  end
end

Does that help?

FYI I'm writing a book about Sinatra

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment