Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 00:07
    Blacksmoke16 labeled #12783
  • 00:07
    Blacksmoke16 labeled #12783
  • Nov 26 23:55
    carlhoerberg opened #12783
  • Nov 26 11:21
    straight-shoota milestoned #12781
  • Nov 26 11:21
    straight-shoota milestoned #12781
  • Nov 26 02:15
    straight-shoota assigned #11331
  • Nov 25 23:24
    straight-shoota labeled #12782
  • Nov 25 23:24
    straight-shoota labeled #12782
  • Nov 25 23:24
    straight-shoota opened #12782
  • Nov 25 23:17
    straight-shoota labeled #12781
  • Nov 25 23:17
    straight-shoota labeled #12781
  • Nov 25 23:17
    straight-shoota opened #12781
  • Nov 25 23:01
    straight-shoota synchronize #11635
  • Nov 25 22:56
    straight-shoota synchronize #11840
  • Nov 25 22:49
    straight-shoota labeled #12636
  • Nov 25 19:21
    straight-shoota labeled #12780
  • Nov 25 19:21
    straight-shoota labeled #12780
  • Nov 25 19:21
    straight-shoota labeled #12780
  • Nov 25 19:21
    straight-shoota opened #12780
  • Nov 25 19:11
    straight-shoota labeled #12779
moe
@moe:busyloop.net
[m]
well, the ruby collection interface in general
or smalltalk or wherever it originally came from...
Daniel Berger
@djberg96
Does something like this already exist in core?
class File
  def self.readn(file : String, nbytes : Number) : Bytes
    bytes = Bytes.new(nbytes)

    begin
      fh = File.open(file)
      fh.read(bytes)
    ensure
      fh.close if fh
    end

    bytes
  end
end
Ali Naqvi
@naqvis
don't believe stdlib have one
Daniel Berger
@djberg96
ok, thanks
and if there's a nicer way to do what I'm doing there, I'm all ears
Ali Naqvi
@naqvis
all good, few suggestions
  • you can follow stdlib api style for params i.e accept file as Path | String, accept encoding etc params
  • you can use open with block so you don't have to deal with closing file while you are done with reading
  def self.readn(filename : Path | String, nbytes : Int32, encoding = nil, invalid = nil) : Bytes
    return Bytes.empty unless nbytes > 0
    open(filename, "r") do |file|
      file.set_encoding(encoding, invalid: invalid) if encoding
      bytes = Bytes.new(nbytes)
      file.read(bytes)
      bytes
    end
  end
Daniel Berger
@djberg96
Excellent, thanks!
Ali Naqvi
@naqvis
:rose:
Daniel Berger
@djberg96
Just curious, why Int32 over Number?
Ali Naqvi
@naqvis
Int32 is the default type, but sure you can expand param type to more broader type like Int or Number
though I doubt Number will work, as Number includes floating points as well
while Bytes size is restricted to Int
so you can restrict nbytes to Int instead, to allow other integer types
def self.readn(filename : Path | String, nbytes : Int, encoding = nil, invalid = nil) : Bytes
will make it accept any of the signed/unsigned integer types
Daniel Berger
@djberg96
fantastic, thank you
azurelmao
@azurelmao
Is it possible to call the initialize method of the inherited class in crystal?
like in java you would do super(params)
azurelmao
@azurelmao
I see
George Dietrich
@Blacksmoke16
so yes, should work just like java
Daniel Berger
@djberg96
What's the equivalent of "\xBA\xB4\u0000\u0000".unpack('i').first in Crystal?
I'm poking around the String class, but just not seeing it I guess
(use case: i'm reading magic numbers out of image files)
George Dietrich
@Blacksmoke16
what kind of image?
George Dietrich
@Blacksmoke16
but it would be something with IO, where you do something like io.read_bytes(Int32) or io.read_bytes Int32, IO::ByteFormat::BigEndian depending on the type/endianess you want to read
Daniel Berger
@djberg96
In this particular case, bitmaps. First 2 bytes should be "BM", next 4 are the file size.
so i'm just validating that those 4 bytes line up with the actual file size
George Dietrich
@Blacksmoke16

could do something like:

File.open image_path do |io|
  first_char = io.read_string 1
  second_char = io.read_string 1

  size = io.read_bytes Int32
end

think that should work

Daniel Berger
@djberg96
Yep, got something like that using the block above from @naqvis, byte slice like Bytes[66, 77, 186, 180, 0, 0]. First 2 bytes are "BM", now i just need to conver the last 4 to an number.
George Dietrich
@Blacksmoke16
dont even really need that method, can just open the file and call methods on the io and call it a day
just tried it:
$ file src/components/image_size/spec/images/bmp/v2.42x50_0_0.bmp 
src/components/image_size/spec/images/bmp/v2.42x50_0_0.bmp: PC bitmap, OS/2 1.x format, 42 x 50 x 24, cbSize 6426, bits offset 26

$ cr src/components/image_size/src/athena-image_size.cr 
"B"
"M"
6426
Daniel Berger
@djberg96
well, i'm trying to use that method as a general solution, because I'm also reading magic values for other formats, like jpg, png, etc
so, just for the sake of argument, how would I convert a byte slice like Bytes[186,180,0,0] to an int?
George Dietrich
@Blacksmoke16
first way that comes to mind is like IO::Memory.new(bytes, writeable: false).read_bytes Int32
wait
IO::ByteFormat::LittleEndian.decode Int32, bytes
using the correct endianness of whatever format you're parsng
Daniel Berger
@djberg96
that works, thanks!
George Dietrich
@Blacksmoke16
:thumbsup:
stellarpower
@stellarpower
Looks like Complex doesn't have a method to raise it to a power. Am I missing something?
stellarpower
@stellarpower
Github is getting worse, I sweat. Finally found it anyway
stellarpower
@stellarpower
*Github search
Daniel Berger
@djberg96
A bit confused as to why Bytes#to_s isn't effectively the same as String.new(bytes). The current behavior of the former doesn't strike me as very useful or intuitive. What am I missing?
George Dietrich
@Blacksmoke16
the former is giving you a string representation of the bytes themselves, not what they represent, while the latter creates a new string instance by interpreting the provided bytes as a string
e.g. creating a string from a pointer or something from a c lib