How do I test Digest::SHA2.hexdigest in online irb? - ruby

I want to see the hash created by the function Digest::SHA2.hexdigest. I do not have Ruby installed, so I went for the online irb. Typing
Digest::SH­A2.hexdige­st("hello"­)
gives
=> #<NameError: uninitialized constant Digest>
Is it possible to add the needed library in any online irb?

You need to do as below :
2.0.0-p0 :003 > require 'digest'
=> true
2.0.0-p0 :004 > Digest::SHA2.hexdigest("hello")
=> "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
2.0.0-p0 :005 >
So do first require 'digest'.
Try in tutorialspoint

Related

undefined method `symbolize_keys' after requiring active support.

I am trying to symbolize the keys of a hash in a non rails project. I can see the symbolize_keys method is part of Active Support so I imported the library but it still doesn't work.
Here is an example of it failing
2.4.2 :001 > require 'active_support'
=> true
2.4.2 :002 > {'test' => 'test'}.symbolize_keys
NoMethodError: undefined method `symbolize_keys' for {"test"=>"test"}:Hash
Expected output
{test: "test"}
You should require 'active_support/all' if you want active support core extensions also required:
2.3.4 :002 > require 'active_support/all'
=> true
2.3.4 :003 > {'test' => 'test'}.symbolize_keys
=> {:test=>"test"}

IPSocket ruby NameError error?

When using IRB with input, getting error:
IPSocket.getaddress("localhost")
Error:
NameError: uninitialized constant IPSocket
Just add top before using the class :
require 'socket'
See this socket/ipsocket.c.
Example :-
2.1.0 :022 > require 'socket'
=> true
2.1.0 :023 > IPSocket.getaddress("localhost")
=> "::1"
2.1.0 :024 >

Ruby: End of file reached with gem serialport

Using gem serialport in ruby i'm having the error code EOFError: end of file reached
irb
2.0.0-p451 :001 > require 'serialport'
=> true
2.0.0-p451 :002 > serial_port = SerialPort.new "/dev/ttyUSB0", 115200
=> #<SerialPort:fd 7>
2.0.0-p451 :009 > serial_port.write "#00RD0000020054*\r"
=> 17
2.0.0-p451 :010 > r = serial_port.readline("\r")
EOFError: end of file reached
from (irb):10:in `readline'
from (irb):10
from /home/user/.rvm/rubies/ruby-2.0.0-p451/bin/irb:12:in `<main>'
any suggestion?
Try crate separate SerialPort objects for reading and writing. It seems to be an issue with Ruby - see here: http://www.rngtng.com/2009/11/27/if-your-ruby-serial-port-doesnt-read-what-youre-sending/
Attempt the following
serial_port.readlines
Instead of the
serial_port.readline("\r")
I might also suggest
serial_port.readlines("\r")

Verify version of a gem with bundler from inside Ruby

Is there a way to verify that I have the latest version of a gem from inside a Ruby program? That is, is there a way to do bundle outdated #{gemname} programmatically?
I tried looking at bundler's source code but I couldn't find a straight-forward way. Currently I'm doing this, which is fragile, slow and so inelegant:
IO.popen(%w{/usr/bin/env bundle outdated gemname}) do |proc|
output = proc.readlines.join("\n")
return output.include?("Your bundle is up to date!")
end
A way to avoid external execution:
For bundler 1.2.x
require 'bundler/cli'
# intercepting $stdout into a StringIO
old_stdout, $stdout = $stdout, StringIO.new
# running the same code run in the 'bundler outdated' utility
Bundler::CLI.new.outdated('rails')
# storing the output
output = $stdout.string
# restoring $stdout
$stdout = old_stdout
For bundler 1.3.x
require 'bundler/cli'
require 'bundler/friendly_errors'
# let's cheat the CLI class with fake exit method
module Bundler
class CLI
desc 'exit', 'fake exit' # this is required by Thor
def exit(*); end # simply do nothing
end
end
# intercepting $stdout into a StringIO
old_stdout, $stdout = $stdout, StringIO.new
# running the same code run in the 'bundler outdated' utility
Bundler.with_friendly_errors { Bundler::CLI.start(['outdated', 'rails']) }
# storing the output
output = $stdout.string
# restoring $stdout
$stdout = old_stdout
There is no programmatic way to use outdated command in bundler, because the code is in a Thor CLI file which prints output to the user. Bundler's tests are also issuing the command to the system and checking the output (Link to outdated tests).
It should be fairly simple to write your own method to mirror what the outdated method in cli.rb is doing, though. See the highlighted code here : Link to outdated method in Bundler source. Remove lines with Bundler.ui and return true/false based on the value of out_count
Update: I've extracted 'bundle outdated' into a reusable method without the console output and the exits. You can find the gist here : link to gist. I have tested this on bundler 1.3 and it seems to work.
bundle check list the gems that are out to date, you might want to use it.
Hmmm, sounds like you might want bundle show or gem env
Disappointing, this looks surprisingly difficult.
There are a couple of open issues in bundler where the official line appears to be:
At this point in time, there isn't a documented ruby API.
It's something that's on our list, though.
Looking through the bundler source code cli.rb, it's fairly clear that it's going to be tricky to call from ruby, or reproduce the code in a sensible manner.
Calling methods from CLI will be difficult because they're sprinkled with calls to exit.
Reproducing the code doesn't look fun either because there is quite a lot of bundler logic in there.
Good luck!
checking the source code of latest bundler source code
I could come up with this
https://github.com/carlhuda/bundler/blob/master/lib/bundler/cli.rb#L398
$ irb
1.9.3p327 :001 > require 'bundler'
=> true
1.9.3p327 :002 > def outdated_gems(gem_name,options={})
1.9.3p327 :003?> options[:source] ||= 'https://rubygems.org'
1.9.3p327 :004?> sources = Array(options[:source])
1.9.3p327 :005?> current_spec= Bundler.load.specs[gem_name].first
1.9.3p327 :006?> raise "not found in Gemfile" if current_spec.nil?
1.9.3p327 :007?> definition = Bundler.definition(:gems => [gem_name], :sources => sources)
1.9.3p327 :008?> options["local"] ? definition.resolve_with_cache! : definition.resolve_remotely!
1.9.3p327 :009?> active_spec = definition.index[gem_name].sort_by { |b| b.version }
1.9.3p327 :010?> if !current_spec.version.prerelease? && !options[:pre] && active_spec.size > 1
1.9.3p327 :011?> active_spec = active_spec.delete_if { |b| b.respond_to?(:version) && b.version.prerelease? }
1.9.3p327 :012?> end
1.9.3p327 :013?> active_spec = active_spec.last
1.9.3p327 :014?> raise "Error" if active_spec.nil?
1.9.3p327 :015?> outdated = Gem::Version.new(active_spec.version) > Gem::Version.new(current_spec.version)
1.9.3p327 :016?> {:outdated=>outdated,:current_spec_version=>current_spec.version.to_s,:latest_version=>active_spec.version.to_s}
1.9.3p327 :017?> end
=> nil
1.9.3p327 :018 >
1.9.3p327 :019 >
1.9.3p327 :020 >
1.9.3p327 :021 >
1.9.3p327 :022 > outdated_gems('rake')
=> {:outdated=>true, :current_spec_version=>"10.0.3", :latest_version=>"10.0.4"}
This may not work with earlier version of bundler.

How do I use an ActionView::Helper in a Ruby script, outside of Rails?

I am looking to use ActionView::Helpers::NumberHelper from a Ruby script.
What all do I need to require etc.?
~> irb
ruby-1.9.2-p180 :001 > require 'action_view'
=> true
ruby-1.9.2-p180 :002 > ActionView::Base.new.number_to_currency 43
=> "$43.00"
As of Rails 3.2.13, you can do the following:
class MyClass
include ActionView::Helpers::NumberHelper
def my_method
...
number_with_precision(number, precision: 2)
...
end
end
You might need to require 'action_view' too.
Edit: This answer is still valid in Rails 4.2.3.

Resources