How to display environment variables as a group in RailsSetting environment variables on OS XA concise explanation of nil v. empty v. blank in Ruby on RailsUnderstanding the Rails Authenticity TokenHow to write a switch statement in RubyHow can I rename a database column in a Ruby on Rails migration?How do I get the current absolute URL in Ruby on Rails?Read environment variables in Node.jsHow to access environment variable values?List all environment variables from command line?How do I delete an exported environment variable?
Behavior of the zero and negative/sign flags on classic instruction sets
Is it rude to tell recruiters I would only change jobs for a better salary?
Confused about 誘われて (Sasowarete)
HackerRank: Electronics Shop
How did John Lennon tune his guitar
What caused Windows ME's terrible reputation?
Why limit to revolvers?
Why do candidates not quit if they no longer have a realistic chance to win in the 2020 US presidents election
Why is the collector feedback bias popular in electret-mic preamp circuits?
What are the arguments for California’s nonpartisan blanket primaries other than giving Democrats more power?
What impact would a dragon the size of Asia have on the environment?
Can someone explain this logical statement?
What is the closed form of the following recursive function?
Old short story where the future emperor of the galaxy is taken for a tour around Earth
Remove intersect line for one circle using venndiagram2sets
Did the Shuttle's rudder or elevons operate when flown on its carrier 747?
(algebraic topology) question about the cellular approximation theorem
3D-Plot with an inequality condition for parameter values
What is this old "lemon-squeezer" shaped pan
What's the phrasal verb for carbonated drinks exploding out of the can after being shaken?
Variation in the spelling of word-final M
Is it okay to retroactively change things when running a published adventure?
Filtering fine silt/mud from water (not necessarily bacteria etc.)
How long do Apple retain notifications to be pushed to iOS devices until they expire?
How to display environment variables as a group in Rails
Setting environment variables on OS XA concise explanation of nil v. empty v. blank in Ruby on RailsUnderstanding the Rails Authenticity TokenHow to write a switch statement in RubyHow can I rename a database column in a Ruby on Rails migration?How do I get the current absolute URL in Ruby on Rails?Read environment variables in Node.jsHow to access environment variable values?List all environment variables from command line?How do I delete an exported environment variable?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am using Rails 5.2 application. I want to display the Environment variables as a group in the endpoint.
env_controller.rb
class EnvController < ApplicationController
def index
render json: ENV.to_h
end
end
When I load http://localhost:3000/env, I see the following result
"XDG_VTNR": "7",
"MANPATH": "/home/ubuntu/.nvm/versions/node/v4.6.0/share/man:/home/ubuntu/.rvm/rubies/ruby-2.6.2/share/man:/home/ubuntu/.rvm/man:/usr/lib/jvm/java-8-oracle/man:/usr/local/man:/usr/local/share/man:/usr/share/man",
"S3_SOURCE_PATH": "dev/source",
"DB_ENV_USER": "postgres",
"XDG_SESSION_ID": "c2",
"rvm_bin_path": "/home/ubuntu/.rvm/bin",
"S3_DESTINATION_PATH": "dev/destination",
"SESSION": "ubuntu",
"DB_PORT_5432_TCP_ADDR": "localhost",
"S3_REGION": "us-east-1"
I want to group the environment variables as follows. My expected result is as follows
"S3":
"S3_SOURCE_PATH": "reports/source",
"S3_DESTINATION_PATH": "reports/destination",
"S3_REGION": "us-east-1"
,
"DB":
"DB_ENV_USER": "postgres",
"DB_PORT_5432_TCP_ADDR": "localhost"
,
"Others":
"XDG_VTNR": "7",
"MANPATH": "/home/ubuntu/.nvm/versions/node/v4.6.0/share/man:/home/ubuntu/.rvm/rubies/ruby-2.6.2/share/man:/home/ubuntu/.rvm/man:/usr/lib/jvm/java-8-oracle/man:/usr/local/man:/usr/local/share/man:/usr/share/man",
"XDG_SESSION_ID": "c2",
"rvm_bin_path": "/home/ubuntu/.rvm/bin",
"SESSION": "ubuntu"
I definitely need "S3" and "DB" to be grouped. There are more variables in my application should be grouped like this. Above are the samples.
How can I display it as the above?
ruby-on-rails ruby environment-variables
|
show 3 more comments
I am using Rails 5.2 application. I want to display the Environment variables as a group in the endpoint.
env_controller.rb
class EnvController < ApplicationController
def index
render json: ENV.to_h
end
end
When I load http://localhost:3000/env, I see the following result
"XDG_VTNR": "7",
"MANPATH": "/home/ubuntu/.nvm/versions/node/v4.6.0/share/man:/home/ubuntu/.rvm/rubies/ruby-2.6.2/share/man:/home/ubuntu/.rvm/man:/usr/lib/jvm/java-8-oracle/man:/usr/local/man:/usr/local/share/man:/usr/share/man",
"S3_SOURCE_PATH": "dev/source",
"DB_ENV_USER": "postgres",
"XDG_SESSION_ID": "c2",
"rvm_bin_path": "/home/ubuntu/.rvm/bin",
"S3_DESTINATION_PATH": "dev/destination",
"SESSION": "ubuntu",
"DB_PORT_5432_TCP_ADDR": "localhost",
"S3_REGION": "us-east-1"
I want to group the environment variables as follows. My expected result is as follows
"S3":
"S3_SOURCE_PATH": "reports/source",
"S3_DESTINATION_PATH": "reports/destination",
"S3_REGION": "us-east-1"
,
"DB":
"DB_ENV_USER": "postgres",
"DB_PORT_5432_TCP_ADDR": "localhost"
,
"Others":
"XDG_VTNR": "7",
"MANPATH": "/home/ubuntu/.nvm/versions/node/v4.6.0/share/man:/home/ubuntu/.rvm/rubies/ruby-2.6.2/share/man:/home/ubuntu/.rvm/man:/usr/lib/jvm/java-8-oracle/man:/usr/local/man:/usr/local/share/man:/usr/share/man",
"XDG_SESSION_ID": "c2",
"rvm_bin_path": "/home/ubuntu/.rvm/bin",
"SESSION": "ubuntu"
I definitely need "S3" and "DB" to be grouped. There are more variables in my application should be grouped like this. Above are the samples.
How can I display it as the above?
ruby-on-rails ruby environment-variables
ENV is an enumerable. Which means you can usegroup_by
,reduce
and all other methods of Enumerable.
– Sergio Tulentsev
Mar 26 at 7:33
@SergioTulentsev Is there any example? How can I use for my scenario?
– Galet
Mar 26 at 7:35
Yes, there are examples (of grouping) in the docs (look up Enumerable#group_by). I assume if you were provided with already grouped data, you'd have no problem displaying it.
– Sergio Tulentsev
Mar 26 at 7:36
Then how can I provide grouped data to ENV?
– Galet
Mar 26 at 7:39
You don't. You take data from ENV and convert/transform it into a grouped data structure.
– Sergio Tulentsev
Mar 26 at 7:40
|
show 3 more comments
I am using Rails 5.2 application. I want to display the Environment variables as a group in the endpoint.
env_controller.rb
class EnvController < ApplicationController
def index
render json: ENV.to_h
end
end
When I load http://localhost:3000/env, I see the following result
"XDG_VTNR": "7",
"MANPATH": "/home/ubuntu/.nvm/versions/node/v4.6.0/share/man:/home/ubuntu/.rvm/rubies/ruby-2.6.2/share/man:/home/ubuntu/.rvm/man:/usr/lib/jvm/java-8-oracle/man:/usr/local/man:/usr/local/share/man:/usr/share/man",
"S3_SOURCE_PATH": "dev/source",
"DB_ENV_USER": "postgres",
"XDG_SESSION_ID": "c2",
"rvm_bin_path": "/home/ubuntu/.rvm/bin",
"S3_DESTINATION_PATH": "dev/destination",
"SESSION": "ubuntu",
"DB_PORT_5432_TCP_ADDR": "localhost",
"S3_REGION": "us-east-1"
I want to group the environment variables as follows. My expected result is as follows
"S3":
"S3_SOURCE_PATH": "reports/source",
"S3_DESTINATION_PATH": "reports/destination",
"S3_REGION": "us-east-1"
,
"DB":
"DB_ENV_USER": "postgres",
"DB_PORT_5432_TCP_ADDR": "localhost"
,
"Others":
"XDG_VTNR": "7",
"MANPATH": "/home/ubuntu/.nvm/versions/node/v4.6.0/share/man:/home/ubuntu/.rvm/rubies/ruby-2.6.2/share/man:/home/ubuntu/.rvm/man:/usr/lib/jvm/java-8-oracle/man:/usr/local/man:/usr/local/share/man:/usr/share/man",
"XDG_SESSION_ID": "c2",
"rvm_bin_path": "/home/ubuntu/.rvm/bin",
"SESSION": "ubuntu"
I definitely need "S3" and "DB" to be grouped. There are more variables in my application should be grouped like this. Above are the samples.
How can I display it as the above?
ruby-on-rails ruby environment-variables
I am using Rails 5.2 application. I want to display the Environment variables as a group in the endpoint.
env_controller.rb
class EnvController < ApplicationController
def index
render json: ENV.to_h
end
end
When I load http://localhost:3000/env, I see the following result
"XDG_VTNR": "7",
"MANPATH": "/home/ubuntu/.nvm/versions/node/v4.6.0/share/man:/home/ubuntu/.rvm/rubies/ruby-2.6.2/share/man:/home/ubuntu/.rvm/man:/usr/lib/jvm/java-8-oracle/man:/usr/local/man:/usr/local/share/man:/usr/share/man",
"S3_SOURCE_PATH": "dev/source",
"DB_ENV_USER": "postgres",
"XDG_SESSION_ID": "c2",
"rvm_bin_path": "/home/ubuntu/.rvm/bin",
"S3_DESTINATION_PATH": "dev/destination",
"SESSION": "ubuntu",
"DB_PORT_5432_TCP_ADDR": "localhost",
"S3_REGION": "us-east-1"
I want to group the environment variables as follows. My expected result is as follows
"S3":
"S3_SOURCE_PATH": "reports/source",
"S3_DESTINATION_PATH": "reports/destination",
"S3_REGION": "us-east-1"
,
"DB":
"DB_ENV_USER": "postgres",
"DB_PORT_5432_TCP_ADDR": "localhost"
,
"Others":
"XDG_VTNR": "7",
"MANPATH": "/home/ubuntu/.nvm/versions/node/v4.6.0/share/man:/home/ubuntu/.rvm/rubies/ruby-2.6.2/share/man:/home/ubuntu/.rvm/man:/usr/lib/jvm/java-8-oracle/man:/usr/local/man:/usr/local/share/man:/usr/share/man",
"XDG_SESSION_ID": "c2",
"rvm_bin_path": "/home/ubuntu/.rvm/bin",
"SESSION": "ubuntu"
I definitely need "S3" and "DB" to be grouped. There are more variables in my application should be grouped like this. Above are the samples.
How can I display it as the above?
ruby-on-rails ruby environment-variables
ruby-on-rails ruby environment-variables
edited Mar 26 at 8:44
aridlehoover
1,82916 silver badges15 bronze badges
1,82916 silver badges15 bronze badges
asked Mar 26 at 6:34
GaletGalet
1,4086 gold badges39 silver badges89 bronze badges
1,4086 gold badges39 silver badges89 bronze badges
ENV is an enumerable. Which means you can usegroup_by
,reduce
and all other methods of Enumerable.
– Sergio Tulentsev
Mar 26 at 7:33
@SergioTulentsev Is there any example? How can I use for my scenario?
– Galet
Mar 26 at 7:35
Yes, there are examples (of grouping) in the docs (look up Enumerable#group_by). I assume if you were provided with already grouped data, you'd have no problem displaying it.
– Sergio Tulentsev
Mar 26 at 7:36
Then how can I provide grouped data to ENV?
– Galet
Mar 26 at 7:39
You don't. You take data from ENV and convert/transform it into a grouped data structure.
– Sergio Tulentsev
Mar 26 at 7:40
|
show 3 more comments
ENV is an enumerable. Which means you can usegroup_by
,reduce
and all other methods of Enumerable.
– Sergio Tulentsev
Mar 26 at 7:33
@SergioTulentsev Is there any example? How can I use for my scenario?
– Galet
Mar 26 at 7:35
Yes, there are examples (of grouping) in the docs (look up Enumerable#group_by). I assume if you were provided with already grouped data, you'd have no problem displaying it.
– Sergio Tulentsev
Mar 26 at 7:36
Then how can I provide grouped data to ENV?
– Galet
Mar 26 at 7:39
You don't. You take data from ENV and convert/transform it into a grouped data structure.
– Sergio Tulentsev
Mar 26 at 7:40
ENV is an enumerable. Which means you can use
group_by
, reduce
and all other methods of Enumerable.– Sergio Tulentsev
Mar 26 at 7:33
ENV is an enumerable. Which means you can use
group_by
, reduce
and all other methods of Enumerable.– Sergio Tulentsev
Mar 26 at 7:33
@SergioTulentsev Is there any example? How can I use for my scenario?
– Galet
Mar 26 at 7:35
@SergioTulentsev Is there any example? How can I use for my scenario?
– Galet
Mar 26 at 7:35
Yes, there are examples (of grouping) in the docs (look up Enumerable#group_by). I assume if you were provided with already grouped data, you'd have no problem displaying it.
– Sergio Tulentsev
Mar 26 at 7:36
Yes, there are examples (of grouping) in the docs (look up Enumerable#group_by). I assume if you were provided with already grouped data, you'd have no problem displaying it.
– Sergio Tulentsev
Mar 26 at 7:36
Then how can I provide grouped data to ENV?
– Galet
Mar 26 at 7:39
Then how can I provide grouped data to ENV?
– Galet
Mar 26 at 7:39
You don't. You take data from ENV and convert/transform it into a grouped data structure.
– Sergio Tulentsev
Mar 26 at 7:40
You don't. You take data from ENV and convert/transform it into a grouped data structure.
– Sergio Tulentsev
Mar 26 at 7:40
|
show 3 more comments
1 Answer
1
active
oldest
votes
I think what you want is a custom serializer. Something similar to this:
class EnvSerializer
def as_json
S3: env_select('S3'),
DB: env_select('DB'),
Other: env_reject('S3', 'DB')
end
private
def env_select(prefix)
ENV.select
end
def env_reject(*prefixes)
ENV.reject
end
end
Which would look like this in the controller:
class EnvController < ApplicationController
def index
render json: EnvSerializer.new.as_json
end
end
Oh nice, since whenstart_with?
is variadic?
– Sergio Tulentsev
Mar 26 at 8:27
@SergioTulentsev For all I can tell, since at least 1.9.1; That's as far as I went back.
– Frederik Spang
Mar 26 at 8:32
3
@FrederikSpang: you live, you learn. This could have saved me a few loops in the past :)
– Sergio Tulentsev
Mar 26 at 8:35
@SergioTulentsev Know the feeling!
– Frederik Spang
Mar 26 at 8:35
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55351090%2fhow-to-display-environment-variables-as-a-group-in-rails%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think what you want is a custom serializer. Something similar to this:
class EnvSerializer
def as_json
S3: env_select('S3'),
DB: env_select('DB'),
Other: env_reject('S3', 'DB')
end
private
def env_select(prefix)
ENV.select
end
def env_reject(*prefixes)
ENV.reject
end
end
Which would look like this in the controller:
class EnvController < ApplicationController
def index
render json: EnvSerializer.new.as_json
end
end
Oh nice, since whenstart_with?
is variadic?
– Sergio Tulentsev
Mar 26 at 8:27
@SergioTulentsev For all I can tell, since at least 1.9.1; That's as far as I went back.
– Frederik Spang
Mar 26 at 8:32
3
@FrederikSpang: you live, you learn. This could have saved me a few loops in the past :)
– Sergio Tulentsev
Mar 26 at 8:35
@SergioTulentsev Know the feeling!
– Frederik Spang
Mar 26 at 8:35
add a comment |
I think what you want is a custom serializer. Something similar to this:
class EnvSerializer
def as_json
S3: env_select('S3'),
DB: env_select('DB'),
Other: env_reject('S3', 'DB')
end
private
def env_select(prefix)
ENV.select
end
def env_reject(*prefixes)
ENV.reject
end
end
Which would look like this in the controller:
class EnvController < ApplicationController
def index
render json: EnvSerializer.new.as_json
end
end
Oh nice, since whenstart_with?
is variadic?
– Sergio Tulentsev
Mar 26 at 8:27
@SergioTulentsev For all I can tell, since at least 1.9.1; That's as far as I went back.
– Frederik Spang
Mar 26 at 8:32
3
@FrederikSpang: you live, you learn. This could have saved me a few loops in the past :)
– Sergio Tulentsev
Mar 26 at 8:35
@SergioTulentsev Know the feeling!
– Frederik Spang
Mar 26 at 8:35
add a comment |
I think what you want is a custom serializer. Something similar to this:
class EnvSerializer
def as_json
S3: env_select('S3'),
DB: env_select('DB'),
Other: env_reject('S3', 'DB')
end
private
def env_select(prefix)
ENV.select
end
def env_reject(*prefixes)
ENV.reject
end
end
Which would look like this in the controller:
class EnvController < ApplicationController
def index
render json: EnvSerializer.new.as_json
end
end
I think what you want is a custom serializer. Something similar to this:
class EnvSerializer
def as_json
S3: env_select('S3'),
DB: env_select('DB'),
Other: env_reject('S3', 'DB')
end
private
def env_select(prefix)
ENV.select
end
def env_reject(*prefixes)
ENV.reject
end
end
Which would look like this in the controller:
class EnvController < ApplicationController
def index
render json: EnvSerializer.new.as_json
end
end
answered Mar 26 at 8:15
aridlehooveraridlehoover
1,82916 silver badges15 bronze badges
1,82916 silver badges15 bronze badges
Oh nice, since whenstart_with?
is variadic?
– Sergio Tulentsev
Mar 26 at 8:27
@SergioTulentsev For all I can tell, since at least 1.9.1; That's as far as I went back.
– Frederik Spang
Mar 26 at 8:32
3
@FrederikSpang: you live, you learn. This could have saved me a few loops in the past :)
– Sergio Tulentsev
Mar 26 at 8:35
@SergioTulentsev Know the feeling!
– Frederik Spang
Mar 26 at 8:35
add a comment |
Oh nice, since whenstart_with?
is variadic?
– Sergio Tulentsev
Mar 26 at 8:27
@SergioTulentsev For all I can tell, since at least 1.9.1; That's as far as I went back.
– Frederik Spang
Mar 26 at 8:32
3
@FrederikSpang: you live, you learn. This could have saved me a few loops in the past :)
– Sergio Tulentsev
Mar 26 at 8:35
@SergioTulentsev Know the feeling!
– Frederik Spang
Mar 26 at 8:35
Oh nice, since when
start_with?
is variadic?– Sergio Tulentsev
Mar 26 at 8:27
Oh nice, since when
start_with?
is variadic?– Sergio Tulentsev
Mar 26 at 8:27
@SergioTulentsev For all I can tell, since at least 1.9.1; That's as far as I went back.
– Frederik Spang
Mar 26 at 8:32
@SergioTulentsev For all I can tell, since at least 1.9.1; That's as far as I went back.
– Frederik Spang
Mar 26 at 8:32
3
3
@FrederikSpang: you live, you learn. This could have saved me a few loops in the past :)
– Sergio Tulentsev
Mar 26 at 8:35
@FrederikSpang: you live, you learn. This could have saved me a few loops in the past :)
– Sergio Tulentsev
Mar 26 at 8:35
@SergioTulentsev Know the feeling!
– Frederik Spang
Mar 26 at 8:35
@SergioTulentsev Know the feeling!
– Frederik Spang
Mar 26 at 8:35
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55351090%2fhow-to-display-environment-variables-as-a-group-in-rails%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
ENV is an enumerable. Which means you can use
group_by
,reduce
and all other methods of Enumerable.– Sergio Tulentsev
Mar 26 at 7:33
@SergioTulentsev Is there any example? How can I use for my scenario?
– Galet
Mar 26 at 7:35
Yes, there are examples (of grouping) in the docs (look up Enumerable#group_by). I assume if you were provided with already grouped data, you'd have no problem displaying it.
– Sergio Tulentsev
Mar 26 at 7:36
Then how can I provide grouped data to ENV?
– Galet
Mar 26 at 7:39
You don't. You take data from ENV and convert/transform it into a grouped data structure.
– Sergio Tulentsev
Mar 26 at 7:40