Is it possible to define own literals in Ruby?











up vote
1
down vote

favorite












For arbitrary-precision floating point decimal arithmetic in Ruby we can use the library BigDecimal. Unfortunately, compared to floats every explicitly given BigDecimal needs a lot of typing:



bd = BigDecimal("42.0")
# vs.
fl = 42.0


Is is possible to define own literals in Ruby?



So that for example the BigDecimal from above could be expressed like:



bd = 42°0


Or at least:



bd = %b(42.0)









share|improve this question






















  • Saves a little bit of typing: BD = BigDecimal. Gets you close to %b, with same amount of typing.
    – Casper
    Nov 18 at 12:28






  • 1




    @Casper You mean alias BD BigDecimal?
    – Min-Soo Pipefeet
    Nov 18 at 12:37










  • Yes that's what I meant, but I just realized you need the quotes for floats, so it's not the same as %b would be. Still a bit clunky, just shorter.
    – Casper
    Nov 18 at 13:46

















up vote
1
down vote

favorite












For arbitrary-precision floating point decimal arithmetic in Ruby we can use the library BigDecimal. Unfortunately, compared to floats every explicitly given BigDecimal needs a lot of typing:



bd = BigDecimal("42.0")
# vs.
fl = 42.0


Is is possible to define own literals in Ruby?



So that for example the BigDecimal from above could be expressed like:



bd = 42°0


Or at least:



bd = %b(42.0)









share|improve this question






















  • Saves a little bit of typing: BD = BigDecimal. Gets you close to %b, with same amount of typing.
    – Casper
    Nov 18 at 12:28






  • 1




    @Casper You mean alias BD BigDecimal?
    – Min-Soo Pipefeet
    Nov 18 at 12:37










  • Yes that's what I meant, but I just realized you need the quotes for floats, so it's not the same as %b would be. Still a bit clunky, just shorter.
    – Casper
    Nov 18 at 13:46















up vote
1
down vote

favorite









up vote
1
down vote

favorite











For arbitrary-precision floating point decimal arithmetic in Ruby we can use the library BigDecimal. Unfortunately, compared to floats every explicitly given BigDecimal needs a lot of typing:



bd = BigDecimal("42.0")
# vs.
fl = 42.0


Is is possible to define own literals in Ruby?



So that for example the BigDecimal from above could be expressed like:



bd = 42°0


Or at least:



bd = %b(42.0)









share|improve this question













For arbitrary-precision floating point decimal arithmetic in Ruby we can use the library BigDecimal. Unfortunately, compared to floats every explicitly given BigDecimal needs a lot of typing:



bd = BigDecimal("42.0")
# vs.
fl = 42.0


Is is possible to define own literals in Ruby?



So that for example the BigDecimal from above could be expressed like:



bd = 42°0


Or at least:



bd = %b(42.0)






ruby literals






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 18 at 12:12









Min-Soo Pipefeet

19310




19310












  • Saves a little bit of typing: BD = BigDecimal. Gets you close to %b, with same amount of typing.
    – Casper
    Nov 18 at 12:28






  • 1




    @Casper You mean alias BD BigDecimal?
    – Min-Soo Pipefeet
    Nov 18 at 12:37










  • Yes that's what I meant, but I just realized you need the quotes for floats, so it's not the same as %b would be. Still a bit clunky, just shorter.
    – Casper
    Nov 18 at 13:46




















  • Saves a little bit of typing: BD = BigDecimal. Gets you close to %b, with same amount of typing.
    – Casper
    Nov 18 at 12:28






  • 1




    @Casper You mean alias BD BigDecimal?
    – Min-Soo Pipefeet
    Nov 18 at 12:37










  • Yes that's what I meant, but I just realized you need the quotes for floats, so it's not the same as %b would be. Still a bit clunky, just shorter.
    – Casper
    Nov 18 at 13:46


















Saves a little bit of typing: BD = BigDecimal. Gets you close to %b, with same amount of typing.
– Casper
Nov 18 at 12:28




Saves a little bit of typing: BD = BigDecimal. Gets you close to %b, with same amount of typing.
– Casper
Nov 18 at 12:28




1




1




@Casper You mean alias BD BigDecimal?
– Min-Soo Pipefeet
Nov 18 at 12:37




@Casper You mean alias BD BigDecimal?
– Min-Soo Pipefeet
Nov 18 at 12:37












Yes that's what I meant, but I just realized you need the quotes for floats, so it's not the same as %b would be. Still a bit clunky, just shorter.
– Casper
Nov 18 at 13:46






Yes that's what I meant, but I just realized you need the quotes for floats, so it's not the same as %b would be. Still a bit clunky, just shorter.
– Casper
Nov 18 at 13:46














2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










No, Ruby does not allow user-defined literals, overloading of literals, or any other similar thing.



Ruby does allow defining operator methods for existing operators, but not the definition of new operators, so even treating



42°0


as a binary operator ° will not work.



The closest you can get would be monkey-patching a ° method on Integer:



class Integer
def °(decimal_part)
BigDecimal("#{self}.#{decimal_part}")
end
end





share|improve this answer























  • Thx. Smart solution. So, the closest we get with this solution is 42.° 0.
    – Min-Soo Pipefeet
    Nov 18 at 12:46








  • 1




    @Dorian: Ruby allows you to leave out the parentheses around an argument list. I am surprised that you have never seen it used, e.g. it is quite common to use puts "Hello" instead of puts("Hello"), require "bigdecimal" instead of require("bigdecimal"), attr_accessor :foo instead of attr_accessor(:foo), or include Foo instead of include(Foo). In all of these cases it is, in fact, not just merely common, it is idiomatic.
    – Jörg W Mittag
    Nov 18 at 16:52


















up vote
0
down vote













You could have:



def b(number)
BigDecimal(number)
end


Then b(42.0) would work, pretty close to %b(42.0)






share|improve this answer





















  • No, it wouldn't. Because b(42.0) already imposes the loss of precision. b("42.0") would work. But this is no more so close to %b(42.0).
    – Min-Soo Pipefeet
    Nov 18 at 18:37











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53360723%2fis-it-possible-to-define-own-literals-in-ruby%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










No, Ruby does not allow user-defined literals, overloading of literals, or any other similar thing.



Ruby does allow defining operator methods for existing operators, but not the definition of new operators, so even treating



42°0


as a binary operator ° will not work.



The closest you can get would be monkey-patching a ° method on Integer:



class Integer
def °(decimal_part)
BigDecimal("#{self}.#{decimal_part}")
end
end





share|improve this answer























  • Thx. Smart solution. So, the closest we get with this solution is 42.° 0.
    – Min-Soo Pipefeet
    Nov 18 at 12:46








  • 1




    @Dorian: Ruby allows you to leave out the parentheses around an argument list. I am surprised that you have never seen it used, e.g. it is quite common to use puts "Hello" instead of puts("Hello"), require "bigdecimal" instead of require("bigdecimal"), attr_accessor :foo instead of attr_accessor(:foo), or include Foo instead of include(Foo). In all of these cases it is, in fact, not just merely common, it is idiomatic.
    – Jörg W Mittag
    Nov 18 at 16:52















up vote
1
down vote



accepted










No, Ruby does not allow user-defined literals, overloading of literals, or any other similar thing.



Ruby does allow defining operator methods for existing operators, but not the definition of new operators, so even treating



42°0


as a binary operator ° will not work.



The closest you can get would be monkey-patching a ° method on Integer:



class Integer
def °(decimal_part)
BigDecimal("#{self}.#{decimal_part}")
end
end





share|improve this answer























  • Thx. Smart solution. So, the closest we get with this solution is 42.° 0.
    – Min-Soo Pipefeet
    Nov 18 at 12:46








  • 1




    @Dorian: Ruby allows you to leave out the parentheses around an argument list. I am surprised that you have never seen it used, e.g. it is quite common to use puts "Hello" instead of puts("Hello"), require "bigdecimal" instead of require("bigdecimal"), attr_accessor :foo instead of attr_accessor(:foo), or include Foo instead of include(Foo). In all of these cases it is, in fact, not just merely common, it is idiomatic.
    – Jörg W Mittag
    Nov 18 at 16:52













up vote
1
down vote



accepted







up vote
1
down vote



accepted






No, Ruby does not allow user-defined literals, overloading of literals, or any other similar thing.



Ruby does allow defining operator methods for existing operators, but not the definition of new operators, so even treating



42°0


as a binary operator ° will not work.



The closest you can get would be monkey-patching a ° method on Integer:



class Integer
def °(decimal_part)
BigDecimal("#{self}.#{decimal_part}")
end
end





share|improve this answer














No, Ruby does not allow user-defined literals, overloading of literals, or any other similar thing.



Ruby does allow defining operator methods for existing operators, but not the definition of new operators, so even treating



42°0


as a binary operator ° will not work.



The closest you can get would be monkey-patching a ° method on Integer:



class Integer
def °(decimal_part)
BigDecimal("#{self}.#{decimal_part}")
end
end






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 18 at 12:49

























answered Nov 18 at 12:23









Jörg W Mittag

286k62352544




286k62352544












  • Thx. Smart solution. So, the closest we get with this solution is 42.° 0.
    – Min-Soo Pipefeet
    Nov 18 at 12:46








  • 1




    @Dorian: Ruby allows you to leave out the parentheses around an argument list. I am surprised that you have never seen it used, e.g. it is quite common to use puts "Hello" instead of puts("Hello"), require "bigdecimal" instead of require("bigdecimal"), attr_accessor :foo instead of attr_accessor(:foo), or include Foo instead of include(Foo). In all of these cases it is, in fact, not just merely common, it is idiomatic.
    – Jörg W Mittag
    Nov 18 at 16:52


















  • Thx. Smart solution. So, the closest we get with this solution is 42.° 0.
    – Min-Soo Pipefeet
    Nov 18 at 12:46








  • 1




    @Dorian: Ruby allows you to leave out the parentheses around an argument list. I am surprised that you have never seen it used, e.g. it is quite common to use puts "Hello" instead of puts("Hello"), require "bigdecimal" instead of require("bigdecimal"), attr_accessor :foo instead of attr_accessor(:foo), or include Foo instead of include(Foo). In all of these cases it is, in fact, not just merely common, it is idiomatic.
    – Jörg W Mittag
    Nov 18 at 16:52
















Thx. Smart solution. So, the closest we get with this solution is 42.° 0.
– Min-Soo Pipefeet
Nov 18 at 12:46






Thx. Smart solution. So, the closest we get with this solution is 42.° 0.
– Min-Soo Pipefeet
Nov 18 at 12:46






1




1




@Dorian: Ruby allows you to leave out the parentheses around an argument list. I am surprised that you have never seen it used, e.g. it is quite common to use puts "Hello" instead of puts("Hello"), require "bigdecimal" instead of require("bigdecimal"), attr_accessor :foo instead of attr_accessor(:foo), or include Foo instead of include(Foo). In all of these cases it is, in fact, not just merely common, it is idiomatic.
– Jörg W Mittag
Nov 18 at 16:52




@Dorian: Ruby allows you to leave out the parentheses around an argument list. I am surprised that you have never seen it used, e.g. it is quite common to use puts "Hello" instead of puts("Hello"), require "bigdecimal" instead of require("bigdecimal"), attr_accessor :foo instead of attr_accessor(:foo), or include Foo instead of include(Foo). In all of these cases it is, in fact, not just merely common, it is idiomatic.
– Jörg W Mittag
Nov 18 at 16:52












up vote
0
down vote













You could have:



def b(number)
BigDecimal(number)
end


Then b(42.0) would work, pretty close to %b(42.0)






share|improve this answer





















  • No, it wouldn't. Because b(42.0) already imposes the loss of precision. b("42.0") would work. But this is no more so close to %b(42.0).
    – Min-Soo Pipefeet
    Nov 18 at 18:37















up vote
0
down vote













You could have:



def b(number)
BigDecimal(number)
end


Then b(42.0) would work, pretty close to %b(42.0)






share|improve this answer





















  • No, it wouldn't. Because b(42.0) already imposes the loss of precision. b("42.0") would work. But this is no more so close to %b(42.0).
    – Min-Soo Pipefeet
    Nov 18 at 18:37













up vote
0
down vote










up vote
0
down vote









You could have:



def b(number)
BigDecimal(number)
end


Then b(42.0) would work, pretty close to %b(42.0)






share|improve this answer












You could have:



def b(number)
BigDecimal(number)
end


Then b(42.0) would work, pretty close to %b(42.0)







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 18 at 15:52









Dorian

12.5k37383




12.5k37383












  • No, it wouldn't. Because b(42.0) already imposes the loss of precision. b("42.0") would work. But this is no more so close to %b(42.0).
    – Min-Soo Pipefeet
    Nov 18 at 18:37


















  • No, it wouldn't. Because b(42.0) already imposes the loss of precision. b("42.0") would work. But this is no more so close to %b(42.0).
    – Min-Soo Pipefeet
    Nov 18 at 18:37
















No, it wouldn't. Because b(42.0) already imposes the loss of precision. b("42.0") would work. But this is no more so close to %b(42.0).
– Min-Soo Pipefeet
Nov 18 at 18:37




No, it wouldn't. Because b(42.0) already imposes the loss of precision. b("42.0") would work. But this is no more so close to %b(42.0).
– Min-Soo Pipefeet
Nov 18 at 18:37


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53360723%2fis-it-possible-to-define-own-literals-in-ruby%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy