IPSocket ruby NameError error? - ruby

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 >

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"}

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")

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

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

Ruby classes with namespaces

Why is this not legal name-spacing ? We use this often with our ActiveRecord classes. Does AR do something magical?
$ irb
1.9.3-p194 :001 > class F::B
1.9.3-p194 :002?> end
NameError: uninitialized constant F
from (irb):1
from /Users/bob/.rvm/rubies/ruby-1.9.3-p194/bin/irb:16:in `<main>'
Because F is not defined before you reference it.
You must first define F as a Class or Module. Try this:
module F; end # OR class F; end
class F::B; end
f = F::B.new # => #<F::B:0x007fba3c1046d8>
I think you're forgetting about modules.
1.9.3p429 :001 > module F
1.9.3p429 :002?> class B
1.9.3p429 :003?> end
1.9.3p429 :004?> end
=> nil
1.9.3p429 :005 > F::B.new
=> #<F::B:0x0000000082a230>
1.9.3p429 :006 >
If the module is defined before the class, it will work:
module F; end
class F::B; end
2.0.0p247 :001 > module F;end
=> nil
2.0.0p247 :002 > class F::B;end
=> nil
2.0.0p247 :003 > F
=> F
2.0.0p247 :004 > F::B
=> F::B

Why is Ruby's Date class automatically loaded but DateTime is not?

Using IRB, why are the Date & Time classes automatically loaded, but DateTime is not? I have to require 'date', this does not make sense to me because I thought that both Date and DateTime were using the standard library 'date'?
ruby-1.9.2-p290 :001 > Date
=> Date
ruby-1.9.2-p290 :002 > Time
=> Time
ruby-1.9.2-p290 :003 > DateTime
NameError: uninitialized constant Object::DateTime
from (irb):3
from /Users/kamilski81/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `<main>'
ruby-1.9.2-p290 :004 > require 'date'
=> true
ruby-1.9.2-p290 :005 > require 'date'
=> false
ruby-1.9.2-p290 :006 > DateTime
=> DateTime
In IRB, include this line: require 'date' then you will be able to use DateTime.
irb(main):000:0> DateTime.class
NameError: uninitialized constant DateTime
from (irb):0
from /path/to/ruby/irb:12:in '(main)'
irb(main):001:0> require 'date'
=> true
irb(main):002:0> DateTime.class
=> Class
Worked for me when first initializing with require 'date'.
Being a little more curious, I tried:
$ ruby -e 'puts DateTime.class'
-e:1:in `<main>': uninitialized constant Object::DateTime (NameError)
[~, kamilski81#mac]
$ ruby -e 'puts Date.class'
-e:1:in `<main>': uninitialized constant Object::Date (NameError)
$ ruby -e 'puts Time.class'
Class
So it makes me think that it's an irb issue that automatically loads 'date'.

Resources