<za1b1tsu>
From what I reading the only XML type formats libraries that ruby includes by default are: xml, json, yaml. Is this correct? Because I'm looking for a format like json, but without the quotes, but don't want to use an extra gem. Any advice?
<havenwood>
za1b1tsu: Marshal is another serializer that ships with Ruby.
<havenwood>
za1b1tsu: What langs are you going between?
<havenwood>
za1b1tsu: Say more about the quotes issue you have with JSON?
<i8igmac>
lol thats cool havenwood ;-) i added 41,000 uniq words with set in like 1 second
<i8igmac>
i like it
<za1b1tsu>
havenwood: so I'm using just ruby, I need to save, load, edit some data from a file. I don't need quotes for strings.
<za1b1tsu>
havenwood: SORRY, what I meant was I don't need quotes for keys!
<za1b1tsu>
I want to save data live element1, element2 etc. Where element type is hash with some keys {name: "dasda", occupation: "dasdad"...}
<za1b1tsu>
*I want to save data like
<havenwood>
za1b1tsu: Ruby ships with PStore and YAML::Store, which wrap Marshal and YAML respectively and provide transactional file storage in those formats.
<za1b1tsu>
If I use json it will be like {"name":"fasfa"...
<za1b1tsu>
hmm lemme check out PStore, thanks havenwood, can I buy you a capuccino?
<havenwood>
za1b1tsu: It ships with Ruby in the stdlib, you can use it like a Hash, and it doesn't have to read the whole data-set into memory to get at one value.
<havenwood>
The caveat is that your keys must be Strings.
dellavg_ has joined #ruby
<za1b1tsu>
so quotes right?
<havenwood>
za1b1tsu: Quotes?
<za1b1tsu>
"key":"value"
<za1b1tsu>
not key: "value"
<havenwood>
za1b1tsu: It doesn't serialize to either of those. You use it like: DB['key'] = 'value'
<havenwood>
za1b1tsu: It takes Strings as the key, not Symbols. It serializes the values to YAML, but the keys are handled by the DB.
<havenwood>
za1b1tsu: It sounds like you want to use Symbols as keys. You could certainly wrap YDBM to make it appear to work that way. Hell, that's even in the spirit of YDBM. :)
grenierm has joined #ruby
<havenwood>
za1b1tsu: Sec, I'll write you a wrapper.
<havenwood>
mm, the methods with blocks are harder
<havenwood>
za1b1tsu: For just the getter/setter, all you'd need to do is: require 'yaml/dbm'; class Za1b1tsuDB < Psych::DBM; def [] key; super key.to_s end end
goldbox has joined #ruby
<goldbox>
hi all
<havenwood>
goldbox: hi
<orbyt_>
So i've got a CSV i've parsed that has values like `3.390000`. I'm storing these in ActiveRecord as a :decimal. I noticed after it finished, that the values in my DB look like `0.339e1`.
<orbyt_>
Is there a better data type to store these values?
<goldbox>
anybody here develop ruby on windows?
elphe has quit [Ping timeout: 244 seconds]
<havenwood>
orbyt_: Which DB? Postgres?
<orbyt_>
Uh, whatever the default Rails is. MySQL i think
<havenwood>
I think Sqlite is default?
<orbyt_>
sqlite3, yea
vondruch has quit [Remote host closed the connection]
<orbyt_>
shit i should change that lol
<havenwood>
orbyt_: There are different implementations of :decimal depending on the RDBMS, is why I ask.
vondruch has joined #ruby
<orbyt_>
Yea `adapter: sqlite3`
<havenwood>
orbyt_: In Sqlite "the maximum supported :precision is 16" for :decimal, but there's no default.
dar123 has joined #ruby
graphene has quit [Ping timeout: 252 seconds]
<za1b1tsu>
havenwood: I never heard of dbm before, so I was reading on it. So from what I understand the format is like this: key: elem, key: elem
<za1b1tsu>
so basically it's big hash, correct?
<za1b1tsu>
*a
<havenwood>
orbyt_: There's a :float native type as well, if you're expecting a Float and not a BigDecimal.
<havenwood>
orbyt_: Or you can set the :precision and :scale of your :decimal.
<orbyt_>
havenwood I know Floats aren't ideal for accurate numbers/long decimals, though they are more performant
<havenwood>
za1b1tsu: Yeah, like a Hash that you can store to disk, but still be able to read out a single key without having to load the whole Hash back into memory.
<orbyt_>
These values are orbital parameters and such, so it's kind of important they are right
drale2k_ has joined #ruby
MoritaShinobu has joined #ruby
<havenwood>
orbyt_: Haha, then a decimal sounds right. Postgres is the one implementation that allows a :precision of infinity. :)
<orbyt_>
I'll fire up a rails project with postgres and check it out, thanks
<havenwood>
orbyt_: Do you know how many significant digits there will be?
<orbyt_>
~10 decimal places or so
<havenwood>
orbyt_: The inspect value your seeing is right for a BigDecmial. You can format it to print like a Float: BigDecimal.new('0.339e1').to_s 'f' #=> "3.39"
<havenwood>
orbyt_: Engineering notation "E" is the default, but conventional float "F" can be specified by that first #to_s param.
<orbyt_>
How does it format it like a float but maintain the decimal places?
<havenwood>
orbyt_: It's not converting to a Float with all that comes with that. It's just giving you a String with the correct digits in floating point notation.
<havenwood>
orbyt_: For example, try: BigDecimal.new('0.1e-1_000_000_000').to_s 'F'
<havenwood>
orbyt_: You'll notice zeros being printed for a VERY long time. :P
johnny56 has quit [Ping timeout: 272 seconds]
dar123 has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
elphe has joined #ruby
<orbyt_>
:p
dar123 has joined #ruby
dellavg_ has quit [Ping timeout: 252 seconds]
elphe has quit [Ping timeout: 260 seconds]
DTZUZO has joined #ruby
kapil____ has joined #ruby
reber has joined #ruby
moei has quit [Read error: Connection reset by peer]
moei has joined #ruby
mattp_ has quit [Read error: No route to host]
mattp_ has joined #ruby
Cork has quit [Remote host closed the connection]
johnny56 has joined #ruby
Cork has joined #ruby
elphe has joined #ruby
tdy has quit [Quit: WeeChat 2.2]
elphe has quit [Ping timeout: 272 seconds]
sauvin has joined #ruby
<za1b1tsu>
havenwood: DBM is not really a human readble format, right? Unless I'm doing something wrong. I can't just open the db file with a texteditor
<havenwood>
za1b1tsu: You can open it with a text editor, but it's a binary format so it'll look like gibberish.
<havenwood>
za1b1tsu: It's not human-readable.
<za1b1tsu>
I see, well learned something new today
<havenwood>
za1b1tsu: If you say more about what you're doing I bet we can give better suggestions.
dar123 has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
<za1b1tsu>
a commandline note taker, notes are oganized in hashes: {text: "", name ""}
<za1b1tsu>
you do note --add, and then takes your input and adds a note to notes.txt
<za1b1tsu>
but I also want to give the option to edit them directly in the editor
<za1b1tsu>
basically I'm searching for possible XML formats, less verbose and easy to read
<za1b1tsu>
but I undestand the limitations now
dar123 has joined #ruby
<za1b1tsu>
Im going to look into the Marshall thing you mentioned
<havenwood>
za1b1tsu: Marshal is fast but it isn't human readable.
<za1b1tsu>
oh
<za1b1tsu>
so basically my options are: json or yaml, if I want to use the default ruby library
<havenwood>
za1b1tsu: or XML
<havenwood>
;P
<havenwood>
za1b1tsu: Or CSV
<za1b1tsu>
hmmm
<za1b1tsu>
csv
<za1b1tsu>
now that is interesting
orbyt_ has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
MIR100 has joined #ruby
apeiros_ has joined #ruby
ansraliant has joined #ruby
elphe has joined #ruby
code_zombie has quit [Read error: Connection reset by peer]
<za1b1tsu>
If try fd.tell == 0 it crashes, if I try fd.tell ==1 it won't read the first character in the file (the last when reading backwards)
<apeiros_>
== only compares…
<apeiros_>
and what do you mean by "crashes"?
duderonomy has joined #ruby
duderonomy has quit [Client Quit]
<za1b1tsu>
apeiros_: it's in pastebin
<za1b1tsu>
multiline error, I can't paste it here, would be spam
<za1b1tsu>
*pasteofcode
<za1b1tsu>
what I'm trying to do is read a file from bottom to top untill a match
arkaros has quit [Ping timeout: 246 seconds]
<apeiros_>
ah, somehow I missed your paste link
<apeiros_>
well, read(1) will move your position. not sure why it wouldn't read the last char if you do == 1, though.
<apeiros_>
btw., reading 1 byte per iteration means that e.g. utf-8 will not be read correctly.
<apeiros_>
not sure why you use loop instead of e.g. fd.size.times do …
<apeiros_>
also that fd.read at the end…?
<apeiros_>
just tried your code. reads the last char just fine with == 1, but of course your fd.read at the end won't (since read(1) moved your position by 1, which is why it is at 1 and not 0 anymore…)
lxsameer has joined #ruby
mr_rich101 has quit [Ping timeout: 272 seconds]
jidar_ has joined #ruby
arkaros has joined #ruby
mr_rich101 has joined #ruby
jidar has quit [Read error: Connection reset by peer]
jidar_ is now known as jidar
LiftLeft has quit [Ping timeout: 252 seconds]
<havenwood>
za1b1tsu: This is less fun, but: File.open(filename) { |file| file.each_char.reverse_each { |char| puts char } }
DTZUZO has quit [Ping timeout: 252 seconds]
nowhere_man has joined #ruby
<apeiros_>
it also reads the full file first :)
<za1b1tsu>
yeah, I can't do that
<apeiros_>
(which I would assume beats the purpose of the exercise)
apeiros_ is now known as apeiros
<za1b1tsu>
apeiros: if I should not read 1 byte per iteration, how should I do it? The plan is to make a match
<za1b1tsu>
if it maches "---" I want to stop
<havenwood>
also i didn't read a single byte
<za1b1tsu>
and retrieve the data until that point
<apeiros>
za1b1tsu: if "-" is "\0x45", then you can treat it as binary and read byte by byte.
<za1b1tsu>
is this better? I changed things based on your suggestions
<apeiros>
eh, that's a rather terrible solution. you read 1 byte, then 2 bytes, then n bytes from the file
<apeiros>
so worst case you read n^2 bytes
<apeiros>
1) open the file with binary encoding, else you'll get encoding errors
<apeiros>
2) read 1 byte at a time and buffer it up (it's probably more performant to read chunks of a couple of KB though, but IMO do that once you get it running with 1 byte reads)
<apeiros>
3) then do as you do now - test the first 3 bytes of your buffer
<apeiros>
then if you got that running, rephrase your loop to have the "---" test + filesize as the loop condition instead of abusing break.
<apeiros>
re 2), ensure your buffer is binary too. again, else you'll get encoding errors.
venmx has joined #ruby
esrse has quit [Ping timeout: 252 seconds]
Beams has joined #ruby
<marz_d`ghostman>
I have a method that wraps a `` command, and when a Errno::ENOENT exception is raised, I'm logging it to a file. SInce the logger is another module's responsibility, I don't want to test it on the current class so instead I'm doing exit(1). How do I test that it terminates with exit_code 1? I'm currently trying .to terminate, but it's asking for argument, tried giving 0 but it doesn't work
<apeiros>
note: both untested. but at the very least should help to get an idea.
<apeiros>
depending on your usecase, you'll want to return "".b instead of nil. also depending on your use-case, you'll want to force_encoding to the actual encoding of the file at the end.
<marz_d`ghostman>
http://termbin.com/hlrt Why am I getting wrong number of arguments error from this?
<marz_d`ghostman>
`included': wrong number of arguments (given 1, expected 0) (ArgumentError) It means I've given 1 and it expects 0 right?
<apeiros>
yes
<marz_d`ghostman>
that seems misleading
<apeiros>
why?
<apeiros>
it does get 1. your definition expects 0. it's exactly what's happening.
<marz_d`ghostman>
If included needs to get an argument, that means it is expecting 1, and I have given 0?
<apeiros>
no
<apeiros>
the *protocol* expects you to define included as a method expecting 1
<marz_d`ghostman>
apeiros: Ah I see
<apeiros>
*you* method definition expects 0
<apeiros>
*your
<marz_d`ghostman>
so when I do include Logging, it is passing the current class as the argument
xfbs has joined #ruby
vondruch has quit [Ping timeout: 252 seconds]
<marz_d`ghostman>
apeiros: How do I create instance variables for the class the includes a module?
<apeiros>
instance variables are "created" by assigning to them. make sure you don't confuse class- and instance-level.
<marz_d`ghostman>
apeiros: so basically, what I'm trying to do is assign a logger for each instance of a class. So I want to define it inside the self.included method of the module Logging
<marz_d`ghostman>
cause include Module, creates instance methods for the object right?
<apeiros>
it adds the included module as an ancestor of the class where you include it. the methods are available through inheritance. as if you'd subclassed a class.
<apeiros>
and "for each instance" won't be possible within the self.included hook. think about it: which instances of your class are available at that point?
<marz_d`ghostman>
apeiros: How do you suggest I do it?
<marz_d`ghostman>
perhaps call another function that generates it?
ta_ has quit [Remote host closed the connection]
<apeiros>
I don't know enough to make a sensible suggestion.
<apeiros>
sounds like you want to set a logger at class level and delegate to the class from the instances.
<marz_d`ghostman>
apeiros: So I have this sync class. Each instance will have its own log file
<marz_d`ghostman>
and I want the logging mechanism to be exported to a module
<apeiros>
and where are you stumbling?
arkaros has quit [Ping timeout: 252 seconds]
elphe has joined #ruby
za1b1tsu has quit [Ping timeout: 252 seconds]
arkaros has joined #ruby
dbz has joined #ruby
arkaros has quit [Ping timeout: 252 seconds]
dbz has quit [Ping timeout: 252 seconds]
za1b1tsu has joined #ruby
akosednar has quit [Ping timeout: 268 seconds]
jic has joined #ruby
akosednar has joined #ruby
AJA4350 has joined #ruby
<marz_d`ghostman>
apeiros: on how to create the the logger for each instance of the class
xfbs has quit [Quit: afk]
xfbs has joined #ruby
ua_ has quit [Ping timeout: 246 seconds]
ua has joined #ruby
rishispeets has joined #ruby
arkaros has joined #ruby
GodFather has joined #ruby
rishispeets has quit [Client Quit]
rishispeets has joined #ruby
<rishispeets>
Hey guys. I'm trying to make the global rbenv ruby installation my ruby in bash. I have added rbenv to my PATH but "which ruby" still points to the system install. Am I missing something?
<rishispeets>
Hey all* ;)
kapil____ has quit [Quit: Connection closed for inactivity]
fluxAeon has joined #ruby
<apeiros>
marz_d`ghostman: that's like waaaay to generic
<rishispeets>
Ah, guess I needed to add shims file path besides the bin path to PATH.
mike11 has quit [Read error: Connection reset by peer]
rishispeets has quit [Ping timeout: 252 seconds]
dinfuehr has quit [Ping timeout: 244 seconds]
Nicmavr has quit [Write error: Connection reset by peer]
dinfuehr has joined #ruby
rishispeets has joined #ruby
Nicmavr has joined #ruby
gregf_ has joined #ruby
akaiiro has quit [Remote host closed the connection]
mzo has quit [Ping timeout: 240 seconds]
im0nde has joined #ruby
akaiiro has joined #ruby
bkxd_ has quit [Ping timeout: 252 seconds]
akaiiro has quit [Remote host closed the connection]
I0 has quit [Remote host closed the connection]
venmx has quit [Ping timeout: 252 seconds]
zapata has quit [Read error: Connection reset by peer]
zapata has joined #ruby
troys has joined #ruby
dbz has joined #ruby
gnufied has quit [Quit: Leaving]
marz_d`ghostman has joined #ruby
dbz has quit [Ping timeout: 245 seconds]
za1b1tsu has quit [Ping timeout: 268 seconds]
<marz_d`ghostman>
What's the correct grammar for this? expect_any_instance_of(Kernel).not_to receive(:`).with(cmd)
darkxploit has quit [Ping timeout: 256 seconds]
venmx has joined #ruby
grvgr has joined #ruby
gnufied has joined #ruby
axsuul has quit [Ping timeout: 260 seconds]
axsuul has joined #ruby
za1b1tsu has joined #ruby
elphe has quit [Ping timeout: 252 seconds]
za1b1tsu has quit [Ping timeout: 252 seconds]
elphe has joined #ruby
rishispeets has quit [Ping timeout: 244 seconds]
alem0lars has joined #ruby
dostoyevsky has quit [Quit: leaving]
dostoyevsky has joined #ruby
alem0lars has quit [Max SendQ exceeded]
samort7 has joined #ruby
aufi_ has joined #ruby
rishispeets has joined #ruby
bmurt has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
maufart__ has quit [Ping timeout: 252 seconds]
rippa has joined #ruby
savolla has joined #ruby
bmurt has joined #ruby
dbz has joined #ruby
xfbs has quit [Quit: afk]
ss942 has left #ruby [#ruby]
WA9ACE has quit [Remote host closed the connection]
ivanskie has joined #ruby
elphe has quit [Ping timeout: 268 seconds]
aupadhye has joined #ruby
<zxq2>
one issue with dynamic langs is certain errors aren't detected unless the code path is traversed, like say a typo in a variable name or constant. is there some kind of static analysis tool for ruby that can detect this kind of error? are there any safeguards?
<zxq2>
one of my commits slipped through code review with this problem, want to avoid it in the future.
<zxq2>
would think it's relatively common in dynamic langs
aufi_ has quit [Ping timeout: 246 seconds]
<zxq2>
i bet some IDEs do it
Azure|dc has quit [Quit: Oops.]
savolla has quit [Ping timeout: 246 seconds]
Azure has joined #ruby
Azure has quit [Client Quit]
dbz has quit [Remote host closed the connection]
optikalmouse has joined #ruby
za1b1tsu has quit [Ping timeout: 252 seconds]
<optikalmouse>
how slow is the ruby interpreter (2.x) in reading files? if I have a 1000 line file that's read & evaluated, will that be slower to run than a 100 line file? or is the difference too small to notice?
<optikalmouse>
I'm trying to figure out whether duplicate code removal and fewer lines of code have a *real* performance impact aside from the cognitive impact that they have
Azure has joined #ruby
grenierm_ has joined #ruby
darkhanb has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
<optikalmouse>
true, I just benchmarked the difference between map(&:downcase) and map {|x| x.downcase}, seems like there is a diff for blocks vs procs..and we have &:whatever littered all over our codebase heh
<alireza>
Which name is a better name for class that formats a ActiveRecord::Exception to a JSON
<alireza>
ErrorJSONFormatter
<alireza>
JSONErrorFormatter
elphe has joined #ruby
<alireza>
what would you prefer a class that formats an specific series of exceptions to JSON
code_zombie has joined #ruby
<alireza>
err, what would you call/prefer to call
<cthulchu>
folks, I'm trying to read a file, but this file is gonna be written to at the same time
<cthulchu>
I think it's fine cuz I'm still allowed to read a file even if someone else is writing into it, right?
arkaros has quit [Ping timeout: 246 seconds]
alireza has quit [Quit: WeeChat 2.1]
akem has quit [Remote host closed the connection]
akem has joined #ruby
elphe has quit [Ping timeout: 252 seconds]
rishispeets has quit [Ping timeout: 245 seconds]
howdoicomputer has joined #ruby
elphe has joined #ruby
xfbs has quit [Quit: afk]
elphe has quit [Ping timeout: 252 seconds]
venmx has joined #ruby
howdoicomputer has quit [Quit: WeeChat 2.2]
fredlinhares has joined #ruby
nowhere_man has joined #ruby
elphe has joined #ruby
xfbs has joined #ruby
<cthulchu>
what does a colon mean before a variable?
dar123 has joined #ruby
<cthulchu>
like a = :b?
<cthulchu>
errr
<fredlinhares>
cthulchu: it's a 'symbol'
krawchyk has joined #ruby
<apeiros>
that it isn't a variable. it's a Symbol literal. like "b" is a String literal and 12 is an Integer literal.
DTZUZO has joined #ruby
elphe has quit [Ping timeout: 268 seconds]
<cthulchu>
oh, okay
<cthulchu>
it's just I always use quotes to cast into string
<cthulchu>
well kinda
<cthulchu>
I guess ruby doesn't care about them
<fredlinhares>
cthulchu: no, Symbols are not strings.
<apeiros>
yeah, using quotes doesn't "cast" a symbol into a string. you just have a string from the get go.
<cthulchu>
anyhow. It lets me do things in a comfortable way and I'm grateful for that
<cthulchu>
errr
<apeiros>
:foo is not the same as "foo" just as 12 is not the same as "12".
<cthulchu>
:b is not ":b"?
<apeiros>
no
<cthulchu>
what is a symbol?
<apeiros>
please google. in short, it's a number which has a string "attached".
arkaros has joined #ruby
<fredlinhares>
Symbol is a kind of "universal enum"
<apeiros>
it's used internally for everything in ruby which has a name. `def foo` will internally create :foo, `class Bar` will create :Bar, `@foo = 12` will create :@foo
pulgolino has quit [Quit: WeeChat 0.4.2]
<cthulchu>
wooooooow
<cthulchu>
this is cool
doubledup has quit [Quit: Leaving]
<apeiros>
I don't think enums are in the same category of things, since usually with enum you say "this value belongs to that enum", and you can't have a value which does not exist in "that" enum. neither is the case for symbols.
pulgolino has joined #ruby
elphe has joined #ruby
<cthulchu>
so symbol means that the name goes after it
<cthulchu>
the name that was defined before
<cthulchu>
hm
<cthulchu>
ok, I need to read about def :)
<apeiros>
o0
<apeiros>
it's the thing you use to define methods. you kinda should have seen that by now.
<cthulchu>
oh, so it's just for methods?
<apeiros>
it? def? yes, def is just to define methods.
<cthulchu>
cool then
<fredlinhares>
In ruby everything is object, so there is no pure functions, only methods.
<cthulchu>
why do I have to explicitly say that it's a method?
<cthulchu>
I usually call methods just like that
<cthulchu>
with no :
<cthulchu>
a()
<cthulchu>
errr
arkaros has quit [Ping timeout: 252 seconds]
<fredlinhares>
def is to define a method, not to use it.
<apeiros>
because different syntaxes fulfill different purposes.
<cthulchu>
oh, right right
<cthulchu>
forgot
<cthulchu>
so when I say a = :b what ends up being in a?
<cthulchu>
a link to b?
<cthulchu>
or b?
<fredlinhares>
a reference to :b
<cthulchu>
ok, good
<apeiros>
as always with assignments: an object. and in this case, an object which is a Symbol instance with the literal representation :b
<apeiros>
(or the string value "b", the numeric value is determined at runtime)