Methods from Tree::TreeNode not working - ruby

I have a question about ruby gems. I'm writing some code in ruby and I want to use a tree. I decided to use the Tree::TreeNode class. See here http://www.rubydoc.info/github/evolve75/RubyTree/Tree/TreeNode for documentation.
This is my first time using a gem. I ran sudo gem install tree in the terminal and added require 'tree' to the beginning of my ruby file. For the most part it works. I can initialize nodes and most of the methods work. However, some methods such as #each_leaf and #children don't work. Here's example code:
require 'tree'
node1 = Tree::TreeNode.new('node1',1)
node2 = Tree::TreeNode.new('node2',2)
node1.add(node2)
node1.each_leaf { |node| puts "name: #{node.name}, content: #{node.content}"}
When I run it I get the following error message:
test.rb:5:in `<main>': undefined method `each_leaf' for #<Tree::TreeNode:0x007fc2a905e560> (NoMethodError)
but when I run the code with each_leaf replaced by each it works perfectly (of course I get info for nodes 1 and 2, whereas if it worked each_leaf should only get me the info for node 2).
Looking through the documentation for TreeNode I've noticed that all the methods that don't work for me are listed next to Tree::TreeNode+ rather than Tree::TreeNode. I haven't been able to figure out what this means. Do I have to do something extra or install some other gem to get those methods? Thanks for any help!

This is because the version of the gem on rubygems.org lags behind the latest version on github (from which the documentation is generated). You can install the github version using:
gem install specific_install
gem specific_install -l https://github.com/evolve75/RubyTree.git
or just add it to your Gemfile:
gem 'tree', github: 'evolve75/RubyTree'
Update: looks like it does have the latest version on rubygems, but it's renamed to rubytree, hence
gem uninstall tree
gem install rubytree
but require 'tree' as you did before.

Related

Is the net/http gem not in the default library for Ruby?

I want to create a Gemfile.lock by typing 'bundle install' but my local machine can't find the gem net/http. I've tried typing 'bundle update net/http' & 'bundle --full-index' & 'gem install bundler' but I keep getting this error when I try 'bundle install' again:
Could not find gem 'net/http' in rubygems repository https://rubygems.org/ or installed locally. The source does not contain any versions of 'net/http'
my Gemfile resembles the following:
source "https://rubygems.org"
gem 'open-uri'
gem 'nokogiri'
gem 'net/http'
gem 'pry'
Other solutions to this problem suggest removing the line for gem net/http because net/http is part of the default library for Ruby...however when I do this everything loads fine, and I can create a Gemfile.lock upon typing 'bundle install' but when I run my code I get the following error:
Traceback (most recent call last):
run.rb:4:in `': uninitialized constant Net (NameError)
Did you mean? Set
The line of code this refers to is
response = Net::HTTP.get(url)
I'm running Ruby version 2.6.1
The name of the gem is net-http. It is one of the first hits when you google for "net/http".
However, in Ruby 2.6.1, net/http is still part of the standard library, not a gem. Net/http was only removed from the standard library in Ruby 3.0.
There are two different kinds of standard gems:
Default gems: These gems are part of Ruby and you can always require them directly. You cannot remove them. They are maintained by Ruby core.
Bundled gems: The behavior of bundled gems is similar to normal gems, but they get automatically installed when you install Ruby. They can be uninstalled and they are maintained outside of Ruby core.
Your problem is the wrong name gem ('net-http' instead of 'net/http', you can run gem search ^net to find out remote gems start by 'net').
If the gem is 'default', no need to declare it in Gemfile.
You can check standard default gems on: https://stdgems.org/

fixing a failed ruby gem install

I am installing the ruby gem mysql-dbd on a new system which is running ruby 2.5. The problem is that it gets a syntax error because at 2.4 ruby combined Integer and Fixed num types.
The failed install leaves the unpacked gem package on disk so I was able to examine the entrails and the fix appears to be trivial (as in insert a '#' in a statement to remove the now redundant reference to FixNum).
My question is having fixed the source how do I go about building and installing the gem? Not being familiar with rake.
BTW the gem has long been "unsupported".
I can't get the gem source repository to load, but you've got at least two options:
Locally, you might be able to build it. Try running rake build from the root folder of the gem, and look in the pkg folder for the built gem. You should be able to then gem install pkg/<gem name>.gem in that folder
If you need to share it with others, push the code up on github. Make your change, and if you're using bundler in the codebase that's using the gem, update your Gemfile to point to your source.
You could try something like this:
# install_dbd_mysql.rb
Fixnum = Integer
require 'rubygems/commands/install_command'
install = Gem::Commands::InstallCommand.new
install.handle_options ['dbd-mysql']
install.execute
Run:
$ ruby install_dbd_mysql.rb

Pry is not a module

Ok, as #tim-moore asked, I will post it in new question.
Ok, so I wanted to make gem using bundle. Pry extension gem require that gem start with pry- as mentioned here.
I used:
bundle gem pry-name
but it messed up my file structure
create pry-name/pry-name.gemspec
create pry-name/lib/pry/name.rb
create pry-name/lib/pry/name/version.rb
As you can see it created lib/pry directory. I know it's gem's style to created such structure but now I pry cannot load this gem automatically
One solution from my question was:
create pry-name.rb that contain only require 'pry/name'
After I have done this, and build gem, I started pry:
This message appear:
require 'pry-name' # Failed, saying: Pry is not a module
As for my guesses:
I'm creating commands writing something like this:
Pry::Commands.create_command "name-of-command" do
# my code goes here
end
and, as ruby find Pry::Commands. it want require it from lib directory not from Pry gem.
What does this error mean. Why it doesn't work. How make it work keeping in mind gem and pry requirements(pry gem starts with pry- and gem will create another directory(ies) when someone use - for example: gem pry-name will make pry/name)
Everywhere in your newly-created gem where it has module Pry, change it to: class Pry. Since Pry is already defined (as a class), you cannot redefine/reopen it as a module.

Why does ri not show TCPSocket class?

I am trying to search TCPSocket ruby class documentation with ri but it shows nothing. Most of the gems are shown. I am using rvm, and require 'socket' works perfectly. How to fix this?
Try using:
gem rdoc --all
The documentation says:
--all Generate RDoc/RI documentation for all
installed gems
You can find these things out by typing:
gem help
or
gem help rdoc
at the command line.
You might want to throw on --overwrite to overwrite existing documentation.
Old versions of Ruby, around 1.8, didn't always have the core and/or std-lib documentation generated. There's a gem you can install that patches that, but I don't remember what it was right now.

Ruby Twitter gem

-- UPDATE --
Ok its fixed. This is what I did. remove all ruby and rubygems completely. then install ruby1.9.1-full and rubygems1.9.1 then install the twitter gem.
Hi guys,
I am having trouble working with the Twitter gem. I am using ruby 1.8.7
After installing when I try to run a simple script I get this error
ruby twitter.rb
./twitter.rb:5: uninitialized constant Twitter (NameError)
from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in gem_original_require'
from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:inrequire'
from twitter.rb:2
I running this on a Ubuntu box. I checked with gem -list and I see the Twitter (1.1.0) is listed there.
this is the code I am trying to run
require "rubygems"
require 'twitter'
puts Twitter.user_timeline("test").first.text
Any ideas ?
I believe it only works with Ruby 1.9 If you want to use twitter gem try version 0.9 with Ruby 1.8.x
This works for me:
ruby-1.9.2-p0 > require 'twitter'
=> true
ruby-1.9.2-p0 > puts Twitter.user_timeline("test").first.text
TExES Pedagogy and Professional Responsibilities EC-12 Teacher Certification Test Prep Study Guid… - by Sharon A Wynne http://amzn.to/f3kF74
=> nil
which version of ruby are you using?
gouravtiwari21's comment seems to fix the problem, but it's wrong to suggest that the twitter gem requires 0.9.0 if you want to run it using Ruby 1.8.x.
You can check out the version compatibility here:
http://travis-ci.org/#!/jnunemaker/twitter
It shows the twitter gem working with Ruby installs as low as 1.8.7.
For me, it was an issue with having the correct dependent gems, as well as the correct versions.
Here's how I got it working:
I ran:
sudo gem list
And compared the versions of specific gems with what I found here:
https://github.com/jnunemaker/twitter/blob/master/HISTORY.md (I simply searched for the word 'dependency' to see which versions twitter cared about.
I also found this diff:
https://github.com/jnunemaker/twitter/commit/ac8114c1f6ba2da20c2267d3133252c2ffc6b6a3
And I compared the gems listed there with what I had installed, and I just made sure my system lined up with what I was seeing in the version notes. Oftentimes what happened is that I had multiple versions of a gem, and for some reason, the lower version was taking precedence.
I'm not sure why I still have to add
gem 'twitter', '1.7.1'
to my Gemfile, but alas, that's the last step required in order to get this stuff working.
Don't forget to restart your server, and you should be good!

Resources