0

I'm trying to understand how to make a variable available to a block that is not passed to the block as a parameter.

For example, how does Sinatra make params hash available?

get '/hello/:name' do
  howAmIAccessingThis = params[:name]
end

Where is params coming from? This:

get '/hello/:name' do |params|
  @hisName = params[:name]
end

might make sense because params is declared as a block argument, but that's not how it works. Looking through the source I cannot find how the params hash is getting passed to the block without it being a block parameter.

Robin Daugherty
  • 7,115
  • 4
  • 45
  • 59
keto23
  • 1,177
  • 1
  • 10
  • 16
  • I don't believe this is a duplicate, as it is asking how access to the param variable is achieved, not where the variables in the hash come from. – Chris Apr 18 '15 at 20:06
  • It's not a duplicate. Being tagged "sinatra" when it's a question about the Ruby language has caused a lot of confusion. There's an answer to this question. – Robin Daugherty Feb 11 '20 at 03:40

2 Answers2

1

If it is not a local variable or a block variable, then it is a method. I don't know about Sinatra, but there must be a method params defined somewhere.

sawa
  • 165,429
  • 45
  • 277
  • 381
  • 2
    This is old enough that the source may have been different at the time, but it Sinatra::Base has an attr accessor for params. I'd have made a full answer, but this topic is closed. – Chris Apr 18 '15 at 20:08
0

Using Parameters

Parameters in Sinatra are like everything else--simple and straightforward.

#!/usr/bin/env ruby
require 'rubygems'
require 'sinatra'

get '/hello/:name' do
  "Hello #{params[:name]}!"
end

Once you've made this change, you'll need to restart the Sinatra application. Kill it with Ctrl-C and run it again. (There's a way around this, but we'll look at that in a future article.) Now, the parameters are straightforward. We've made an action called /hello/:name. This syntax is imitating what the URLs will look like, so go to http://localhost:4567/hello/Your Name to see it in action.

The /hello portion matches that portion of the URL from the request you made, and :name will absorb any other text you give it and put it in the params hash under the key :name. Parameters are just that easy. There is of course much more you can do with these, including regexp-based parameters, but this is all you'll need in almost every case.

Reference: http://ruby.about.com/od/sinatra/a/sinatra2.htm

EDIT

params values can come from the query string of a GET request, or the form data of a POST request, but there's also a third place they can come from: The path of the URL.

As you might know, Rails uses something called routes to direct requests to their corresponding controller actions. These routes may contain segments that are extracted from the URL and put into params. For example, if you have a route like this:

match 'products/:id', ... Then a request to a URL like http://example.com/products/42 will set params[:id] to 42

So, whenever an URL GET, POST or Path contains such pattern then params hash is automatically constructed by rails.

Also check the Parameters section(Section 4) here

Vamsi Krishna
  • 3,742
  • 4
  • 20
  • 45
  • I know how to use Sinatra, I'm just wondering how Sinatra passes in the params hash under the hood. – keto23 Sep 11 '13 at 11:09
  • Ok, then you want to know how :name is converted to a params[:name] in Sinatra... – Vamsi Krishna Sep 11 '13 at 11:15
  • In that case please go through this link http://stackoverflow.com/questions/6885990/rails-params-explained – Vamsi Krishna Sep 11 '13 at 11:21
  • I know how to use it, I want to know _how_ sinatra passes it into the block, as its not defined anywhere (block argument, etc). – keto23 Sep 12 '13 at 09:19
  • Still not answering the question..... I'm wondering *how* sinatra is passing in the params hash. Where is it being declared? – keto23 Sep 13 '13 at 09:10