How do i get list of all installed packages in ruby 2.0.0 - ruby

I want to get list of all installed packages on my machine using ruby gem or plugin. Functionality is similar to dpkg -l command on ubuntu. Is there any appropriate ruby gem or plugin available to get the same?

If you mean Ruby gems, then
gem list

You can execute a shell command inside the ruby interpreter. In your case, simply run
`dpkg -l`
The output will be a string containing the output of the command. Please note the `.
Keep in mind there are several ways to perform a shell command in Ruby.

There is a Debian package called ruby-debian (in older Debian releases it is called dpkg-ruby) which contains Ruby bindings to dkpg.
Note: it relies on a C extension, so it will not work on Ruby implementations that don't support them, such as IronRuby.

Related

error Your Ruby version is 2.6.8, but your Gemfile specified 2.7.5

This happens when I run npx react-native init AwesomeProject.
When I check the system ruby version with ruby -v, it is already 2.7.5. ruby 2.7.5p203 (2021-11-24 revision f69aeb8314) [x86_64-darwin21]. Anyone has any idea for this problem?
Sounds like you’re using rvm to manage Ruby versions.
You need to install and run the correct version, not delete the current.
Something like
rvm install 2.7.5
rvm use 2.7.5
The Gemfile Isn't (Directly) Your Issue
The Gemfile is irrelevant to solving this issue. It's just triggering it because your in-project Ruby version isn't matching what RubyGems (via the Gemfile or Gemfile.lock) expects as a constraint. It could be a minimum version, an exact version, an approximate version, and so forth. There are a lot of ways to specify version constraints in a project, and the Gemfile is just where the constraint-related exception is being raised by Bundler.
You could possibly make the problem go away simply by removing the requirement for a later version of Ruby from the Gemfile or gemspec, removing the Gemfile.lock, and re-running Bundler. However, if your code relies on features in a later version this will just create other problems for you. You really ought to uncover what's changing your Ruby environment within the project directory.
Dotfiles
There are a lot of reasons this could happen, but if your system Ruby is 2.7.5, then you need to check your project directory for various dotfiles like:
.ruby-version
.rvmrc
~/.rvmrc
.envrc
.env
or various other files that affect your shell environment or whatever Ruby version manager you're using. Most Ruby version managers respect .ruby-version, but some version managers use other files, including defaults or shims that may be set elsewhere. IDE's also often have their own project-specific configuration files, too, and they can sometimes be set to override the project's standard settings.
Also, make sure to check your Gemfile.lock and *.gemspec in addition to the Gemfile itself, in case something is being specified there or constrained by some other dependency.
Inspecting Environment Variables
You should also look at the Ruby- and RubyGems-related environment variables from your project directory to see how various values are set within the project. For example:
printenv | grep -E '^(RUBY|GEM)' | sort
Shebang Lines
In addition, you should check your shebang lines in any executable Ruby or shell scripts you're relying on to see whether a specific non-system Ruby is being invoked. For example:
grep -Enr '^#.*ruby' *.rb | grep -F '.rb:1:'
will find all the shebang lines that properly appear on the first line of a Ruby file. This will either point to a specific Ruby like #!/usr/bin/ruby or might be using a PATH lookup with #!/usr/bin/env ruby.
Shell scripts might be harder to inspect, as there may be calls to other executables or even an exec command, so you'll need to be more liberal with your grepping if you are looking for an interpreter call further down than the shebang line.
Checking PATH Order
In the case of #!/usr/bin/env ruby, you should inspect your PATH environment variable to see why the Ruby you want isn't being called first. Using which -a ruby (if supported by your OS) will show you all rubies in your PATH in the order they would be invoked by the shell. It's possible that you're simply calling an unexpected Ruby version that comes first in the PATH.

Run a Ruby Script with Python

I have a Ruby Script that when I run in the shell it works (as user):
/home/user/wpscan/$ ruby ./wpscan.rb -u www.mysite.com
However, I'd like to automate this with a function that I created with Python. Here is the python script:
#!/usr/bin/python
import os
wpscan_env = "/wpscan/"
os.chdir(os.environ['HOME'] + wpscan_env)
os.system("ruby ./wpscan.rb -u www.mysite.com")
Notice that the Python script is in a different folder, home/user/python/first.py and this is why I do the os.chdir() function. When I go back to the shell and type:
/home/user/python/$ python first.py
This is the output I get:
Could not find addressable-2.4.0 in any of the sources
Run `bundle install` to install missing gems.
I am using Ubuntu 14.04 and in order to get wpscan to work, it asked me to install Ruby 2.3.0. I did this via RVM.
wpscan.rb has a few dependencies and it seems it's not instantiating them. Also if I'm inside the wpscan folder and do ruby ./wpscan.rb ... it will work. However if I try to do this from the home directory: ruby wpscan/wpscan.rb ... it throws an error:
[ERROR] cannot load such file --typhoeus
[TIP] try to run 'gem install typhoeus' or 'gem install --user-install typhoeus'. If you still get an error, Please see README file or https://github.com/wpscanteam/wpscan
I have no knowledge of Ruby, this is my first real Python script, and I just installed wpscan 2 nights ago. I'm way out of my league here and I need help. Any further question can be elaborated per request.
Most probably, ruby in your python script is not the same as ruby in your shell. This can happen if you e.g. have a default system ruby installed (e.g. from the system packages) and you install another ruby version via RVM.
When using shell, the RVM automatically loads it's environment from the shell init scripts (e.g. ~/.bashrc) and knows which ruby to use from its settings. Whereas your python script does not load any rvm environment (it's not a login shell) and calls the default system ruby.
In that case, you need to explicitly call the correct ruby from the RVM in your python script. You can to it by calling the RVM wrapper:
browse directories under ~/.rvm/wrappers/ and find the correct ruby version and gemset that you want to use
in your python script, call the ruby command from this wrapper directory instead of the plain `ruby, something like:
rvm_ruby = os.environ['HOME'] + "/.rvm/wrappers/ruby-2.3.0-p100#myproject/ruby"
os.system(rvm_ruby + " ./wpscan.rb -u www.mysite.com")
This should fix your problem.
Thats so simple to execute ruby scripts from python...
import os
os.system('ruby filename.rb')
#if you want to store the output in seperate file
os.system('ruby filename.rb > outfilename.txt)

Change gem env RUBY EXECUTABLE path for one command

I would like to run gem commands, such as gem install, with a different ruby version than what is listed in gem env. The Ruby version I want to use is a pre-compiled version which I have the path for, so installing and using another version from RVM or similar would not solve my problem.
I do not want to change the RUBY EXECUTABLE permanently, just for one command at a time. I have tried to set GEM_HOME, GEM_PATH, PATH, RUBY and more. I have tried firing up gem with specific/version/of/ruby/path/ruby path/to/gem env, but I still get the default Ruby in my RUBY EXECUTABLE variable.
I even tried settingRUBY_EXECUTABLE=/path/to/correct/ruby, which also did not work.
What really surprised me was that when I edited the shebang in the path/to/gem file itself so it pointed to the correct Ruby, it still did not work! What is up with that?!
How can I change this variable so I can use gem goodness with my custom compiled Ruby?
This one is really beating me. I have now updated my rbconfig.rb to point to the desired Ruby path. I have looked at the rubygems source and replaced every single instance of the default ruby , in all the files I could find, with the path to the one I want. Even this did not set the environment correctly. Is this somehow hard-coded into the compiled ruby? If that is the case, why the star*4 is this done?
Try using rbenv (https://github.com/sstephenson/rbenv) or RVM to manage Ruby versions (https://rvm.io/). When you switch Ruby versions with rbenv, gem env will use use the new Ruby version. The following command can be used to change the Ruby version for a single shell:
$ rbenv shell 2.1.2
After hours and hours of research, stepping through the Ruby source with Pry, reading source code and more I figured out that this is not possible to do because it is hard-coded into ruby at compile time (wtf?). Anyway, the way to solve this is to simply recompile Ruby. Yeah.
There is also apparently a compile flag which you can set which removes this hard-coded environment: --enable-load-relative
After struggling with this for way to long I finally got this project working, where I have made an easy to use portable version of Ruby. Simply put, a folder with Ruby on it which you can move about, put on a USB stick or whatever, and it still works :)

How to control what Ruby interpreter Puppet uses?

I have an app that requires Ruby 2.1 and use Puppet to provision a handful of servers.
The problem is, when I install on the 2.1 version of Ruby, Puppet starts using that, and it is not supported.
I would like to somehow point Puppet to a 2.0 version of Ruby installed in /opt.
Any suggestions?
Perhaps you could add a shebang line in the .rb file that points to the other version of ruby?
Alternatively you could try to specifically add the bin/ruby location in /opt to the $PATH variable.
Typically, you shouldn't let the system determine the path, but instead provide the full path to the Ruby interpreter you want to execute your script:
/usr/bin/ruby /some/path/to/foo.rb
Or:
/usr/local/bin/ruby1.9.2 /path/to/bar.rb

Create named pipe in Ruby

I am trying to create a named pipe inside Ruby. Besides using the system command (system("mkfifo #{pipe_name}")), is there a native Ruby function allowing me to do this?
Current versions of Ruby (starting with 2.3.0) now have a native File::mkfifo:
File.mkfifo('pipe_name')
Old answer for older versions of Ruby:
I don't believe there's anything fully native, but there's the mkfifo gem.
Install like this:
gem install mkfifo
Then use like this:
require "mkfifo"
File.mkfifo('pipe_name')

Resources