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)
ruby literals
add a comment |
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)
ruby literals
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 meanalias 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
add a comment |
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)
ruby literals
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
ruby literals
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 meanalias 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
add a comment |
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 meanalias 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
add a comment |
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
Thx. Smart solution. So, the closest we get with this solution is42.° 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 useputs "Hello"
instead ofputs("Hello")
,require "bigdecimal"
instead ofrequire("bigdecimal")
,attr_accessor :foo
instead ofattr_accessor(:foo)
, orinclude Foo
instead ofinclude(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
add a comment |
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)
No, it wouldn't. Becauseb(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
add a comment |
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
Thx. Smart solution. So, the closest we get with this solution is42.° 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 useputs "Hello"
instead ofputs("Hello")
,require "bigdecimal"
instead ofrequire("bigdecimal")
,attr_accessor :foo
instead ofattr_accessor(:foo)
, orinclude Foo
instead ofinclude(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
add a comment |
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
Thx. Smart solution. So, the closest we get with this solution is42.° 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 useputs "Hello"
instead ofputs("Hello")
,require "bigdecimal"
instead ofrequire("bigdecimal")
,attr_accessor :foo
instead ofattr_accessor(:foo)
, orinclude Foo
instead ofinclude(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
add a comment |
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
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
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 is42.° 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 useputs "Hello"
instead ofputs("Hello")
,require "bigdecimal"
instead ofrequire("bigdecimal")
,attr_accessor :foo
instead ofattr_accessor(:foo)
, orinclude Foo
instead ofinclude(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
add a comment |
Thx. Smart solution. So, the closest we get with this solution is42.° 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 useputs "Hello"
instead ofputs("Hello")
,require "bigdecimal"
instead ofrequire("bigdecimal")
,attr_accessor :foo
instead ofattr_accessor(:foo)
, orinclude Foo
instead ofinclude(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
add a comment |
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)
No, it wouldn't. Becauseb(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
add a comment |
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)
No, it wouldn't. Becauseb(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
add a comment |
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)
You could have:
def b(number)
BigDecimal(number)
end
Then b(42.0)
would work, pretty close to %b(42.0)
answered Nov 18 at 15:52
Dorian
12.5k37383
12.5k37383
No, it wouldn't. Becauseb(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
add a comment |
No, it wouldn't. Becauseb(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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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