<KOTP>
isene, if possible generate documentation for your intalled version of ruby and you will always have access to your documentation tailored (generated) for your version specifically.
Dooky has joined #ruby
hansolo has quit [Remote host closed the connection]
hansolo has joined #ruby
Sheilong has joined #ruby
pookie has joined #ruby
<isene>
KOTP: How do I do that? And how do I access it?
<johnjaye>
like if i wanted to convert x=2.5 to an integer to call succ on it, can i cast it to integer
<ox1eef_>
You can use Float#ceil, Float#floor, or Float#to_i.
<mooff>
only through asking an object to convert itself
<johnjaye>
right but i meant like in java where you just force a type to something
<johnjaye>
as in (float)(x) --> now x is float
<mooff>
there is .to_i on almost everything
<mooff>
try `ri String#to_i
<mooff>
= 5.0.to_i.succ
<ruby-eval>
=> 6
<mooff>
btw isene..
<ox1eef_>
Java is not comparable in this case. There is no strict typing. You can turn a float into an integer with the above mentioned methods. You can't treat 2.5 as an integer, because the concept doesn't exist in Ruby and in strictly typed languages there would most likely be coercion rules that define what 2.5 becomes (ie 2 or 3).
<mooff>
= alias :system_backticks :`; def `(cmd) = case cmd; when /^ri (.+)/ then "Documentation for #{$1}" end
<ruby-eval>
=> :`
<mooff>
= `ri String.foo`
<ruby-eval>
=> "Documentation for String.foo"
<mooff>
a variable is *always* like it was of type Object in Java, yeah
<johnjaye>
so if i have x and i want to call an method should i just test if it responds to it ?
<mooff>
yeah, that's reasonable
<ox1eef_>
Generally, yes, in practice you will find is_a? is used regularly, and that acts more like a type check.
<mooff>
there are some global methods for converting in Ruby, that can help, when you don't know what kind of object you are getting
<johnjaye>
is is_a? identical to just calling x.class
<mooff>
strangely, but also kind of nicely, they're named String(), Float(), etc
<ox1eef_>
No. It asks the object if it is a certain class / module, and if any of its ancestors are as well.
<mooff>
then it Does The Right Thing but from C, so it's faster
<mooff>
if it can't be converted (e.g. Float("dog")), it raises ArgumentError by default
<mooff>
but if you pass exception: false, then it returns nil for that instead
<mooff>
= x = Float("foo"); if x then x + 1 else "didn't convert" end
<ruby-eval>
ERROR: invalid value for Float(): "foo"
<ruby-eval>
<internal:kernel>:173:in `Float'
<mooff>
= x = Float("foo", exception: false); if x then x + 1 else "didn't convert" end
<ruby-eval>
=> "didn't convert"
<johnjaye>
i was going to ask if you could treat nil as false somehow using that. but apparently that is the default in ruby anyway
<ox1eef_>
"foo".to_f will return nil, and exceptions are not always desirable. So you can say, if "foo".to_i.nil?; puts "not an int"; end instead. And yes, nil and false are the only false values in Ruby.
<ox1eef_>
My mistake. "foo".to_i returns 0.
<johnjaye>
how do you check the empty string in ruby
<johnjaye>
other than something like str.length==0
<johnjaye>
well for example in lisp where nil and the empty list are identified, often you'll write a loop which pops elements out of a list and only stops when it is empty
<leftylink>
well, you do that with just `widgets.each { |widget| act(widget) }` or `while (widget = widgets.pop); act(widget); end`
<leftylink>
as we can see, that while loop takes advantage of the fact that nil is falsey. if [] were falsey, then the while loop would do the WRONG behaviour if widget were a []. this is why [] should not be falsey.
<johnjaye>
oh it has nice semantics. it returns nil instead of an error when the list is empty
<mooff>
Rails adds Object#blank?, and then defines it for String, Hash, etc
<mooff>
it would be really good in the stdlib
<mooff>
same with Object#try
<johnjaye>
are you going to say that Object#blank? does essentially what I said earlier?
<mooff>
yes. except, no number is blank :D
<mooff>
`gem install activesupport`, then `irb -r active_support/all`, if you want to play
<mooff>
reading ActiveSupport, too, was one of the best experiences of my programming life.
<mooff>
just the docs.
<mooff>
(and clicking "View Source" there some times)
<johnjaye>
i see...
<mooff>
several of the methods they defined have already made it into core Ruby
<johnjaye>
apparently i have to sudo gem install
<mooff>
try gem install --user
<johnjaye>
otherwise i don't have write permissions on the /var/lib/gems/2.7.0 directory
<mooff>
but sudo apt install ruby-activesupport would also be reasonable
<johnjaye>
ok. i'm not sure what the convention is
<johnjaye>
python has some weird virtualenv thing i've never understood.
<mooff>
any is good, imo
<johnjaye>
other languages people use often the distro provided package
<mooff>
i haven't needed virtual Ruby environments since last using Rails
<johnjaye>
it warned me that i dont' have ~/.local/share/gem/ruby/2.7.0/bin in my path. i cannot run gems. but that folder doesn't even exist.
<johnjaye>
apparently it installed the gems to ~/.local/share/gem/ruby/2.7.0/gems
<mooff>
all good
<mooff>
activesupport's relatively forward and backward compatible
<johnjaye>
what exactly is a virtualenv and why is everyone so fanatical about them
<mooff>
isolates the libraries installed for a given Python or Ruby project
<johnjaye>
so like it copies them?
<mooff>
they get the standard library, but only whichever gems or Python packages you install in that environment
<mooff>
so there's no potential for the system Python to influence a project's Python
<johnjaye>
that only makes sense if gems often conflict with each other
jhass has quit [Ping timeout: 240 seconds]
<mooff>
they don't, outside of web dev, imo
<johnjaye>
i could see if you needed to fix a particular library version maybe
jhass has joined #ruby
<mooff>
where your system Ruby is 2.7.0, i would encourage upgrading to a newer release of your OS :)
<mooff>
2.7.0 is the version on Debian 11, right?
<mooff>
2.7.4
<johnjaye>
yes. what debian declares, I must use.
<johnjaye>
unless compiling ruby is not too hard
<johnjaye>
then i could try that
<mooff>
bookworm is as stable as it is going to be for launch :)
<mooff>
it's been frozen since Jan or Feb. release next couple of weeks
<mooff>
also yes, that is relatively friendly
<mooff>
it just "make install"s nicely to /usr/local etc
<johnjaye>
oh ok. then i'll try it.
<johnjaye>
so many projects surprisingly don't even do that much
<mooff>
yeah. Ruby is just a ./configure && make && make install
<mooff>
make -j4 :)
<johnjaye>
"to build our project type ./blargfoo in the main dir, then make PLATFORM=hurblegurble and finally run configure git submodule init"
<mooff>
yes. don't forget to install the snarglewurtschler, which can darnglewizz the snooglesnapthes
<johnjaye>
my favorite one is a project that builds and runs perfectly fine on arm. but there is no option to explicitly say to build for it.
<johnjaye>
instead you pass the option NOX86=1
<mooff>
that'll auto-wangle your curbobitses into hard emblaggon, for upservence swangling
<johnjaye>
haha. well autotools may have drawbacks. but every build system probably does.
<johnjaye>
building software is a lot messier than i thought it was
<mooff>
yeah, certainly
Sheilong has quit []
<hexology>
wow, chruby and ruby-install *are* really nice
<hexology>
i wish i had this for python now
<hexology>
oh, speaking of which. johnjaye i almost always use a "version manager" for any language runtime that has one. i tend to ignore whatever is in my package manager except where it's required as a dep for other packages
<hexology>
i'm not much of a ruby user (yet) but it's been my successful experience with python, node, etc
<hexology>
something like chruby but for python seems like a great project
<johnjaye>
you mean the venv stuff in python?
<johnjaye>
one of the things i like about ruby vs python is it always does "the right thing" (TM) more often
<johnjaye>
but that might just be my lisp prejudice showing
<johnjaye>
like to_s taking a parameter to specify the number base. finally, common sense string manipulation
<mooff>
that's not the case for e.g. HTTPS or SSL verification
<mooff>
the module i made for IIRC was hard-fought. it's *hard* to dial SSL, with hostname and peer authentication, in Ruby
<ox1eef_>
chruby is especially nice. I don't remember ruby-install that well.
friendlypunk has joined #ruby
agent_white has quit [Remote host closed the connection]
friendlypunk has quit [Remote host closed the connection]
friendlypunk has joined #ruby
Prodigy has joined #ruby
lena64t has joined #ruby
Prodigy has quit [Quit: WeeChat 3.8]
perrierjouet has quit [Quit: WeeChat 3.8]
perrierjouet has joined #ruby
perrierjouet has quit [Quit: WeeChat 3.8]
perrierjouet has joined #ruby
ur5us has joined #ruby
otisolsen70 has quit [Quit: Leaving]
_ht has quit [Remote host closed the connection]
szkl has joined #ruby
Linux_Kerio has quit [Ping timeout: 248 seconds]
reset has joined #ruby
Sankalp- has joined #ruby
Sankalp has quit [Ping timeout: 240 seconds]
Sankalp- is now known as Sankalp
<hololeap>
I haven't used ruby much in a while, but I remember there was some gem I would use in combination with nokogiri to do some basic web page scraping. any ideas what it might have been?
<hololeap>
it could do basic http(s) stuff, store cookies, and the like
ox1eef_ has quit [Ping timeout: 240 seconds]
<hololeap>
ok, I think it might have been mechanize
<weaksauce>
hololeap basic web scraping katamari. browser automation selenium
<hololeap>
yeah, I don't need anything like selenium for this. I want to log in to my router and log some stats every few seconds
<weaksauce>
wait no nokogiri
<hololeap>
mechanize uses nokogiri
ox1eef_ has joined #ruby
roadie has quit [Ping timeout: 248 seconds]
roadie has joined #ruby
infinityfye has quit [Quit: Leaving]
fat_shinobi has joined #ruby
<isene>
Just discovered mruby. Before testing this myself, does anyone here know if my rsh would run with it - and if it would speed up rsh with a compiled execution file with embedded mruby?
roadie has quit [Ping timeout: 248 seconds]
ur5us has quit [Remote host closed the connection]
ur5us has joined #ruby
joto has quit [Remote host closed the connection]
joto has joined #ruby
huan has joined #ruby
<huan>
uhm shouldn't this (1..16).inject([]) {|result, num| result.size} give me 0?
<leah2>
no it fails?
<weaksauce>
isene depends on what you need to do in your shell
<weaksauce>
a lot of gems are incompatible with mruby
<isene>
I have no gem dependencies...
<huan>
leah2 it runs fine on my pc, it returns 8, I also put it in an online REPL and it gave me 4
<leah2>
oh, it uses the size of the integer
<weaksauce>
isene are you planning on requiring anything and using it?
<weaksauce>
if not then yeah it would be faster and smaller footprint